Flatten PDF sample code

Posted by: rbroadwell on 2 June 2026, 4:09 pm EST

    • Post Options:
    • Link

    Posted 2 June 2026, 4:09 pm EST - Updated 2 June 2026, 4:14 pm EST

    I use version 7.2 of DsPdf.

    I used the exact sample code that can flatten a pdf that has form fields found here:

    https://developer.mescius.com/document-solutions/dot-net-pdf-api/demos/features/modify/flatten-form/code-cs

    My flattened output does not look quite right when compared to my source file.

    I have attached the source pdf (form-fields.pdf) and the flattened output pdf (form-fields-flattened.pdf)

    These are the things that I noticed:

    1. the source file has a custom page size: 9.33 x 11.83 but the flattened output has a different page size: 8.5 x 11
    2. the position of the “SalesDeskPhone” field is different in the output
    3. the position of the image is different in the output
    4. the size of the image is different in the output
    5. the image in the output has the “Headshot1” field name overlayed on the image (in the source file, the Headshot1 field was hidden and an image was placed over it, scaled to the size of the Headshot1 field)

    I am trying to find out if these issues are due to a bug in the sample code or would they be due to a bug in DsPdf?

    I’d appreciate any info regarding this since I now have a requirement to add a flattening option to my solution.

    Thank you.

  • Posted 2 June 2026, 4:10 pm EST

    don’t think source file made it the first time, trying again

  • Posted 2 June 2026, 4:12 pm EST

    third time should workform-fields.zip

  • Posted 2 June 2026, 4:13 pm EST

  • Posted 3 June 2026, 3:59 am EST

    Hi Robin,

    Thank you for reporting this behavior and for providing the sample files.

    After investigating the issue, we found that the behavior is not caused by the sample code itself but rather by the approach used for flattening. The sample creates a new PDF document and redraws each page into it, which can lead to differences in page size, field positioning, and image rendering for certain PDFs.

    To preserve the original page size and visual appearance, we recommend flattening the PDF using the following approach instead:

    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);
    }

    This approach preserves the original page dimensions and correctly renders the page content together with the form field appearances, avoiding the issues you observed with page size changes, field positioning, and image placement.

    You can further refer to the attached sample that demonstrates the usage of the above code snippet in DsPdf v7.2.0.

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

    Best regards,

    Chirag

    Attachment: FlatPDF.zip

  • Posted 3 June 2026, 9:05 am EST

    Your sample code fixed the issue, the output is perfect.

    Thank you very much!

Need extra support?

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

Learn More

Forum Channels