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
| Requirement | Detail |
|---|---|
| Runtime | .NET 8.0 or later |
| Package | dotnet 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
| Class | Purpose |
|---|---|
OperatorCollection | Page content-stream operators |
ContentStreamBuilder | Fluent content builder |
GraphicsState | Transformation and color state |
SetColor | Fill color operator |
ShowText | Text rendering operator |