PdfFormField object | PdfDocument Object | Working with Acro Forms | Programming with Merger for COM/ActiveX

The easiest way to flatten an Acro Form is to mark it as read only and use Security to prevent the modification of the form field values.

This example shows how to mark form fields as read only and encrypt the document to prevent modification.

[VBScript]
    Dim MyDocument
    Dim MgOptions
    Set MgOptions = WScript.CreateObject( "DynamicPDF.MergeOptions" )
    MgOptions.MergeFormFields = True
    Set MyDocument = WScript.CreateObject( "DynamicPDF.Document" )
    MyDir = CreateObject( "Scripting.FileSystemObject" ).GetFolder( "." ).Path
    ' Current Directory
    MyDocument.LoadPdf MyDir & "\PDFs\fw9AcroForm_03.pdf", , , MgOptions 
    ' Set the field values
    MyDocument.Form.Fields.Item( "f1-1" ).Value = "My Text"  ' TextBox field
    ' Make the form read only
    MyDocument.Form.IsReadOnly = True
    ' Encrypt and Secure the document
    Dim MyDocSecurity
    Set MyDocSecurity = MyDocument.SetHighSecurity( "owner", "pass" )
    MyDocSecurity.AllowEdit = False
    ' Save the PDF
    MyDocument.DrawToFile("MyDocument.pdf" )
    Set MyDocument = Nothing

Replace Acro Form Fields with Text

If you would prefer to remove the form fields entirely and place text at their coordinates, this can be done using the CreateLabel method of the PdfFormField class. The coordinates of the form field can also be read if other PageElements are required.

[VBScript]
    Option Explicit
    Dim MyDocument 
    Set MyDocument = Server.CreateObject( "DynamicPDF.Document" )
    Dim MyPdfDocument 
    Set MyPdfDocument = Server.CreateObject( "DynamicPDF.PdfDocument" )
    MyPdfDocument.LoadPdf Server.MapPath( "./PDFs/fw9AcroForm_03.pdf" ), ""
    Dim MyMergeOptions
    Set MyMergeOptions = Server.CreateObject( "DynamicPDF.MergeOptions" )
    MyMergeOptions.MergeFormFields = False 
    MyDocument.AppendPdf MyPdfDocument, , , MyMergeOptions
    Dim MyPage
    Set MyPage = MyDocument.GetPage( 1 )
    MyPdfDocument.Form.Fields.Item( "f1-1" ).CreateLabel MyPage, "Acme Corp.", DPDF_Font_Helvetica, 12, DPDF_TextAlign_Left
    MyPdfDocument.Form.Fields.Item("c1-2").CreateLabel page, "3", DPDF_Font_ZapfDingbats, 12, DPDF_TextAlign_Left, 2.5, -2.5
    MyPdfDocument.Form.Fields.Item("f1-4").CreateLabel page, "123 Main Street", DPDF_Font_Courier, 12
    MyDocument.DrawToWeb
    Set MyDocument = Nothing

If the page that the original form field was located on is not known ahead of time it can be read using the form fields GetOriginalPageNumber method. Note that this information is not required by the PDF spec and the method will return a value of -1 if this information is not provided in the import document.

[VBScript]
    Option Explicit
    Dim MyDocument 
    Set MyDocument = Server.CreateObject( "DynamicPDF.Document" )
    Dim MyPdfDocument 
    Set MyPdfDocument = Server.CreateObject( "DynamicPDF.PdfDocument" )
    MyPdfDocument.LoadPdf Server.MapPath( "./PDFs/fw9AcroForm_03.pdf" ), ""
    Dim MyMergeOptions
    Set MyMergeOptions = Server.CreateObject( "DynamicPDF.MergeOptions" )
    MyMergeOptions.MergeFormFields = False 
    Dim MyPageArr
    Set MyPageArr = MyDocument.AppendPdf(MyPdfDocument, , , MyMergeOptions)
    Dim MyPage
    Set MyPage = MyPageArr( 1 )
    Dim MyF1_1 
    Set MyF1_1 = MyPdfDocument.Form.Fields.Item("f1-1")
    Dim MyC1_2 
    Set MyC1_2 = MyPdfDocument.Form.Fields.Item("c1-2")
    Dim MyF1_4
    Set MyF1_4 = MyPdfDocument.Form.Fields.Item("f1-4")
    Dim MyF1_5 
    Set MyF1_5 = MyPdfDocument.Form.Fields.Item("f1-5")
    ' Add the labels
    MyF1_1.CreateLabel MyPageArr(MyF1_1.GetOriginalPageNumber() - 1), "Acme Corp.", DPDF_Font_Courier, 12
    MyC1_2.CreateLabel MyPageArr(MyC1_2.GetOriginalPageNumber() - 1), "3", DPDF_Font_ZapfDingbats, 12, , 2.5, -2.5
    MyF1_4.CreateLabel MyPageArr(MyF1_4.GetOriginalPageNumber() - 1), "123 Main Street", DPDF_Font_Courier, 12
    MyF1_5.CreateLabel MyPageArr(MyF1_5.GetOriginalPageNumber() - 1), "Anytown, DC  12345", DPDF_Font_Courier, 12
    MyDocument.DrawToWeb
    Set MyDocument = Nothing

See Also 

PdfFormField object | PdfDocument Object | Working with Acro Forms | Programming with Merger for COM/ActiveX