FlattenForm.cs
//
// This code is part of GrapeCity Documents for PDF samples.
// Copyright (c) GrapeCity, 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 GcPdfWeb.Samples
{
    // 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)
        {
            GcPdfDocument doc = new GcPdfDocument();
            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 the new one:
                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;
            }
        }
    }
}