Free Support Forum - aspose.com

Problem(s) concatenating PDFs with PDF.Kit

Greetings:

We are evaluating your PDF.Kit product for possible purchase by our organization.

We have a need to concatenate / append multiple PDF files into a single output PDF for delivery via an ASP.NET application.

During our evaluation, we have encountered multiple problems with the PDF.Kit and respectfully request your assistance in resolving these issues prior to purchasing your product.

As stated, we are trying to concatentate / append from 1 to x PDF files into a single output file. We have attempted to use the pdfeditor.concatenate and the pdfeditor.append methods to achieve this goal, without success. We CAN concatenate / append 2 PDF files together successfully, but our attempts to join more than 2 PDFs have failed.

I will list code snippets of the various methods we have attempted and the resulting errors we are receiving.

Example #1:

First we attempted to use the code example provided in the help files for concatenating memory streams. This worked for the first 2 PDF files, but once we took the output from the first call to concatenate and attempted to concatenate a 3rd PDF, we got the following error:

System.IO.IOException : Rebuild failed: Value cannot be null.
Parameter name: args; Original message: PDF startxref not found.

Here is the sample code:

Dim x As Integer
Dim inStream1 As FileStream
Dim inStream2 As FileStream
Dim inFile1 As String
Dim inFile2 As String
Dim pdfEditor As PdfFileEditor = New PdfFileEditor
Dim outStream As MemoryStream = New MemoryStream
Dim outputPDF As String

inFile1 = Server.MapPath(sReportFiles(0).Trim)
For x = 1 To sReportFiles.length - 1
If sReportFiles(x).Trim.Length < 1 Then
If x = 1 Then
outputPDF = inFile1
End If
Exit For
End If

inStream1 = File.Open(inFile1, FileMode.Open, FileAccess.Read, FileShare.Read)
inFile2 = Server.MapPath(sReportFiles(x))
inStream2 = File.Open(inFile2, FileMode.Open, FileAccess.Read, FileShare.Read)
pdfEditor.Concatenate(inStream1, inStream2, outStream)

Dim outBuf() As Byte = outStream.GetBuffer()
Dim l_filestream As New System.IO.FileStream(outputPDF, System.IO.FileMode.Create)

l_filestream.Write(outBuf, 0, outBuf.Length)
l_filestream.Close()
l_filestream = Nothing
pdfEditor = Nothing
outStream = Nothing
outBuf = Nothing

pdfEditor = New PdfFileEditor
outStream = New MemoryStream
inStream1.Close()
inStream1 = Nothing
inStream2.Close()
inStream2 = Nothing

If x = 1 Then
inFile1 = outputPDF
End If

Next

Example #2:

In this attempt, we used the append method, allowing the pdfeditor class to handle all file IO. We simply passed the names of the PDF files that we wished to concatenate. The first two PDF files appended correctly, but on the third file, we got a file sharing error as the pedfeditor.append method did not release the file handle to the output PDF file once the first append operation was complete.

Dim x As Integer
Dim inStream1 As FileStream
Dim inStream2 As FileStream
Dim inFile1 As String
Dim inFile2 As String
Dim pdfEditor As PdfFileEditor = New PdfFileEditor
Dim outStream As MemoryStream = New MemoryStream
Dim outputPDF As String
Dim pstart As Integer = 1
Dim pend As Integer = 1
Dim TotalStreams As Integer

outputPDF = Server.MapPath(“Temp/OUTPUT_” & Now.Ticks.ToString + “.pdf”)
inFile1 = Server.MapPath(sReportFiles(0).Trim)
For x = 1 To sReportFiles.Length - 1
If sReportFiles(x).Trim.Length < 1 Then
If x = 1 Then
outputPDF = inFile1
End If
Exit For
End If

inFile2 = Server.MapPath(sReportFiles(x).trim)
pdfEditor = New PdfFileEditor
pdfEditor.Append(inFile1, inFile2, pstart, pend, outputPDF)
pdfEditor = Nothing
If x = 1 Then
inFile1 = outputPDF
End If
Next

Example #3:

In this example, we used the memory array method as demonstrated in your sample code listed at URL: http://www.aspose.com/wiki/default.aspx/Aspose.Pdf.Kit/EditExistingPdf.html

Once we built an array of FileStream objects containing the various PDF data streams, we attempted to call the pdfeditor.concatenate method, only to find that this Overloaded method does not support the calling hierarchy as shown in the sample code.

Dim x As Integer
Dim inFile1 As String
Dim pdfEditor As PdfFileEditor = New PdfFileEditor
Dim outputPDF As String
Dim TotalStreams As Integer

outputPDF = Server.MapPath(“Temp/OUTPUT_” & Now.Ticks.ToString + “.pdf”)

For x = 0 To sReportFiles.Length - 1
If sReportFiles(x).Trim.Length < 1 Then
Exit For
End If
TotalStreams = x
Next

Dim FileStreams As FileStream() = Array.CreateInstance(GetType(System.IO.Stream), TotalStreams)

Dim outputStream As FileStream = New FileStream(outputPDF, FileMode.Create)

For x = 0 To TotalStreams
inFile1 = sReportFiles(x).Trim
FileStreams(x) = File.Open(inFile1, FileMode.Open, FileAccess.Read, FileShare.Read)
Next

pdfEditor.Concatenate(FileStreams, outputStream) <-- JIT error = Overload resolution failed because no accessible ‘Concatenate’ accepts this number of arguments.


--------------------------------------------------

Please advise as to whether your product can / will function as advertised.

Regards,

Mark Davis


We have corrected the problems we were having…

We were using the 1.1.0 version of the Pdf.Kit assembly. We upgraded to the 1.3.0 version today and our solution is now working…

Please disregard our previous posting…

Regards,

Mark Davis