'''' This code is part of Document Solutions for PDF .NET demos.'' Copyright (c) MESCIUS inc. All rights reserved.''ImportsSystem.IOImportsSystem.Collections.GenericImportsGrapeCity.Documents.Pdf '' 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.PublicClassRemoveDuplicateImagesPrivateSharedFunctionMergePDFs(dest AsGcPdfDocument, fpath AsString) AsFileStreamDim d = NewGcPdfDocument()Dim fs = File.OpenRead(fpath) d.Load(fs) dest.MergeWithDocument(d)Return fsEndFunction PublicFunctionCreatePDF(ByVal stream AsStream) AsInteger'' Input file names:ConstN = 7Dim fpaths = NewList(OfString)(N)For i = 1To7 fpaths.Add(Path.Combine("Resources", "PDFs", $"DsDemoInvoice{i}.pdf"))Next' Facilitate clean up:Dim fss = NewList(OfFileStream)()Dim tempPdfBefore AsString = NothingDim tempPdfAfter AsString = NothingTryDim doc = NewGcPdfDocument()'' Merge the invoices into a single PDF:For i = 0ToN - 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 = NewFileInfo(tempPdfBefore).Length doc.RemoveDuplicateImages() tempPdfAfter = Path.GetTempFileName() doc.Save(tempPdfAfter)Dim sizeAfter = NewFileInfo(tempPdfAfter).LengthUtil.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.CountFinallyIf tempPdfBefore IsNotNothingThenFile.Delete(tempPdfBefore)EndIfIf tempPdfAfter IsNotNothingThenFile.Delete(tempPdfAfter)EndIf fss.ForEach(Sub(fs_) fs_.Dispose())EndTryEndFunctionEndClass