Help! Can I concatenate PDFs in-memory?

Hello,

This is urgent! I need to demo an application at 1pm today and I can’t seem to concatenate PDFs as I was lead to believe I could! It seems the only way to do it is from filestreams - what if I convert a bunch of word docs to PDFs in-memory and then want to concatenate the resultant PDFs? Is there a way to do this??

Please response asap.

Thanks,
Ken

MemoryStream inStream1 = new MemoryStream();//Pdf file in mem
MemoryStream inStream2 = new MemoryStream();//Pdf file in mem

//Result Pdf saved as a file

FileStream outputStream = new FileStream(outFile,FileMode.Create);
PdfFileEditor editor = new PdfFileEditor();
editor.Concatenate(inStream1,inStream2,outputStream);
outputStream.Close();

Hi Kevin,

Thank you for your quick response! That seems to work. I guess my next question would have to be how would I go about taking the final memory stream with the concatenated pdf and send it to the user’s browser?

Thanks!
Ken

//output Pdf stream.
MemoryStream outStream = new MemoryStream();

byte[] outBuf = outStream.GetBuffer();

//response to user's browser.
HttpResponse response.Expires = 0;
response.Buffer = true;
response.ClearContent();
response.AddHeader("content-disposition","inline; filename=" + "output.pdf");
response.ContentType = "application/pdf";
response.BinaryWrite(outBuf);
outStream.Close();
response.End();

I am combining PDFs using the following code:


    Public Function CombinePDFs(ByVal refHyperLinks() As String) As MemoryStream
        ’ Takes array of word document filenames and converts them into a merged Pdf memory stream

        Dim inStreams() As Stream
        Dim outStream As MemoryStream = New MemoryStream
        Dim i As Integer = 0

        Dim fileName As String
        For Each fileName In refHyperLinks
            Dim outPdf As Pdf = New Pdf

            ’ Convert source word doc to Pdf
            Dim sourceDocument As Document = New Document(fileName)
            outPdf = WordToPdf(sourceDocument)

            ’ Save created Pdf to stream and add to stream array of documents to be combined
            Dim inStream As MemoryStream = New MemoryStream
            outPdf.Save(inStream)
            inStreams(i) = inStream
            i += 1
        Next

        ’ Combine Pdf streams into one Pdf
        Dim pdfEditor As Kit.PdfFileEditor = New Kit.PdfFileEditor
        pdfEditor.Concatenate(inStreams, outStream)

        Return outStream
        outStream.Close()
    End Function


However I’m getting the good ol “object reference not set to an instance of an object” error. Do you have any idea what I’m doing wrong?

Thanks,
K

Thanks! I had the response part correct but I can’t seem to be able to concatenate without hitting object errors. Let me repost the code as it’s more or less unreadable:


Public Function CombinePDFs(ByVal refHyperLinks() As String) As MemoryStream
’ Takes array of word document filenames and converts them into a merged Pdf memory stream

Dim inStreams() As Stream
Dim outStream As MemoryStream = New MemoryStream
Dim i As Integer = 0

Dim fileName As String
For Each fileName In refHyperLinks
Dim outPdf As Pdf = New Pdf

’ Convert source word doc to Pdf
Dim sourceDocument As Document = New Document(fileName)
outPdf = WordToPdf(sourceDocument)

’ Save created Pdf to stream and add to stream array of documents to be combined
Dim inStream As MemoryStream = New MemoryStream
outPdf.Save(inStream)
inStreams(i) = inStream
i += 1
Next

’ Combine Pdf streams into one Pdf
Dim pdfEditor As Kit.PdfFileEditor = New Kit.PdfFileEditor
pdfEditor.Concatenate(inStreams, outStream)

Return outStream
outStream.Close()
End Function


Thanks!
K

Hi ,
Could u provide more details about the error, such as when did it occur after the code stopped?

And be sure you have enough access authority to manipulate those words and pdfs.

Hi

It seems this error has nothing to do with Aspose.Pdf and Kit, because you don’ provide details, please check out the following page. Maybe it could do you a favor.

http://www.dotnet247.com/247reference/msgs/54/273098.aspx

http://www.c-sharpcorner.com/Code/2002/Sept/ObjectRefError.asp

I understand what the error means - it is happening within that function somewhere according to the trace. I don’t have time to figure it out right now as the meeting is soon. I will report back and let you know how it works so other users can benefit from this type of conversion.

Dear K,

Please change add the following line (in Bold fashion) into your code and try again:

Public Function CombinePDFs(ByVal refHyperLinks() As String) As MemoryStream
' Takes array of word document filenames and converts them into a merged Pdf memory stream

Dim inStreams() As Stream
Dim outStream As MemoryStream = New MemoryStream
Dim i As Integer = 0

Dim fileName As String
For Each fileName In refHyperLinks
Dim outPdf As Pdf = New Pdf

' Convert source word doc to Pdf
Dim sourceDocument As Document = New Document(fileName)
outPdf = WordToPdf(sourceDocument)

' Save created Pdf to stream and add to stream array of documents to be combined
Dim inStream As MemoryStream = New MemoryStream
outPdf.Save(inStream)
inStreams(i) = inStream

inStreams(i).Position = 0


i += 1
Next

' Combine Pdf streams into one Pdf
Dim pdfEditor As Kit.PdfFileEditor = New Kit.PdfFileEditor
pdfEditor.Concatenate(inStreams, outStream)

Return outStream
outStream.Close()
End Function

Best regards.