Persits Software, Inc. Knowledge Base Articles

HOWTO: Fill out and stitch together multiple instances of a form

Problem Description

AspPDF is capable of stitching multiple documents together using the Doc.AppendDocument method. If a document being appended needs to be modified first (such as, fill out a form or template) it is necessary to save and then reopen this document, or the changes will not take effect during the appending phase.

This article describes a technique that should be used when modifying and stitching multiple templates together.

Solution

First of all, if a document being appended needs to be modified, it must first be saved, reopened, and only then appended. This operation can be performed entirely in memory via the SaveToMemory and OpenDocumentBinary to avoid dealing with temporary files.

Secondly, each document being appended must have its own separate Doc variable. Reusing a single Doc variable for all documents being appended may corrupt the output document, and is not allowed. The easiest solution is to use an array of PdfDocument objects.

The following code snippet demonstrates this technique:

Set Pdf = Server.CreateObject("Persits.Pdf")

Set objDocMain = Pdf.CreateDocument

Dim objDocs(10)
Dim i
For i = 0 To 10
  Set objDoc = Pdf.OpenDocument("c:\path\invoice.pdf")

  Set field = objDoc.Form.FindField("fieldname1")
  field.SetFieldValue "test value"&i, objDoc.fonts("Helvetica")

  ' set other fields
  
...

  ' save and reopen
  Set objDocs(i) = Pdf.OpenDocumentBinary(objDoc.SaveToMemory)

  ' Call AppendDocument to append
  objDocMain.AppendDocument objDocs(i)
Next

objDocMain.Save "c:\path\Out.pdf", False