Word to PDF - OpenInBrowser/OpenInAcrobat

Hello,

We’re tyring out different APIs for converting Word documents to PDF on the fly on our website, and Aspose.Pdf and Aspose.Word seems to be a good option. But before buying the license we want to make sure it can handle our files and do what we want.

Right now I’m having a problem opening the created pdf document in the browser or acrobat. But, if I save the pdf to disk, it opens without any problems. I get this error: “There was an error opening the document. The file is damaged and could not be repaired.” First I had a more complex word document, but I’ve also tried a word document with one single line of text.

Here’s my code:

Aspose.Word.License word = new Aspose.Word.License();
Document doc = new Document(“c:\Temp\gb.doc”);

MemoryStream memStream = new MemoryStream();
byte[] buffer = new byte[1024];

doc.Save(memStream, SaveFormat.FormatAsposePdf);
memStream.Seek(0, SeekOrigin.Begin);

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(memStream);

Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();
pdf.BindXML(xmlDoc, null);

[//pdf.Save](https://pdf.save/)(“c:\temp\testpdf.pdf”);

pdf.Save(“test.pdf”, Aspose.Pdf.SaveType.OpenInBrowser, Response);

Regards,

Jan-Erik Rom?ren

Dear Jan-Erik,

Thank you for considering Aspose.

Can you please send an example which can reproduce the error to me?

FYI: The solution was very simple: I had forgot to end the response.

pdf.Save(“test.pdf”, Aspose.Pdf.SaveType.OpenInAcrobat, System.Web.HttpContext.Current.Response);

System.Web.HttpContext.Current.Response.End();

Thanks for the info. Fixed my problem also..

Hello again,

Well, I thought I had everything under control now, but it seems there’s more to be solved.

First of all. By ending the response, the pdf worked, but I encountered another problem; after opening the pdf, there are more code I want to run, but since I’m ending the response, the code won’t run.

Second, I want to open the pdf in a new browser window (OpenInBrowser). Do anyone know how I can do this? I’ve tried to use Response.write and using javascript window.open, which works fine when that’s the only thing I do. But, when I’m trying to call the code which opens the pdf after the new window should be opened, the pdf opens in the same window, and the new window is never opened.

Do anyone have any idea how this can be solved?