๐ฆ Compact Serialization
Reduce PDF file size by 20โ40% with cross-reference streams and object streams (PDF 1.5+).
Compact Serialization
using ObviousPDF;
using ObviousPDF.Accessibility;
using ObviousPDF.Fonts;
var doc = new PdfDocument();
doc.Info.Title = "Compact Serialization";
doc.Language = "en-US";
doc.DisplayDocTitle = true;
doc.UseCrossReferenceStreams = true;
doc.UseObjectStreams = true;
var root = doc.EnableTaggedPdf();
var page = doc.AddPage();
var h1 = root.AddChild(StructureType.H1);
page.AddTaggedText(h1, "Compact Serialization", 72, 740,
new PdfTextOptions { Font = StandardFont.HelveticaBold, FontSize = 20 });
var p = root.AddChild(StructureType.P);
page.AddTaggedTextBlock(p, new[]
{
"This PDF uses cross-reference streams and object streams",
"for maximum file size reduction.",
"",
"Cross-reference streams: Binary xref in FlateDecode stream",
"Object streams: Multiple objects in one compressed stream",
"",
"These features are recommended for modern PDFs (PDF 1.5+)."
}, 72, 700, new PdfTextOptions { FontSize = 12, Leading = 18 });
doc.Save("compact_serialization.pdf");
Imports ObviousPDF
Imports ObviousPDF.Accessibility
Imports ObviousPDF.Fonts
Dim doc As New PdfDocument()
doc.Info.Title = "Compact Serialization"
doc.Language = "en-US"
doc.DisplayDocTitle = True
doc.UseCrossReferenceStreams = True
doc.UseObjectStreams = True
Dim root = doc.EnableTaggedPdf()
Dim page = doc.AddPage()
Dim h1 = root.AddChild(StructureType.H1)
page.AddTaggedText(h1, "Compact Serialization", 72, 740,
New PdfTextOptions With { .Font = StandardFont.HelveticaBold, .FontSize = 20 })
Dim p = root.AddChild(StructureType.P)
page.AddTaggedTextBlock(p, New String() {
"This PDF uses cross-reference streams and object streams",
"for maximum file size reduction.",
"",
"Cross-reference streams: Binary xref in FlateDecode stream",
"Object streams: Multiple objects in one compressed stream",
"",
"These features are recommended for modern PDFs (PDF 1.5+)."
}, 72, 700, New PdfTextOptions With { .FontSize = 12, .Leading = 18 })
doc.Save("compact_serialization.pdf")
open ObviousPDF
open ObviousPDF.Accessibility
open ObviousPDF.Fonts
let doc = PdfDocument()
doc.Info.Title <- "Compact Serialization"
doc.Language <- "en-US"
doc.DisplayDocTitle <- true
doc.UseCrossReferenceStreams <- true
doc.UseObjectStreams <- true
let root = doc.EnableTaggedPdf()
let page = doc.AddPage()
let h1 = root.AddChild(StructureType.H1)
page.AddTaggedText(h1, "Compact Serialization", 72.0, 740.0,
PdfTextOptions(Font = StandardFont.HelveticaBold, FontSize = 20.0))
let p = root.AddChild(StructureType.P)
page.AddTaggedTextBlock(p, [|
"This PDF uses cross-reference streams and object streams"
"for maximum file size reduction."
""
"Cross-reference streams: Binary xref in FlateDecode stream"
"Object streams: Multiple objects in one compressed stream"
""
"These features are recommended for modern PDFs (PDF 1.5+)."
|], 72.0, 700.0, PdfTextOptions(FontSize = 12.0, Leading = 18.0))
doc.Save("compact_serialization.pdf")
Add-Type -Path "ObviousPDF.dll"
$doc = [ObviousPDF.PdfDocument]::new()
# Enable cross-reference streams (PDF 1.5+)
# Replaces the ASCII cross-reference table with
# a compressed binary stream โ smaller files.
$doc.UseCrossReferenceStreams = $true
# Enable object streams (PDF 1.5+)
# Packs multiple small objects into a single
# compressed stream โ further size reduction.
$doc.UseObjectStreams = $true
# Build the document as usual...
$doc.Info.Title = "Compact PDF"
$doc.Language = "en-US"
$doc.DisplayDocTitle = $true
$root = $doc.EnableTaggedPdf()
$h1 = $root.AddChild([ObviousPDF.Accessibility.StructureType]::H1)
$page = $doc.AddPage()
$page.AddTaggedText($h1, "Compact Serialization",
72, 720, [ObviousPDF.PdfTextOptions]@{
Font = [ObviousPDF.Fonts.StandardFont]::HelveticaBold
FontSize = 20
})
$p = $root.AddChild([ObviousPDF.Accessibility.StructureType]::P)
$page.AddTaggedText($p,
("This PDF uses cross-reference streams " +
"and object streams for smaller file size."),
72, 690)
$doc.Save("compact.pdf")
# File size is 20-40% smaller than default!
You should see a bold "Compact Serialization" heading and a seven-line paragraph explaining cross-reference streams and object streams. The PDF itself is notably smaller than an equivalent document without these features โ check file size in Explorer to confirm (typically ~2.8 KB versus ~4.2 KB for the same content).
File: 18_compact_serialization.pdf
Serialization Options
| Property | Default | Effect |
|---|---|---|
UseCrossReferenceStreams | false | Binary xref table โ compressed stream |
UseObjectStreams | false | Packs small objects into compressed streams |