Converting from .docx to .pdf then merging pdfs creates corrupted pdfs

Versions:
Aspose.Pdf 11.3.0.0
Aspose.Words 16.1.0.0
Error:
Result: Produces corrupted pdfs…
Error Message: There is a problem reading this document (14).
Meaning: Pdf is corrupted…
Simplified Code Snippet:

static void Main(string[] args)
{
string url1 = “C:\temp\input1.docx”;
string url2 = “C:\temp\input2.pdf”;
string url3 = “C:\temp\input2.pdf”;
// Gather Inputs.
Document input1 = WordToPdf(url1);
Document input2 = new Document(url2);
Document input3 = new Document(url3);
// Humor the array method… The actual implementation assumes that not
// all inputs have data, so it populates the array as necessary.
Queue queue = new Queue();
queue.Enqueue(input1);
queue.Enqueue(input2);
queue.Enqueue(input3);
// Actual Implementation converts result to Memory Stream, and then to byte array
// to be stored in the database.
Document collatedpdf = queue.Dequeue();
while (queue.Count > 0) {
collatedpdf.Pages.Add(queue.Dequeue().Pages);
}
// Save Collated Pdf.
collatedpdf.Save(“C:\temp\output.pdf”, SaveFormat.Pdf);
}
private static Aspose.Pdf.Document WordToPdf(string url)
{
// Load .docx file…
Aspose.Words.Document wordDoc = new Aspose.Words.Document(url);
// Convert .docx to .pdf
Aspose.Pdf.Document pdf = new Aspose.Pdf.Document();
using (MemoryStream ms = new MemoryStream())
{
wordDoc.Save(ms, Aspose.Words.SaveFormat.Pdf);
pdf = new Aspose.Pdf.Document(ms);
//ms.Position = 0;
}
return pdf;
}
}

Hi there,


Thanks for your inquiry. I have tested the scenario using Aspose.Words for .NET 16.1.0 and Aspose.Pdf for .NET 11.3.0 and unable to replicate the reported exception issue. We will appreciate it if you please share a sample console project here to replicate the issue, we will look into it and guide you accordingly.

We are sorry for inconvenience caused.

Best Regards,

I would be glad too. It certainly is a weird error. I am sharing the output file, and the project file.

Hi Zachary,


Thanks for sharing the sample project. We are working on testing the scenario using this project and will keep you posted with our findings.

I did some more testing, and I can confirm that it has something to do with the Word to Pdf conversion. I ran the solution without trying to convert and merge the word document, and the merge was successful.

Hi Zachary,


Thanks for your patience. Yes you are right actually stream object should not be closed before the Save() method call of the Aspose.Pdf.Document class. So if you change your code as following then it will work as expected.

//
using (MemoryStream ms = new MemoryStream())
<o:p></o:p>

// {

MemoryStream ms = new MemoryStream();

wordDoc.Save(ms, Aspose.Words.SaveFormat.Pdf);

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

//ms.Position = 0;

// }

return pdf;


We are sorry for the inconvenience caused.

Best Regards,

You are a genius, it worked. Thank you very much.

Hi Zachary,


Thanks for the acknowledgement.

We are glad to hear that your problem is resolved. Please continue using our API’s and in the event of any further query, please feel free to contact.