Concatenate pdf docs that use autofiller


#1

Hi,

I have two PDF documents I want to concatenate. The first doc I am loading up and using the autofiller method to populate the form elements. The second doc needs to be concatenated to the first.

We are getting this error when output the results to the http response:

Invalid pdf format:pdf head signature is not found!

Here is the code:

'Set the response

Response.Expires = 0

Response.Buffer = True

Response.ClearContent()

Response.AddHeader(“content-disposition”, “inline; filename=” + “output.pdf”)

Response.ContentType = “application/pdf”

'Create a data table to populate the form elements

Dim formData As New DataTable

Dim columns As DataColumnCollection = formData.Columns

columns.Add(“SubmitDate”, Type.GetType(“System.String”))

Dim dataRow As DataRow = formData.NewRow

dataRow(“SubmitDate”) = “President”

formData.Rows.Add(dataRow)

'PDF Doc 1 – this has the form elements

Dim appStream As FileStream = File.Open(Server.MapPath("~/enrollment/cms1011_application.pdf"), FileMode.Open, FileAccess.Read, FileShare.Read)

Dim appOutput As New MemoryStream

'PDF Doc 2

Dim appStream1 As FileStream = File.Open(Server.MapPath("~/enrollment/cms1011_attachment1.pdf"), FileMode.Open, FileAccess.Read, FileShare.Read)

'Fill the form elements in PDF Doc 1

Dim autoFiller As New AutoFiller

autoFiller.InputStream = appStream

autoFiller.OutputStream = appOutput

autoFiller.ImportDataTable(formData)

autoFiller.Save()

'Concatenate the two documents

Dim fileEditor As PdfFileEditor = New PdfFileEditor

Dim finalStream As MemoryStream = New MemoryStream

fileEditor.Concatenate(appOutput, appStream1, finalStream)

'Write out everything to the HTTP Response

Dim outBuffer() As Byte = finalStream.GetBuffer

Response.BinaryWrite(outBuffer)

finalStream.Close()

appStream.Close()

appOutput.Close()

appStream1.Close()

Response.End()

------

Any help would be appreciated?

Bryon Brewer
Order # 050805124251


#2

Dear Bryon,

Thanks for considering Aspose.Pdf.Kit.

1) Before you use fileEditor.Concatenate(), you shuld set the input stream’s position as 0. In your program, it is just like this:
appOutput.Position = 0
appStream1.Position = 0

fileEditor.Concatenate(appOutput, appStream1, finalStream)

2) The AutoFiller class is just used for batch filling fields, such as getting batch data from database and filling the fields and creating a merged pdf or multiple pdfs. Detailed example is in the demo. If you only want to fill one pdf, the Form class is more suitable and simple. The example is also in the demo code and wiki.

Any more question is welcome.
Best regards.