OptimizePDF.vb
''
'' This code is part of Document Solutions for PDF .NET demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.IO.Compression
Imports System.Drawing
Imports System.Collections.Generic
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Common

'' This sample shows how to use the GcPdfDocument.Optimize() method
'' to reduce the size of a PDF by applying various optimization strategies,
'' such as using the optimal compression, using object streams,
'' merging duplicate and removing unused embedded fonts, and so on.
'' For details on the optimization strategies, see the OptimizeDocumentOptions class.
''
'' This sample extracts the first five pages of a PDF, and then merges those pages
'' into a single new document without applying any optimizations.
'' The sample records the merged PDF's size, then runs Optimize() on that PDF
'' and records the size of the optimized PDF. These results are printed into the
'' PDF returned by the sample, thus illustrating the effect of running Optimize()
'' with most optimization options on (the only option that is not applied is
'' removing embedded fonts, which is not recommended in most cases as it can produce
'' documents not readable on some systems).
''
'' See also OptimizeFonts and RemoveDuplicateImages.
Public Class OptimizePDF
    Public Function CreatePDF(ByVal stream As Stream) As Integer
        '' Create a 5 page non-optimal PDF:
        Dim srcFn = Path.Combine("Resources", "PDFs", "CompleteJavaScriptBook.pdf")
        Dim tmpInput = MakeInputFile(srcFn)
        Dim fiInput = New FileInfo(tmpInput)

        '' Create a new PDF, load the source PDF into it, and optimize it:
        Dim tmpOutput = Path.GetTempFileName()
        Dim tmpDoc = New GcPdfDocument()
        Using fs = File.OpenRead(tmpInput)
            tmpDoc.Load(fs)
            Dim odo = New OptimizeDocumentOptions()
            ' Maximize minimization:
            odo.SetForMinimumSize()
            ' But do not remove embedded fonts which is not recommended in most cases:
            odo.RemoveEmbeddedFonts = False
            tmpDoc.Optimize(tmpOutput, odo)
        End Using
        Dim fiOutput = New FileInfo(tmpOutput)

        '' Record the input and output file sizes in the resultant PDF:
        Dim doc = New GcPdfDocument()
        Util.AddNote(String.Format(
            "Using the GcPdfDocument.Optimize() method reduced the size of a 5-page PDF from {0:N0} to {1:N0} bytes " &
            "by applying various optimization strategies." & vbLf &
            "To reproduce these results locally, download and run this sample. You may also modify the sample code to keep the temporary " &
            "input and output files and compare their sizes using a file manager.", fiInput.Length, fiOutput.Length),
            doc.NewPage())
        doc.Save(stream)

        '' Clean up:
        File.Delete(tmpInput)
        File.Delete(tmpOutput)

        '' Done:
        Return doc.Pages.Count
    End Function

    Private Shared Function MakeInputFile(inFn As String) As String
        Dim indoc = New GcPdfDocument()
        Using fs = File.OpenRead(inFn)
            indoc.Load(fs)

            ' Create 5 PDFs from the first 5 pages of the source document:
            Dim pageCount = 5
            Dim docs = New List(Of GcPdfDocument)(pageCount)
            For i = 0 To pageCount - 1
                Dim outdoc = New GcPdfDocument()
                outdoc.MergeWithDocument(indoc, New MergeDocumentOptions() With {.PagesRange = New OutputRange(i + 1, i + 1)})
                docs.Add(outdoc)
            Next
            ' Merge the PDFs into a single document:
            Dim doc = New GcPdfDocument()
            For Each d In docs
                doc.MergeWithDocument(d)
            Next

            ' Save the resultant PDF in a temp file:
            Dim outFn = Path.GetTempFileName()
            doc.Save(outFn)
            Return outFn
        End Using
    End Function
End Class