Create PDF marked content in C# and VB.NET

Marked content is PDF content that is either adjacent to (if the mark type is Point) or surrounded by (if the mark type is Start and End) PdfContentMark elements.

The PdfContentMark element specifies a tag that indicates the role or significance of the marked content, and, optionally, the properties of the marked content.

The following example shows how to mark geometrical content that 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.