GemBox.Pdf is a .NET component that enables developers to read, write and update Portable Document Format (PDF) files from .NET applications in a simple and efficient way.

Create content stream using PDF objects in C# and VB.NET

With GemBox.Pdf, you can use basic PDF objects for currently unsupported PDF features in your C# or VB.NET application.

For more information about PDF content streams in GemBox.Pdf, see Content Streams and Resources help page.
For more information about basic PDF objects, see Objects help page.

The following example demonstrates how to create a content stream with a simple text content.

Screenshot
PDF Library for C# / VB.NET Screenshot

See the full code below, use Run Example to execute.

using System;
using System.Globalization;
using System.Text;
using GemBox.Pdf;
using GemBox.Pdf.Filters;
using GemBox.Pdf.Objects;
using GemBox.Pdf.Text;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        PdfDocument document = new PdfDocument();

        // Specify content stream's content as a sequence of content stream operands and operators.
        var content = new StringBuilder();
        // Begin a text object.
        content.AppendLine("BT");
        // 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("/F1 12 Tf");
        // Specify a starting position on the page, setting parameters in the text object.
        content.AppendLine("70 760 Td");
        // Paint the glyphs for a string of characters at that position.
        content.AppendLine("(GemBox.Pdf) Tj");
        // End the text object.
        content.AppendLine("ET");

        // 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 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;

        document.Save("Content Stream.pdf");
        document.Close();
    }
}
Imports System.Globalization
Imports System.IO
Imports System.Text
Imports GemBox.Pdf
Imports GemBox.Pdf.Filters
Imports GemBox.Pdf.Objects
Imports GemBox.Pdf.Text

Module Samples

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document As PdfDocument = New PdfDocument()

        ' Specify content stream's content as a sequence of content stream operands and operators.
        Dim content = New StringBuilder()
        ' Begin a text object.
        content.AppendLine("BT")
        ' 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("/F1 12 Tf")
        ' Specify a starting position on the page, setting parameters in the text object.
        content.AppendLine("70 760 Td")
        ' Paint the glyphs for a string of characters at that position.
        content.AppendLine("(GemBox.Pdf) Tj")
        ' End the text object.
        content.AppendLine("ET")

        ' Create content stream and write content to it.
        Dim contentStream = PdfStream.Create()
        contentStream.Filters.AddFilter(PdfFilterType.FlateDecode)

        Using stream = contentStream.Open(PdfStreamDataMode.Write, PdfStreamDataState.Decoded)
            Dim contentBytes = PdfEncoding.Byte.GetBytes(content.ToString())
            stream.Write(contentBytes, 0, contentBytes.Length)
        End Using

        ' Create font dictionary for Standard Type 1 'Helvetica' font.
        Dim 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.
        Dim fontResources = PdfDictionary.Create()
        fontResources(PdfName.Create("F1")) = PdfIndirectObject.Create(font)

        Dim resources = PdfDictionary.Create()
        resources(PdfName.Create("Font")) = fontResources

        ' Create new empty A4 page.
        Dim page = document.Pages.Add()

        ' Set contents and resources of a page.
        Dim pageDictionary = page.GetDictionary()
        pageDictionary(PdfName.Create("Contents")) = PdfIndirectObject.Create(contentStream)
        pageDictionary(PdfName.Create("Resources")) = resources

        document.Save("Content Stream.pdf")
        document.Close()

    End Sub

End Module

Check next sample.