Converting to Word doc then using aspose.word corrupts the file

Hello, I am using both Aspose.PDF and Aspose.Words in C# to replace template words with the correct values. Currently I want to convert PDF documents to word documents because I want to use the word document replace functionality. I am seeing a problem when I convert to a word doc using Aspose.Pdf, then try to create an Aspose.Words document from the saved converted MemoryStream. When I download the file saved from Aspose.Words, I get an error when opening the file - “File has been corrupted”. This happens without any replace actions or any editing of the document.

Here is example code that I am using:

MemoryStream outStream = new MemoryStream();
byte[] content = template.Content;
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(new MemoryStream(template.Content));
doc.Save(outStream, Aspose.Pdf.SaveFormat.DocX);
Aspose.Words.Document doc2 = new Aspose.Words.Document(outStream);
//Normally edit doc2 here
doc2.Save(outStream, Aspose.Words.SaveFormat.Docx);

return outStream.ToArray() //Get bytes for download

This happens with any file I use, so I do not believe it is file specific.

@Shoffmann

Could you please try following code snippet with your file to convert it into .docx using Aspose.PDF and in case you still face any issue, please share sample file with us. We will test the scenario in our environment and address it accordingly.

MemoryStream outStream = new MemoryStream();
byte[] content = template.Content;
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(new MemoryStream(template.Content));
doc.Save(outStream, new DocSaveOptions()
            {
                Format = DocSaveOptions.DocFormat.DocX
            });

Hello, thank you for your help!

What was causing the problem was I wasn’t resetting the stream.Position = 0.

Here is my code that I got working if anyone ever has a similar issue:

MemoryStream outStream = new MemoryStream();
byte[] content = template.Content;
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(new MemoryStream(content));
doc.Save(outStream, Aspose.Pdf.SaveFormat.DocX);

outStream.Position = 0; // reset stream position

Aspose.Words.Document doc2 = new Aspose.Words.Document(outStream);
//Normally edit doc2 here
doc2.Save(outStream, Aspose.Words.SaveFormat.Docx);

outStream.Position = 0; // reset stream position`

return outStream.ToArray() //Get bytes for download

@Shoffmann

It is good to know that you managed to resolve your issue. Please keep using our API and in case of any other inquiry.