# Copyright (c) 2019 ActivePDF, Inc. # ActivePDF Toolkit 2018 # Example generated 12/08/19 require 'win32ole' # Get current path strPath = File.expand_path(File.dirname(__FILE__)) + "\\" # Instantiate Object oTK = WIN32OLE.new("APToolkit.Object") # This example will take one PDF form and fill it out twice # resulting in a 2 page PDF containing two filled forms # The new PDF will have renamed fields so they do not conflict # Unused fields will be flattened (removed) # Create the new PDF file intOpenOutputFile = oTK.OpenOutputFile(strPath + 'new.pdf') if intOpenOutputFile != 0 puts "Error in OpenOutputFile: #{intOpenOutputFile}" end # Open the template PDF intOpenInputFile = oTK.OpenInputFile(strPath + 'form.pdf') if intOpenInputFile != 0 puts "Error in OpenInputFile: #{intOpenInputFile}" end # Set how you want form field data formatting to be handled # By default the field will remain the same (1) # You can have the field formatting be ignored (0) oTK.DoFormFormatting = 1 # As we will be using the same PDF form for multiple pages we do not want # the field names to conflict as different data will be in same named fields # Using FormNumbering will rename the fields so they do not conflict # field 'name' would become 'name__1', 'name__2', etc # 0 = False, 1 = True oTK.FormNumbering = 1 # Populate form fields for the first page with data # Refer to the documentation for possible flag options oTK.SetFormFieldData('name', 'John Doe', 1) oTK.SetFormFieldData('date', '03-27-1984', -997) oTK.SetFormFieldData('amount', '4231.34', -997) # Copy the first page (with field data) to the new pdf intCopyForm = oTK.CopyForm(0, 0) if intCopyForm != 1 puts "Error in CopyForm: #{intCopyForm}" end # Use ResetFormFields to clear the form data we previously used oTK.ResetFormFields() # Populate form fields for the second page with data oTK.SetFormFieldData('name', 'Jane Doe', 1) oTK.SetFormFieldData('date', '07/13/1988', -997) oTK.SetFormFieldData('amount', '1567.12', -997) # Set whether to flatten all other fields not touched by SetFormFieldData oTK.FlattenRemainingFormFields = 1 # Copy the second page (with field data) to the new pdf intCopyForm = oTK.CopyForm(0, 0) if intCopyForm != 1 puts "Error in CopyForm: #{intCopyForm}" end # Close the new file to complete PDF creation oTK.CloseOutputFile() # Release Object oTK = '' # Process Complete puts "Done!"