Flatten PDF sample code (Round 2)

Posted by: rbroadwell on 22 September 2026, 11:45 am EST

  • Posted 22 September 2026, 11:45 am EST

    Hello,

    We use version 7.2 of DsPdf.

    We are flattening our pdf forms using the solution that was provided to us in an earlier post:

    https://developer.mescius.com/forums/documents-pdf/flatten-pdf-sample-code#86318

    This is the code being used to flatten the pdf:

    var doc = new GcPdfDocument();

    foreach (var srcPage in srcDoc.Pages)

    {

    var page = doc.Pages.Add();

    page.Size = srcPage.Size;

    var fxo = new FormXObject(doc, srcPage);

    srcPage.DrawAnnotations(page.Graphics, page.Bounds);

    page.Graphics.DrawForm(fxo, page.Bounds, null, ImageAlign.Default);

    }

    Attached is a file called ‘Test Form.pdf’ which contains form fields.

    After flattening, the form field values no longer appear on the flattened version of the file.

    Can you tell us how to make the fields appear on the flattened file (sample output is also attached.)

    Thank you.Test Files.zip

  • Posted 23 September 2026, 1:27 am EST

    Hi Robin,

    Thank you for reaching out.

    The field values are missing because the page annotations and form widgets are being drawn before the page content. As per the official approach, it is recommended to draw the page content first, then draw the annotations and widgets:

    GcPdfDocument doc = new();
    foreach (Page srcPage in srcDoc.Pages)
    {
        Page page = doc.Pages.Add();
        page.Size = srcPage.Size;
        FormXObject fxo = new(doc, srcPage);
        GcPdfGraphics pdfGraphics = page.Graphics;
        pdfGraphics.DrawForm(fxo, page.Bounds, null, ImageAlign.Default);
        srcPage.DrawAnnotations(pdfGraphics, page.Bounds);
    }

    However, one important caveat applies to your use case. Your earlier PDF has an image covering the hidden Headshot1 field. Drawing the annotations last can place that field’s appearance over the image. Therefore, it is required to redraw the image on the destination page, which adds additional image-drawing overhead. Please refer to the code snippet below that demonstrates the approach:

    GcPdfDocument doc = new();
    foreach (Page srcPage in srcDoc.Pages)
    {
        Page page = doc.Pages.Add();
        page.Size = srcPage.Size;
        FormXObject fxo = new(doc, srcPage);
        GcPdfGraphics pdfGraphics = page.Graphics;
        pdfGraphics.DrawForm(fxo, page.Bounds, null, ImageAlign.Default);
        srcPage.DrawAnnotations(pdfGraphics, page.Bounds);
        foreach (PdfImageInfo imgInfo in srcPage.GetImages())
        {
            foreach (PdfImageLocation imgLocation in imgInfo.Locations)
    	{
    	    pdfGraphics.DrawImage(imgInfo.Image, imgLocation.PageBounds.ToRect(), null, ImageAlign.ScaleImage);
    	}
        }
    }

    You can further refer to the attached sample below that demonstrates the above approach and generates the flattened PDF as per your requirements.

    Please let us know if you encounter any further issues or require additional assistance.

    Best regards,

    Chirag

    Attachments: FlatPDF.zip

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels