How to Work with Content Operations in .NET

How to Work with Content Operations in .NET

How to Work with Content Operations in .NET

This guide shows how to inspect and build PDF content streams using the operator-level API.


Prerequisites

RequirementDetail
Runtime.NET 8.0 or later
Packagedotnet add package Aspose.Pdf.Foss

Read content-stream operators

Every PDF page stores its visual content as a sequence of operators in a content stream. Iterate Page.Contents to inspect each operator, which is useful for debugging layout issues or analyzing existing page structure.

using var doc = Document.Open(pdfBytes);
foreach (var op in doc.Pages[1].Contents)
{
    Console.WriteLine(op.ToString());
}

Build new content

Use ContentStreamBuilder to construct content-stream instructions programmatically. The builder provides a fluent API for graphics state manipulation including fill colors, transformations, and text placement.

var builder = new ContentStreamBuilder();
builder.SaveState();
builder.SetFillColor(1.0, 0.0, 0.0);
builder.SetMatrix(1, 0, 0, 1, 72, 700);
builder.RestoreState();

Key Classes

ClassPurpose
OperatorCollectionPage content-stream operators
ContentStreamBuilderFluent content builder
GraphicsStateTransformation and color state
SetColorFill color operator
ShowTextText rendering operator

See Also