Create PDF form XObjects

The example below shows how you can use GemBox.Pdf to create a form XObject, populate its content, and draw it at several locations on the same PDF page using a different fill color each time.

using GemBox.Pdf;
using GemBox.Pdf.Content;

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

        using (var document = new PdfDocument())
        {
            var form = new PdfForm(document, new PdfSize(200, 200));

            form.Content.BeginEdit();

            var textGroup = form.Content.Elements.AddGroup();

            // Add text with the default fill (fill will be inherited from outer PdfFormContent).
            using (var formattedText = new PdfFormattedText())
            {
                formattedText.Font = new PdfFont("Helvetica", 24);
                formattedText.Append("Hello world!");

                // Draw the formatted text at location (50, 150) from the bottom-left corner of the group/form.
                textGroup.DrawText(formattedText, new PdfPoint(50, 150));
            }

            // Add the same text with a black fill 50 points below the first text.
            var blackText = (PdfTextContent)textGroup.Elements.AddClone(textGroup.Elements.First);
            blackText.TextTransform = PdfMatrix.CreateTranslation(0, -50) * blackText.TextTransform;
            blackText.Format.Fill.Color = PdfColors.Black;

            var pathGroup = form.Content.Elements.AddGroup();

            // Add a rectangle path with the default fill (fill will be inherited from the outer PdfFormContent).
            var path = pathGroup.Elements.AddPath();
            path.AddRectangle(0, 50, 200, 40);
            path.Format.Fill.IsApplied = true;

            // Add the same path with a black fill 50 points below the first path.
            var blackPath = pathGroup.Elements.AddClone(path);
            blackPath.Subpaths.Transform(PdfMatrix.CreateTranslation(0, -50));
            blackPath.Format.Fill.Color = PdfColors.Black;

            form.Content.EndEdit();

            var page = document.Pages.Add();

            // Add the outer PdfFormContent with the default (black) fill.
            // Elements in the inner PdfForm that do not have a fill set, will have the default (black) fill.
            var contentGroup = page.Content.Elements.AddGroup();
            var formContent1 = contentGroup.Elements.AddForm(form);
            formContent1.Transform = PdfMatrix.CreateTranslation(100, 600);

            // Add the outer PdfFormContent with a green fill.
            // Elements in the inner PdfForm that do not have a fill set, will have a green fill.
            contentGroup = page.Content.Elements.AddGroup();
            var formContent2 = contentGroup.Elements.AddForm(form);
            formContent2.Transform = PdfMatrix.CreateTranslation(100, 350);
            formContent2.Format.Fill.Color = PdfColors.Green;

            // Add the outer PdfFormContent with a red fill.
            // Elements in the inner PdfForm that do not have a fill set, will have a red fill.
            contentGroup = page.Content.Elements.AddGroup();
            var formContent3 = contentGroup.Elements.AddClone(formContent1);
            formContent3.Transform = PdfMatrix.CreateTranslation(100, 100);
            formContent3.Format.Fill.Color = PdfColors.Red;

            document.Save("FormXObjects.%OutputFileType%");
        }
    }
}
Imports GemBox.Pdf
Imports GemBox.Pdf.Content

Module Program

    Sub Main()

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

        Using document = New PdfDocument()

            Dim form = New PdfForm(document, New PdfSize(200, 200))

            form.Content.BeginEdit()

            Dim textGroup = form.Content.Elements.AddGroup()

            ' Add text with the default fill (fill will be inherited from outer PdfFormContent).
            Using formattedText = New PdfFormattedText()

                formattedText.Font = New PdfFont("Helvetica", 24)
                formattedText.Append("Hello world!")

                ' Draw the formatted text at location (50, 150) from the bottom-left corner of the group/form.
                textGroup.DrawText(formattedText, New PdfPoint(50, 150))
            End Using

            ' Add the same text with a black fill 50 points below the first text.
            Dim blackText = CType(textGroup.Elements.AddClone(textGroup.Elements.First), PdfTextContent)
            blackText.TextTransform = PdfMatrix.CreateTranslation(0, -50) * blackText.TextTransform
            blackText.Format.Fill.Color = PdfColors.Black

            Dim pathGroup = form.Content.Elements.AddGroup()

            ' Add a rectangle path with the default fill (fill will be inherited from the outer PdfFormContent).
            Dim path = pathGroup.Elements.AddPath()
            path.AddRectangle(0, 50, 200, 40)
            path.Format.Fill.IsApplied = True

            ' Add the same path with a black fill 50 points below the first path.
            Dim blackPath = pathGroup.Elements.AddClone(path)
            blackPath.Subpaths.Transform(PdfMatrix.CreateTranslation(0, -50))
            blackPath.Format.Fill.Color = PdfColors.Black

            form.Content.EndEdit()

            Dim page = document.Pages.Add()

            ' Add the outer PdfFormContent with the default (black) fill.
            ' Elements in the inner PdfForm that do not have a fill set, will have the default (black) fill.
            Dim contentGroup = page.Content.Elements.AddGroup()
            Dim formContent1 = contentGroup.Elements.AddForm(form)
            formContent1.Transform = PdfMatrix.CreateTranslation(100, 600)

            ' Add the outer PdfFormContent with a green fill.
            ' Elements in the inner PdfForm that do not have a fill set, will have a green fill.
            contentGroup = page.Content.Elements.AddGroup()
            Dim formContent2 = contentGroup.Elements.AddForm(form)
            formContent2.Transform = PdfMatrix.CreateTranslation(100, 350)
            formContent2.Format.Fill.Color = PdfColors.Green

            ' Add the outer PdfFormContent with a red fill.
            ' Elements in the inner PdfForm that do not have a fill set, will have a red fill.
            contentGroup = page.Content.Elements.AddGroup()
            Dim formContent3 = contentGroup.Elements.AddClone(formContent1)
            formContent3.Transform = PdfMatrix.CreateTranslation(100, 100)
            formContent3.Format.Fill.Color = PdfColors.Red

            document.Save("FormXObjects.%OutputFileType%")
        End Using
    End Sub
End Module
PDF form XObject created with GemBox.Pdf C#/VB.NET library
Screenshot of PDF form XObject created with GemBox.Pdf library

You can draw a form XObject multiple times using the PdfFormContent element, either on several pages or at many locations on the same page. It’s also possible to produce the same result each time, subject only to the formatting of the PdfFormContent element.

See also


Next steps

GemBox.Pdf is a .NET component that enables developers to read, merge and split PDF files or execute low-level object manipulations from .NET applications in a simple and efficient way.

Download Buy