๐ฆ Compact Serialization
Reduce PDF file size by 20โ40% with cross-reference streams and object streams (PDF 1.5+).
C# โ Compact Serialization
var doc = new PdfDocument();
// 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;
var root = doc.EnableTaggedPdf();
var h1 = root.AddChild(StructureType.H1);
var page = doc.AddPage();
page.AddTaggedText(h1, "Compact Serialization",
72, 720, new PdfTextOptions {
Font = StandardFont.HelveticaBold,
FontSize = 20
});
var p = root.AddChild(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 |