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

'' 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
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Using fs = File.OpenRead(Path.Combine("Resources", "PDFs", "form-fields.pdf"))
            '' Load the filled PDF form into a temp document:
            Dim srcDoc = New GcPdfDocument()
            srcDoc.Load(fs)
            '' Draw all pages And annotation of the source PDF into the New one:
            For Each srcPage In srcDoc.Pages
                Dim page = doc.Pages.Add()
                Dim fxo = New FormXObject(doc, srcPage)
                page.Graphics.DrawForm(fxo, page.Bounds, Nothing, ImageAlign.Default)
                '' This method draws all annotations on the page including form field widgets:
                srcPage.DrawAnnotations(page.Graphics, page.Bounds)
            Next
            '' Done:
            doc.Save(stream)
            Return doc.Pages.Count
        End Using
    End Function
End Class