Sample code for 30+ languages & platforms
VB.NET Requires Chilkat v11.7.0+

Optimize a PDF Without Rewriting It (Incremental Update)

See more PDF Optimization Examples

Demonstrates the one combination of options that does not rewrite the file. With dropOldRevisions false and objectStreams "disable", the optimized objects are appended to the original bytes as an incremental update: every byte of the input is still there, byte for byte, followed by the changes and a new cross-reference section. That is what keeps an existing digital signature valid, and what lets a reader that has the original cached fetch only the tail. The file gets a little larger before it gets smaller, so this is for compatibility, not for size.

Chilkat VB.NET Downloads

VB.NET
Dim success As Boolean = False

'  This example requires the Chilkat API to have been previously unlocked.
'  See Global Unlock Sample for sample code.

'  The PDF used in this example is available at watersense_accomplishments.pdf

Dim pdf As New Chilkat.Pdf
success = pdf.LoadFile("qa_data/pdf/watersense_accomplishments.pdf")
If (success = False) Then
    Debug.WriteLine(pdf.LastErrorText)
    Exit Sub
End If


Dim json As New Chilkat.JsonObject
json.UpdateBool("dropOldRevisions",False)
json.UpdateString("objectStreams","disable")

Dim jsonOut As New Chilkat.JsonObject
success = pdf.Optimize("optimize",json,jsonOut)
If (success = False) Then
    Debug.WriteLine(pdf.LastErrorText)
    Exit Sub
End If


success = pdf.WriteFile("qa_output/watersense_incremental.pdf")
If (success = False) Then
    Debug.WriteLine(pdf.LastErrorText)
    Exit Sub
End If


Dim fac As New Chilkat.FileAccess
Debug.WriteLine("original: " & jsonOut.IntOf("fileSize"))
Debug.WriteLine("written:  " & fac.FileSize("qa_output/watersense_incremental.pdf"))

'  Everything the optimizer changed is in the appended part; the original bytes are the start of the
'  new file.  Any other combination of options -- dropOldRevisions, or generated object streams --
'  rewrites the document instead.
Debug.WriteLine("bytesSaved: " & jsonOut.IntOf("bytesSaved"))