Create PDF marked content in C# and VB.NET

The marked content feature helps to identify a portion of a PDF content stream and assigns how it will be presented in a reader application. We mark content in PDF with tags, which provide a logical composition of the PDF document and control how the content will be displayed to the user. Each of these tags will contain the role and properties of the content.

Moreover, adding PDF tags helps to organize the PDF composition, such as the visual elements (headings, images, tables, etc.). Having a tagged PDF is also a best practice to make documents accessible to people with vision impairment, and for reflowing the content through different devices.

There are two mark types:

  • Point, in which a specific point in a page content stream may be marked with a simple tag with the MP operator.
  • And Begin-End type, which is a collection of drawing operators surrounded by PdfContentMark elements.

With the following example, you can learn how to mark geometrical content, which is a visual representation of specific Unicode content.

PDF marked content created with GemBox.Pdf C#/VB.NET library
Screenshot of PDF marked content created with GemBox.Pdf library
using GemBox.Pdf;
using GemBox.Pdf.Content.Marked;
using GemBox.Pdf.Objects;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        using (var document = new PdfDocument())
        {
            var page = document.Pages.Add();

            // Surround the path with the marked content start and marked content end elements.
            var markStart = page.Content.Elements.AddMarkStart(new PdfContentMarkTag(PdfContentMarkTagRole.Span));
            
            var markedProperties = markStart.GetEditableProperties().GetDictionary();

            // Set replacement text for a path, as specified in http://www.adobe.com/content/dam/acom/en/devnet/pdf/PDF32000_2008.pdf#page=623
            markedProperties[PdfName.Create("ActualText")] = PdfString.Create("H");

            // Add the path that is a visual representation of the letter 'H'.
            var path = page.Content.Elements.AddPath();
            path.
                BeginSubpath(100, 600).LineTo(100, 800).
                BeginSubpath(100, 700).LineTo(200, 700).
                BeginSubpath(200, 600).LineTo(200, 800);
            var format = path.Format;
            format.Stroke.IsApplied = true;
            format.Stroke.Width = 10;

            page.Content.Elements.AddMarkEnd();

            document.Save("MarkedContent.pdf");
        }
    }
}
Imports GemBox.Pdf
Imports GemBox.Pdf.Content.Marked
Imports GemBox.Pdf.Objects

Module Program

    Sub Main()

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

        Using document = New PdfDocument()

            Dim page = document.Pages.Add()

            ' Surround the path with the marked content start and marked content end elements.
            Dim markStart = page.Content.Elements.AddMarkStart(New PdfContentMarkTag(PdfContentMarkTagRole.Span))

            Dim markedProperties = markStart.GetEditableProperties().GetDictionary()

            ' Set replacement text for a path, as specified in http://www.adobe.com/content/dam/acom/en/devnet/pdf/PDF32000_2008.pdf#page=623
            markedProperties(PdfName.Create("ActualText")) = PdfString.Create("H")

            ' Add the path that is a visual representation of the letter 'H'.
            Dim path = page.Content.Elements.AddPath()
            path.
                BeginSubpath(100, 600).LineTo(100, 800).
                BeginSubpath(100, 700).LineTo(200, 700).
                BeginSubpath(200, 600).LineTo(200, 800)
            Dim format = path.Format
            format.Stroke.IsApplied = True
            format.Stroke.Width = 10

            page.Content.Elements.AddMarkEnd()

            document.Save("MarkedContent.pdf")
        End Using
    End Sub
End Module

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Pdf yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.