Multiple PDFs to browser

I have several word documents I would like to convert to PDF and then combine those PDFs to one stream to send to the browser. Any ideas. I was trying to use the PdfFileEditor Concatenate but I am getting errors.

Hello Micheal,

Thanks for considering Aspose.

I am not sure why you are facing the problem. Please share the resource files and code snippet that you are using so that we can test the issue at our end. We apologize for your inconvenience.

Dim i As Integer = 0
Dim stream(docList.Count - 1) As IO.Stream
Dim outStream As IO.Stream = New IO.MemoryStream()
Dim pdf As Aspose.Pdf.Pdf = New Aspose.Pdf.Pdf()
Dim pdfEdit As New Aspose.Pdf.Kit.PdfFileEditor()
'
For Each wordDoc As Document In docList
stream(i) = New IO.MemoryStream()
wordDoc.Save(stream(i), SaveFormat.AsposePdf)
i += 1
Next
'
pdfEdit.Concatenate(stream, outStream) --Blows up here with error
outStream.Seek(0, IO.SeekOrigin.Begin)
Dim xmlDoc As New XmlDocument()
xmlDoc.Load(outStream)
pdf.BindXML(xmlDoc, Nothing)
pdf.Save("Forms.pdf", Aspose.Pdf.SaveType.OpenInBrowser, Response)

Blows up here with error -- pdfEdit.Concatenate(stream, outStream)

error is "Invalid pdf format:pdf head signature is not found!"

If I change the SaveType to just PDF I get an xml error when loading it into the Xml document.

That error is XmlException "Data at the root level is invalid. Line 1, position 1."

Hello Micheal,

Let me share something related to converting Word document into Pdf.

Recent version of Aspose.Words offers the capability to save the word file directly into Pdf format. Use the following lines of code to accomplish this requirement.

[C#]

Document doc = new Document(MyDir + "Document.doc");

doc.Save(MyDir + "Document.Doc2PdfSave Out.pdf");

You can also convert the Word document into Pdf format while using Aspose.Words and Aspose.Pdf components. In this case Aspose.Words transforms the input word document into intermediate XML file and Aspose.Pdf generates Pdf file based on the contents of XML file generated by Aspose.Words. (This is the “legacy” method of converting to PDF and will be deprecated. See the new method in Convert to PDF Directly.).

[C#]<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

// Open the document.

Document doc = new Document(MyDir + "Document.doc");

// ... You can merge data/manipulate document content here.

// Save the document in the Aspose.Pdf.Xml format.

string xmlFile = MyDir + "Document.ConvertToPdf Out.xml";

doc.Save(xmlFile, SaveFormat.AsposePdf);

// Read the document into Aspose.Pdf.

Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();

pdf.BindXML(xmlFile, null);

// Instruct to delete temporary image files.

pdf.IsImagesInXmlDeleteNeeded = true;

// This is optional, but can speed up Aspose.Pdf if you convert multiple files in one session.

pdf.IsTruetypeFontMapCached = true;

pdf.TruetypeFontMapPath = Path.GetTempPath();

// Produce the PDF file.

pdf.Save(MyDir + "Document.ConvertToPdfViaAsposePdf Out.pdf");

As far as the problem that I've noticed in your snippet is that,

pdfEdit.Concatenate(stream, outStream) --Blows up here with error

this line shows error because, over the line wordDoc.Save(stream(i), SaveFormat.AsposePdf) you have saved word document contents in XML format into Stream objet, and when Concatenate method is called, it shows "pdf head signature is not found" because Stream is holding XML based conents.

Secondly, when changing the Save type to Pdf, XML throws error, because in this case Stream is holding Pdf object and XMLDocument object is looking for XML based contents.

So as a solution, First please try converting Word documents into Pdf and than use Concatenate method in PdfFileEditor class of Aspose.Pdf.Kit for .NET and Java to combine all the Pdf files into Single Stream object and display it into Browser.

In order to display the Pdf file you can use PdfViewer in Aspose.Pdf.Kit. For more information please visit How to use PdfViewer.

For more information on how to Convert Word file into Pdf, please visit Saving Documents

For more information on how to Concatenate Pdf files, please visit Concatenate PDF Documents

In case you still face any problem, please feel free to share.

If try to do that i get an error at the browser that says “File does not begin with ‘%PDF-’”

Got it working with using the PDF veiwer