Click or drag to resize

Content Streams and Resources

A PDF content stream contains a sequence of instructions composed of Objects (operands) and keywords (operators) describing the appearance of a page or other graphical entity.

The following section gives more information about the current state of PDF Content Streams and Resources implementation in GemBox.Pdf.

Content Streams and Resources

GemBox.Pdf currently does not provide any high-level interface for interacting with PDF Content Streams and Resources.

Note Note

Implementation of a high-level interface for PDF Content Streams and Resources is currently in progress and is expected to be released with the next major version of GemBox.Pdf.

Still, Content Streams and Resources can be used in GemBox.Pdf by using PDF Objects as in the following example.

// Specify content stream's content as a sequence of content stream operands and operators.
var content = new StringBuilder();
content.AppendLine("BT");                // Begin a text object.          
content.AppendLine("/F1 12 Tf");         // Set the font and font size to use, installing them as parameters in the text state. In this case, the font resource identified by the name F1 specifies the font externally known as Helvetica.
content.AppendLine("70 760 Td");         // Specify a starting position on the page, setting parameters in the text object.
content.AppendLine("(Hello world!) Tj"); // Paint the glyphs for a string of characters at that position.
content.AppendLine("ET");                // End the text object.

// Create content stream and write content to it.
var contentStream = PdfStream.Create();
contentStream.Filters.AddFilter(PdfFilterType.FlateDecode);
using (var stream = contentStream.Open(PdfStreamDataMode.Write, PdfStreamDataState.Decoded))
{
    var contentBytes = PdfEncoding.Byte.GetBytes(content.ToString());
    stream.Write(contentBytes, 0, contentBytes.Length);
}

// Create font dictionary for Standard Type 1 'Helvetica' font.
var font = PdfDictionary.Create();
font[PdfName.Create("Type")] = PdfName.Create("Font");
font[PdfName.Create("Subtype")] = PdfName.Create("Type1");
font[PdfName.Create("BaseFont")] = PdfName.Create("Helvetica");

// Add font dictionary to resources.
var fontResources = PdfDictionary.Create();
fontResources[PdfName.Create("F1")] = PdfIndirectObject.Create(font);
var resources = PdfDictionary.Create();
resources[PdfName.Create("Font")] = fontResources;

// Create document.
using (var document = new PdfDocument())
{
    // Create new empty A4 page.
    var page = document.Pages.Add();

    // Set contents and resources of a page.
    var pageDictionary = page.GetDictionary();
    pageDictionary[PdfName.Create("Contents")] = PdfIndirectObject.Create(contentStream);
    pageDictionary[PdfName.Create("Resources")] = resources;

    // Save the document as a PDF file.
    document.Save("Document.pdf");
}
// The PDF file is closed after 'using' block.
See Also