.Net 4.8, Spire PDF, PDF Kopmrimieren

This commit is contained in:
2024-08-30 22:41:48 +02:00
parent 59873ed3e0
commit 2d6ad53ddd
17 changed files with 310 additions and 130 deletions

View File

@@ -3,6 +3,9 @@
'Imports iTextSharp.text
Imports System.IO
Imports Spire.Pdf
Imports Spire.Pdf.Conversion
Imports Spire.Pdf.Conversion.Compression
Imports Spire.Pdf.Exporting
Imports VERAG_PROG_ALLGEMEIN
'Imports GrapeCity.Documents.Pdf
@@ -115,6 +118,11 @@ Public Class frmSendungAnhangImport
initFlpANH_LIST()
End Sub
AddHandler USRCNTL.ME_COMPRESS, Sub(docId, anhId)
compressElement(docId, anhId)
initFlpANH_LIST()
End Sub
AddHandler USRCNTL.KeyDown, Sub(ob As Object, ev As KeyEventArgs)
If ev.KeyCode = Keys.Delete Then
deleteActiveElement(getAcitvePnl())
@@ -645,11 +653,11 @@ Public Class frmSendungAnhangImport
Public Function ExtractPdfPages_NEW(ByVal SourceFile As String, ByVal TargetFile As String, pagesToExtract As List(Of Integer)) As Boolean 'ITEXTSHARP
Try
Dim impPage As itextsharp.text.pdf.PdfImportedPage = Nothing
Dim Reader As New itextsharp.text.pdf.PdfReader(SourceFile)
Dim impPage As iTextSharp.text.pdf.PdfImportedPage = Nothing
Dim Reader As New iTextSharp.text.pdf.PdfReader(SourceFile)
If Not TargetFile.ToLower.EndsWith(".pdf") Then TargetFile = TargetFile & ".pdf"
Dim srcDoc As New itextsharp.text.Document() 'Reader.GetPageSizeWithRotation(0))
Dim PdfCopyProvider As New itextsharp.text.pdf.PdfCopy(srcDoc, New System.IO.FileStream(TargetFile, System.IO.FileMode.Create))
Dim srcDoc As New iTextSharp.text.Document() 'Reader.GetPageSizeWithRotation(0))
Dim PdfCopyProvider As New iTextSharp.text.pdf.PdfCopy(srcDoc, New System.IO.FileStream(TargetFile, System.IO.FileMode.Create))
PdfCopyProvider.SetFullCompression()
srcDoc.Open()
For Each p In pagesToExtract
@@ -660,7 +668,7 @@ Public Class frmSendungAnhangImport
End If
impPage = PdfCopyProvider.GetImportedPage(Reader, p)
' ----- Ermitteln der Seitenauflösung und setzen für die neue Seite
PdfCopyProvider.SetPageSize(New itextsharp.text.Rectangle(0.0F, 0.0F, impPage.Width, impPage.Height, Reader.GetPageRotation(p)))
PdfCopyProvider.SetPageSize(New iTextSharp.text.Rectangle(0.0F, 0.0F, impPage.Width, impPage.Height, Reader.GetPageRotation(p)))
' ----- PDF Seite in das neue Dokument einfügen
PdfCopyProvider.AddPage(impPage)
@@ -896,7 +904,7 @@ Public Class frmSendungAnhangImport
Try
PagesLeft = 0
Dim r As itextsharp.text.pdf.PdfReader = New itextsharp.text.pdf.PdfReader(sourceFile)
Dim r As iTextSharp.text.pdf.PdfReader = New iTextSharp.text.pdf.PdfReader(sourceFile)
Dim pagesToKeep As New List(Of Integer)
For p = 1 To r.NumberOfPages
Dim del = False
@@ -918,9 +926,9 @@ Public Class frmSendungAnhangImport
Using fs As FileStream = New FileStream(destinationFile, FileMode.Create, FileAccess.Write, FileShare.None)
Using doc As itextsharp.text.Document = New itextsharp.text.Document()
Using doc As iTextSharp.text.Document = New iTextSharp.text.Document()
Dim w As itextsharp.text.pdf.PdfWriter = itextsharp.text.pdf.PdfWriter.GetInstance(doc, fs)
Dim w As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, fs)
doc.Open()
For Each page As Integer In pagesToKeep
doc.NewPage()
@@ -1046,6 +1054,100 @@ Public Class frmSendungAnhangImport
Return False
End Function
Shared Function compressElement(FILE_DOCID, FILE_ANHID, Optional showMessage = True) As Boolean
If Not showMessage OrElse vbYes = MsgBox("Möchten Sie das Element komprimieren? Durch Minderung der Datenqualität gehen Informationen eventuell verloren.", vbYesNoCancel) Then
Try
Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER(FILE_DOCID)
'Load a PDF document while initializing the PdfCompressor object
Dim compressor As Spire.Pdf.Conversion.Compression.PdfCompressor = New Spire.Pdf.Conversion.Compression.PdfCompressor(DS.GET_TOP1_PATH)
'Get text compression options
Dim textCompression As TextCompressionOptions = compressor.Options.TextCompressionOptions
'Compress fonts
textCompression.CompressFonts = True
'Unembed fonts
textCompression.UnembedFonts = True
'Get image compression options
Dim imageCompression As ImageCompressionOptions = compressor.Options.ImageCompressionOptions
'Set the compressed image quality
imageCompression.ImageQuality = ImageQuality.High
'Resize images
imageCompression.ResizeImages = True
'Compress images
imageCompression.CompressImage = True
'Save the compressed document to file
compressor.CompressToFile(DS.GET_TOP1_PATH)
'Load the pdf document
Dim doc As New PdfDocument()
doc.LoadFromFile(DS.GET_TOP1_PATH)
'Compress the content in document
CompressContent(doc)
'Compress the images in document
CompressImage(doc)
'Save the document
doc.SaveToFile(DS.GET_TOP1_PATH)
'View the pdf document
PDFDocumentViewer(DS.GET_TOP1_PATH)
'Dim Convert = New PdfGrayConverter("C:\Users\DEVELOPER1\Desktop\CompressDocument_result2.pdf")
'Convert.ToGrayPdf("C:\Users\DEVELOPER1\Desktop\CompressDocument_result3.pdf")
Catch ex As Exception
VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name,, "Fehler QuickScan")
End Try
End If
Return False
End Function
Private Shared Sub CompressContent(ByVal doc As PdfDocument)
'Disable the incremental update
doc.FileInfo.IncrementalUpdate = False
'Set the compression level to best
doc.CompressionLevel = PdfCompressionLevel.Best
End Sub
Private Shared Sub CompressImage(ByVal doc As PdfDocument)
Try
'Disable the incremental update
doc.FileInfo.IncrementalUpdate = False
'Traverse all pages
For Each page As PdfPageBase In doc.Pages
If page IsNot Nothing Then
If page.ImagesInfo IsNot Nothing Then
For Each info As PdfImageInfo In page.ImagesInfo
page.TryCompressImage(info.Index)
Next info
End If
End If
Next page
Catch ex As Exception
VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name,, "Fehler QuickScan")
End Try
End Sub
Private Shared Sub PDFDocumentViewer(ByVal fileName As String)
Try
Process.Start(fileName)
Catch
End Try
End Sub
Private Sub frmSendungAnhangImport_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.Height = Screen.PrimaryScreen.WorkingArea.Height
@@ -1226,6 +1328,7 @@ Public Class frmSendungAnhangImport
If pnl Is Nothing OrElse pnl Is flpnlSendungen Then flpnlSendungen.Controls.AddRange(ListToAdd_flpnlSendungen.ToArray)
If pnl Is Nothing OrElse pnl Is flpnlAviso Then flpnlAviso.Controls.AddRange(ListToAdd_flpnlAviso.ToArray)
'If act Is Nothing Then initLocationViewer("", "PDF")
End Sub
@@ -1239,6 +1342,7 @@ Public Class frmSendungAnhangImport
USRCNTL.FILE_NAME = a.anh_Name
USRCNTL.FILE_TYPE = a.anh_Typ
USRCNTL.FILE_SIZE = a.anh_size.ToString("N0") & " KB"
USRCNTL.FILE_ANHID = a.anh_id
USRCNTL.FILE_DOCID = a.anh_docId
@@ -1326,7 +1430,7 @@ Public Class frmSendungAnhangImport
End Try
End Sub
Sub addUsrcnlToPnl(TMP_Path, dicId, anhId, Optional filename = "", Optional Typ = "")
Sub addUsrcnlToPnl(TMP_Path, dicId, anhId, Optional filename = "", Optional Typ = "", Optional Size = "")
Dim USRCNTL As New usrCntlSendungAnhangElement
Dim fi As New FileInfo(TMP_Path)
@@ -1339,6 +1443,7 @@ Public Class frmSendungAnhangImport
USRCNTL.FILE_ANHID = anhId
USRCNTL.FILE_TYPE = Typ
USRCNTL.FILE_SIZE = Size
initElement(USRCNTL)
flpnlAviso.Controls.Add(USRCNTL)