[实用]VB.NET PDF文件批量打印

2019-07-22 14:56:27 浏览数 (1)

VB.NET PDF文件批量打印,可以指定打印的页面,倒叙打印,顺序打印!方便快捷!


Imports Spire.License Imports Spire.Pdf Imports System.Drawing.Printing Imports System.IO

Public Class pdf_print Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load get_pt() End Sub Sub PT_PDF() On Error Resume Next If pd_file() = False Then Exit Sub Dim pdf_page As Integer = 0 Dim path As String Dim pdf As New PdfDocument For Each file_name In txt_coding.Lines If file_name <> “” Then path = txt_path.Text & file_name & “.pdf” If cb_all.Checked = True Then pdf_page = pdf.Pages.Count pdf.LoadFromFile(path) End If If cb_zh.Checked = True Then pdf.LoadFromFile(path) pdf_page = pdf.Pages.Count pdf.PrintFromPage = pdf_page pdf.PrintToPage = pdf_page End If If cb_zd.Checked = True Then pdf.LoadFromFile(path) pdf_page = pdf.Pages.Count pdf.PrintFromPage = zd_ks.Text pdf.PrintToPage = zd_js.Text End If If cb_ds.Checked = True Then pdf.LoadFromFile(path) pdf_page = pdf.Pages.Count 1 pdf.PrintFromPage = pdf_page - Val(ds_ks.Text) pdf.PrintToPage = pdf_page - Val(ds_js.Text) End If pdf.PrinterName = pt_list.Text pdf.PrintDocument.Print() End If Application.DoEvents() Next End Sub

代码语言:javascript复制
Private Function Boo_FileExist(ByVal Str_File As String) As Boolean
   Boo_FileExist = System.IO.File.Exists(Str_File)
End Function
Function pd_file() As Boolean
   Dim temp As String = ""
   For Each file_name In txt_coding.Lines
       If file_name <> "" Then
           If Boo_FileExist(txt_path.Text & file_name & ".pdf") = False Then
               temp = file_name   vbCrLf   temp
           End If
       End If
   Next
   If temp <> "" Then
       MsgBox(temp, MsgBoxStyle.Exclamation, "警告:没有以下文件;请下载!")
       Return False
   Else
       Return True
   End If
End FunctionSub get_pt() ''获取打印机列表
   On Error Resume Next
   Dim prtdoc As New PrintDocument()
   Dim strDefaultPrinter As String = prtdoc.PrinterSettings.PrinterName
   Dim strPrinter As [String]
   For Each strPrinter In PrinterSettings.InstalledPrinters
       pt_list.Items.Add(strPrinter)
       If strPrinter = strDefaultPrinter Then
           pt_list.SelectedIndex = pt_list.Items.IndexOf(strPrinter)
       End If
   Next strPrinter
   pt_list.Text = strDefaultPrinter
End Sub
Sub get_filelist()
   Try
       Dim dirs As String() = Directory.GetFiles(txt_path.Text, "*.pdf")
       Dim dir As String
       Dim filel As String
       For Each dir In dirs
           filel = Split(dir, "")(UBound(Split(dir, "")))
           txt_coding.Text = Split(filel, ".pdf")(0)   vbCrLf   txt_coding.Text
           Application.DoEvents()
       Next
   Catch e As Exception
       MsgBox("文件列表获取失败!", vbInformation, "VB小源码")
   End Try
End SubPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_pt_pdf.Click
   If txt_coding.Text <> "" Then
       PT_PDF()
   Else
       MsgBox("请输入文件名或者获取文件列表!", MsgBoxStyle.Information, "VB小源码")
   End IfEnd SubPrivate Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cb_zh.CheckedChanged
   If cb_zh.Checked = True Then
       cb_all.Checked = False
       cb_zd.Checked = False
       zd_ks.Enabled = False
       zd_js.Enabled = False
       cb_ds.Checked = False
       ds_ks.Enabled = False
       ds_js.Enabled = False
   End If
End SubPrivate Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cb_zd.CheckedChanged
   If cb_zd.Checked = True Then
       zd_ks.Enabled = True
       zd_js.Enabled = True
       cb_all.Checked = False
       cb_zh.Checked = False
       cb_ds.Checked = False
       ds_ks.Enabled = False
       ds_js.Enabled = False
   End If
End SubPrivate Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cb_all.CheckedChanged
   If cb_all.Checked = True Then
       zd_ks.Enabled = False
       zd_js.Enabled = False
       cb_zh.Checked = False
       cb_zd.Checked = False
       cb_ds.Checked = False
       ds_ks.Enabled = False
       ds_js.Enabled = False
   End If
End SubPrivate Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_lj.Click
   Dim path_p As String
   get_path_pdf.ShowDialog()
   get_path_pdf.RootFolder = Environment.SpecialFolder.Desktop
   path_p = get_path_pdf.SelectedPath & ""
   If path_p <> "" Then
       txt_path.Text = path_p
   End IfEnd SubPrivate Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cb_ds.CheckedChanged
   If cb_ds.Checked = True Then
       zd_ks.Enabled = False
       zd_js.Enabled = False
       cb_zh.Checked = False
       cb_zd.Checked = False
       cb_all.Checked = False
       ds_ks.Enabled = True
       ds_js.Enabled = True
   End If
End SubPrivate Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ds_ks.TextChangedEnd Sub
Private Sub clear_list_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clear_list.Click
   txt_coding.Clear()
End SubPrivate Sub 获取文件列表_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles get_file.Click
   If MsgBox("建议:选择少于1000个文件的文件夹获取列表!" & vbCrLf & "是否继续?", vbYesNo, "VB小源码") = vbYes Then
       get_filelist()
   Else
       Exit Sub
   End IfEnd SubPrivate Sub ds_js_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ds_js.TextChanged
   ds_ks.Text = ds_js.Text
End SubPrivate Sub zd_ks_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles zd_ks.TextChanged
   zd_js.Text = zd_ks.Text
End Sub

End Class

0 人点赞