Content Stream Operators

Content Stream Operators

This guide shows how to access and manipulate PDF content stream operators using ContentStreamParser and ContentStreamBuilder in Aspose.PDF FOSS for Java. You will learn to read raw page content bytes and use the parser to inspect drawing commands.

Content Streams

PDF page content is encoded as a sequence of operators in a content stream. Each Page provides access to its content operators via the getContents() property.

ContentStreamParser

ContentStreamParser parses the raw content stream bytes into operator objects, enabling programmatic inspection of page drawing commands. Pass the raw bytes from page.getContents().toByteArray() to the parser constructor:

try (Document doc = new Document("input.pdf")) {
    PageCollection pages = doc.getPages();
    Page page = pages.get(1);
    byte[] contentBytes = page.getContents().toByteArray();
    ContentStreamParser parser = new ContentStreamParser(contentBytes);
    // Iterate over parsed operators
}

ContentStreamBuilder

ContentStreamBuilder creates new content streams for embedding in a Page. Use ContentStreamBuilder when constructing page content programmatically rather than modifying an existing stream. After building the stream, assign it back to the page and save the document.

Operator Reference

PDF content stream operators are defined in ISO 32000-1:2008, Table A.1. Common operators include text operators (BT, ET, Tf, Tj), graphics state operators (q, Q, cm), and path operators (m, l, h, f, S).

Content stream operators follow the PDF specification (ISO 32000-1:2008, §8). Use ContentStreamParser to iterate over the operators in an existing stream.

Accessing Raw Page Content

To read the raw bytes of a page content stream, call page.getContents().toByteArray(). The bytes can then be inspected, decoded, or passed to ContentStreamParser:

try (Document doc = new Document("input.pdf")) {
    PageCollection pages = doc.getPages();
    Page page = pages.get(1);
    byte[] contentBytes = page.getContents().toByteArray();
    System.out.println("Content stream size: " + contentBytes.length + " bytes");
}

See Also

 English