''
'' This code is part of Document Solutions for PDF .NET demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Collections.Generic
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.Annotations
'' When merging PDFs that contain identical images, by default the resulting PDF
'' will end up containing several copies of the same image.
'' To ensure that the PDF is not larger than necessary, DsPdf provides the method
'' GcPdfDocument.RemoveDuplicateImages() that scans the PDF, finds and removes duplicates.
'' The MergeDocumentOptions class also has the property RemoveDuplicateImages,
'' which allows you to scan for/remove duplicates at once when merging PDFs,
'' but by default it is false. This is because the occurrence of duplicate images
'' in merged PDFs is not a very common scenario, while scanning for duplicates
'' may affect the performance of the merge. Also, if you are merging multiple PDFs
'' that contain duplicate images, it is better to merge all PDFs first, and then
'' remove all duplicates from the final PDF at once with a single call to
'' GcPdfDocument.RemoveDuplicateImages(), as demonstrated by this demo.
''
'' In this demo we merge several sample invoices that include the same company logo,
'' and then call GcPdfDocument.RemoveDuplicateImages() and compare the sizes of the
'' resulting PDF before and after that call.
''
'' See also OptimizeFonts.
Public Class RemoveDuplicateImages
Private Shared Function MergePDFs(dest As GcPdfDocument, fpath As String) As FileStream
Dim d = New GcPdfDocument()
Dim fs = File.OpenRead(fpath)
d.Load(fs)
dest.MergeWithDocument(d)
Return fs
End Function
Public Function CreatePDF(ByVal stream As Stream) As Integer
'' Input file names:
Const N = 7
Dim fpaths = New List(Of String)(N)
For i = 1 To 7
fpaths.Add(Path.Combine("Resources", "PDFs", $"DsDemoInvoice{i}.pdf"))
Next
' Facilitate clean up:
Dim fss = New List(Of FileStream)()
Dim tempPdfBefore As String = Nothing
Dim tempPdfAfter As String = Nothing
Try
Dim doc = New GcPdfDocument()
'' Merge the invoices into a single PDF:
For i = 0 To N - 1
fss.Add(MergePDFs(doc, fpaths(i)))
Next
'' Compare the merged PDF size before and after removing duplicates:
tempPdfBefore = Path.GetTempFileName()
doc.Save(tempPdfBefore)
Dim sizeBefore = New FileInfo(tempPdfBefore).Length
doc.RemoveDuplicateImages()
tempPdfAfter = Path.GetTempFileName()
doc.Save(tempPdfAfter)
Dim sizeAfter = New FileInfo(tempPdfAfter).Length
Util.AddNote(String.Format(
"Using the GcPdfDocument.RemoveDuplicateImages() method on this document (which was produced by merging " &
"several demo invoices with the same company logo) reduced the size of the merged PDF from {0:N0} to {1:N0} bytes.",
sizeBefore, sizeAfter),
doc.Pages.Insert(0))
doc.Save(stream)
'' Done:
Return doc.Pages.Count
Finally
If tempPdfBefore IsNot Nothing Then
File.Delete(tempPdfBefore)
End If
If tempPdfAfter IsNot Nothing Then
File.Delete(tempPdfAfter)
End If
fss.ForEach(Sub(fs_) fs_.Dispose())
End Try
End Function
End Class