''
'' 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
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing
'' This sample shows how to use the SavePdfOptions.UseObjectStreams property
'' to reduce the size of a PDF.
''
'' Note that as with most optimizations, the size reduction will vary depending
'' on the actual content of the source PDF.
Public Class ObjectStreams
Public Function CreatePDF(ByVal stream As Stream) As Integer
'' Create a 5 page non-optimal PDF:
Dim tmpInput = MakeInputFile()
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)
' By default GcPdfDocument uses CompressionLevel.Fastest when saving a PDF.
' If PDF size is important, we should use optimal compression:
tmpDoc.CompressionLevel = CompressionLevel.Optimal
' Minimize stream sizes, use object streams:
tmpDoc.Save(tmpOutput, New SavePdfOptions(SaveMode.Default, PdfStreamHandling.MinimizeSize, UseObjectStreams.Multiple))
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 UseObjectStreams.Multiple option when saving a PDF will in most cases reduce the resulting file size, " &
"sometimes significantly. In this case the size of the PDF generated by the 'Large Document' sample decreased " &
"from {0:N0} to {1:N0} bytes, without any loss in fidelity or PDF opening speed." & vbLf &
"Using the UseObjectStreams.Single option yields an even slightly smaller PDF size at the cost of slower opening in PDF viewers." & vbLf &
"To reproduce these results locally, download and run this sample, specifying a valid license key " &
"(otherwise loading is limited to 5 pages, and the size reduction may be too small). " &
"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() As String
'' Number of pages to generate:
Const N = Util.LargeDocumentIterations
Dim start = Util.TimeNow()
Dim doc = New GcPdfDocument()
' Prep a TextLayout to hold/format the text:
Dim tl = New TextLayout(72) With {
.MaxWidth = doc.PageSize.Width,
.MaxHeight = doc.PageSize.Height,
.MarginAll = 72,
.FirstLineIndent = 36
}
tl.DefaultFormat.Font = StandardFonts.Times
tl.DefaultFormat.FontSize = 12
' Generate the document:
For pageIdx = 0 To N - 1
tl.Append(Util.LoremIpsum(1))
tl.PerformLayout(True)
doc.NewPage().Graphics.DrawTextLayout(tl, PointF.Empty)
tl.Clear()
Next
' Insert a title page:
tl.FirstLineIndent = 0
Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSerif-Regular.ttf"))
Dim tf0 = New TextFormat() With {.Font = fnt, .FontSize = 24, .FontBold = True}
tl.Append(String.Format("Large Document" & vbLf & "{0} Pages of Lorem Ipsum" & vbLf & vbLf, N), tf0)
Dim tf1 = New TextFormat(tf0) With {.FontSize = 14, .FontItalic = True}
tl.Append(String.Format("Generated on {0} in {1:m\m\ s\s\ fff\m\s}.", Util.TimeNow().ToString("R"), Util.TimeNow() - start), tf1)
tl.TextAlignment = TextAlignment.Center
tl.PerformLayout(True)
doc.Pages.Insert(0).Graphics.DrawTextLayout(tl, PointF.Empty)
'' Save the resultant PDF in a temp file with UseObjectStreams.None (it is the default):
Dim outFn = Path.GetTempFileName()
doc.Save(outFn, New SavePdfOptions(SaveMode.Default, PdfStreamHandling.Copy, UseObjectStreams.None))
Return outFn
End Function
End Class