PowerShell Requires Chilkat v11.7.0+
PowerShell
Where a PDF's Bytes Go: the Optimize Audit
See more PDF Optimization Examples
Demonstrates the "audit" operation, which measures the loaded PDF and changes nothing. The report says how large the file is, which PDF version it claims, whether it claims to be PDF/A, where its bytes go ("usage", as stored in the file, so compressed sizes), and how many bytes each lossless option would be expected to save ("estimatedSavings"). Use it to decide whether optimizing is worth the trouble, and which options matter for this file.Chilkat PowerShell Downloads
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"
$success = $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 grand_canyon_hiking_guide.pdf
$pdf = New-Object Chilkat.Pdf
$success = $pdf.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf")
if ($success -eq $false) {
$($pdf.LastErrorText)
exit
}
# "audit" takes the same options as "optimize", so an empty JSON means the default options.
$json = New-Object Chilkat.JsonObject
$jsonOut = New-Object Chilkat.JsonObject
$success = $pdf.Optimize("audit",$json,$jsonOut)
if ($success -eq $false) {
$($pdf.LastErrorText)
exit
}
$("fileSize: " + $jsonOut.IntOf("fileSize"))
$("pdfVersion: " + $jsonOut.StringOf("pdfVersion"))
$("pdfa: " + $jsonOut.StringOf("pdfa"))
# Where the bytes go. Every category is present even when it is 0, and the sizes are the stored
# (compressed) sizes, so they add up to fileSize. "overhead" is what is left: the cross-reference
# tables, the object headers and the white space between objects.
$("images: " + $jsonOut.IntOf("usage.images.bytes") + " in " + $jsonOut.IntOf("usage.images.count") + " objects")
$("fonts: " + $jsonOut.IntOf("usage.fonts.bytes") + " in " + $jsonOut.IntOf("usage.fonts.count") + " objects")
$("pageContent: " + $jsonOut.IntOf("usage.pageContent.bytes"))
$("formXObjects: " + $jsonOut.IntOf("usage.formXObjects.bytes"))
$("annotations: " + $jsonOut.IntOf("usage.annotations.bytes"))
$("structureTree: " + $jsonOut.IntOf("usage.structureTree.bytes"))
$("metadata: " + $jsonOut.IntOf("usage.metadata.bytes"))
$("embeddedFiles: " + $jsonOut.IntOf("usage.embeddedFiles.bytes"))
$("oldRevisions: " + $jsonOut.IntOf("usage.oldRevisions.bytes"))
$("overhead: " + $jsonOut.IntOf("usage.overhead.bytes"))
# What each option is expected to save. An option that would save nothing is left out.
$("estimated total: " + $jsonOut.IntOf("estimatedSavings.total"))
$est = New-Object Chilkat.JsonObject
$success = $jsonOut.ObjectOf2("estimatedSavings",$est)
if ($success -eq $false) {
$($jsonOut.LastErrorText)
exit
}
$i = 0
$numEst = $est.Size
while ($i -lt $numEst) {
$optionName = $est.NameAt($i)
$(" " + $optionName + ": " + $est.IntOf($optionName))
$i = $i + 1
}