Private Sub 压缩图片pdfToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 压缩图片pdfToolStripMenuItem.Click
Dim quality As Integer = 80
Dim scale As Integer = 2
Dim qualitystr() As String = Split(InputBox("设置图片质量,缩小倍数,逗号分隔。", "", "80,2"), ",")
If IsNumeric(qualitystr(0)) editor barThen
quality = qualitystr(0)
End If
If IsNumeric(qualitystr(1)) Then
scale = qualitystr(1)
End If
Dim fp As String = TextBox1.Text.Trim
Dim nfp As String = Label1.Text
Dim fl() As String = Directory.GetFiles(fp, "*.pdf", SearchOption.AllDirectories)
dgv1dt.Rows.Clear()
dgv1dt.Columns.Clear()
dgv1dt.Columns.Add()
dgv1.DataSource = dgv1dt
For Each fn As String In fl
Try
'估计单页大小 大于2M
Dim fnl() As String = tools1.getfileinfo(fn)
Dim npdf As String = nfp & "\" & fnl(1) & ".pdf"
Dim pdfdoc As New Aspose.Pdf.Document(fn)
Dim fi As New System.IO.FileInfo(fn)
Dim fiMB As Double = fi.Length / 1000000
If fiMB / pdfdoc.Pages.Count > 1 And File.Exists(npdf) = False Then
dgv1addrow(fn)
tsCount1.Text = fn
tools1.AppendText("d:\", "开始" & fn)
Application.DoEvents()
Dim spdfpath As String = fnl(0) & "\" & fnl(1) & "_s" '拆分目录
Dim opdfpath As String = fnl(0) & "\" & fnl(1) & "_o" '压缩目录
If Directory.Exists(spdfpath) = False Then
Directory.CreateDirectory(spdfpath)
End If
If Directory.Exists(opdfpath) = False Then
Directory.CreateDirectory(opdfpath)
End If
splitpdf(fn, spdfpath) '拆分
Dim spdfnl() As String = Directory.GetFiles(spdfpath, "*.pdf")
'压缩
probar.Maximum = spdfnl.Length
probar.Value = 0
tsCount1.Text = "正在压缩"
For Each spdfn As String In spdfnl
OptimizationPDFImg(spdfn, opdfpath & "\" & tools1.getfileinfo(spdfn)(1) & ".pdf", scale, quality)
probar.PerformStep()
Application.DoEvents()
Next
'合并
tsCount1.Text = "正在合并"
Application.DoEvents()
Dim pdfl() As String = Directory.GetFiles(opdfpath, "*.pdf")
'Instantiate PdfFileEditor object
Dim pdfEditor As Aspose.Pdf.Facades.PdfFileEditor = New Aspose.Pdf.Facades.PdfFileEditor()
'Call Concatenate method of PdfFileEditor object to concatenate all input files
'into a single output file
pdfEditor.Concatenate(pdfl, nfp & "\" & fnl(1) & ".pdf")
Directory.Delete(spdfpath, True)
'Directory.Delete(opdfpath, True)
End If
Catch ex As Exception
'Dim dr As DataRow = dgv1dt.NewRow
'dr(0) = ex.Message & fn
dgv1addrow(ex.Message & fn)
tools1.AppendText("d:\", "错误" & ex.Message & fn)
End Try
Application.DoEvents()
Next
tsCount1.Text = "完成"
End Sub
''' <summary>
''' 压缩pdf中的图片
''' </summary>
''' <param name="fn"></param>
''' <param name="nfn"></param>
''' <param name="scale"></param>
''' <param name="quality"></param>
''' <remarks></remarks>
Private Sub OptimizationPDFImg(ByVal fn As String, ByVal nfn As String, ByVal scale As Integer, Optional ByVal quality As Integer = 80)
If File.Exists(nfn) = False And File.Exists(fn) = True Then
Dim pdfDocument As Aspose.Pdf.Document = New Aspose.Pdf.Document(fn)
For Each page As Aspose.Pdf.Page In pdfDocument.Pages
Dim idx As Integer = 1
For Each image As Aspose.Pdf.XImage In page.Resources.Images
Dim imageStream As MemoryStream = New MemoryStream()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论