FlattenForm.cs
//
// This code is part of Document Solutions for PDF demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Pdf.AcroForms;
using GrapeCity.Documents.Pdf.Graphics;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Text;

namespace DsPdfWeb.Demos
{
    // This sample shows how to easily 'flatten' an AcroForm PDF with data -
    // i.e. convert that form into a non-form PDF that looks like the
    // original filled form.
    // The original filled form loaded by this sample is generated by FormFields.
    public class FlattenForm
    {
        public int CreatePDF(Stream stream)
        {
            using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "form-fields.pdf"));
            // Load the filled PDF form into a temp document:
            var srcDoc = new GcPdfDocument();
            srcDoc.Load(fs);
            // Draw all pages and annotation of the source PDF into a new PDF:
            var doc = new GcPdfDocument();
            foreach (var srcPage in srcDoc.Pages)
            {
                var page = doc.Pages.Add();
                var fxo = new FormXObject(doc, srcPage);
                page.Graphics.DrawForm(fxo, page.Bounds, null, ImageAlign.Default);
                // This method draws all annotations on the page including form field widgets:
                srcPage.DrawAnnotations(page.Graphics, page.Bounds);
            }
            // Done:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}