We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Word-->PDF To Client Browser

I have created an ASPOSE word document on the server and I am trying to send it to the client as a downloadable PDF file.
I have been using the following solution below for awhile which works fine, but I want to be able to have the same solution using the new functionality of ASPOSE.Words.Document.SaveToPDF(). Can you please provide me with an equivalent solution that opens the document (without saving it) in Acrobat on the client.

'Save the document in Aspose.Pdf.Xml format into a memory stream.
Dim xmlStream As MemoryStream = New MemoryStream
doc.Save(xmlStream, SaveFormat.AsposePdf)
'Seek to the beginning so it can be read by XmlDocument.
xmlStream.Seek(0, SeekOrigin.Begin)
'Load the XML document into Aspose.Pdf
Dim pdf As Aspose.Pdf.Pdf = New Aspose.Pdf.Pdf
'Make sure the images that were saved by Aspose.Words into Windows temporary
'folder are automatically deleted by Aspose.Pdf when they are no longer needed.
pdf.IsImagesInXmlDeleteNeeded = True
pdf.BindXML(xmlStream, Nothing)
'*** Aspose.Pdf font cache, see comments below.
pdf.IsTruetypeFontMapCached = False
'If you convert to PDF multiple files in your application, 
'uncomment the following lines to improve the speed of conversion.
'pdf.IsTruetypeFontMapCached = true;
'pdf.TruetypeFontMapPath = 
'Now produce the PDF file.
pdf.Save(title & ".pdf", Aspose.Pdf.SaveType.OpenInAcrobat, Response)

Thank You,

Thanks for your inquiry. The technique is the same. Please see the following code example.

// Open a word document
Document doc = new Document(@"C:\Temp\in.doc");
// Save the document in PDF format and send it to client browser
doc.Save("out.pdf", SaveFormat.Pdf, SaveType.OpenInWord, Response);

Hope this helps.
Best regards.

Hi, thanks for the reply.
I tried this method the other day, and then I just tried it again…but I get an error when I open the document in Adobe Reader stating “There was an error opening this document. The file is damaged and cannot be repaired”.
I’m simply creating a report starting from a Word Document Template (.doc extension), with a couple of hundred pages.
I don’t have any problems when creating the pdf the old way, nor do I have problems creating the pdf by doing document.saveToPdf(filename) and then opening the document.
Also, why would I provide saveFormat.OpenInWord, when I’m trying to open the document in Adobe
Can you please tell me why this is happening.

Thanks for your request.

  1. Let I explain a bit regarding SaveType.OpenInBrowser and SaveType.OpenInWord. Actually, document is sent to a client browser using the following code:
MemoryStream stream = new MemoryStream();
doc.Save(stream, SaveFormat.Pdf);
byte[] bytes = stream.GetBuffer();
// Specify the document type.
Response.ContentType = "**application/pdf**";
// Other options:
// Response.ContentType = "application/msword"
// Response.ContentType = "text/plain"
// Response.ContentType = "text/html"
// Specify how the document is sent to the browser.
Response.AddHeader("content-disposition", "attachment; filename=test.doc");
// Another option could be:
// Response.AddHeader "content-disposition","inline; filename=MyDocument.doc"; 
// Get data bytes from the stream and send it to the response.

When attachment option is used PDF document will be opened using default allocation for PDF format (Adobe Acrobat Reader in my case) otherwise (when inline option is used) PDF document will be opened in browser window.
SaveType.OpenInBrowser uses inline option.
SaveType.OpenInWord uses attachment option
Application that will be used to open file is detected by content type. In our case, it is “application/pdf”.
2. Maybe there is something wrong with PDF document generated by Aspose.Words. Could you please attach your document for testing? Have you tried to save a PDF document on a disk and then open it using PDF reader?
Best regards.

I tried changing my document extension to .docx and now it works. I believe there is a bug trying to use .doc or .dot files when doing this.
Can you please confirm this and let me know.

Thanks for your request. Why did you change an extension? Here is what each parameter means:
doc.Save(“out.pdf” //Name of document that will appear in open/save dialog
, SaveFormat.Pdf //ContentType “application/pdf” is used for PDF documents (“application/msword” for word documents)
, SaveType.OpenInWord //inline or attachment
, Response //Reponse in which we will send our document.
Best regards.

I found that if I don’t put a response.end() after the doc.save() method (even though my code doesn’t execute any further) gives me the error. Can you please explain?
Also, I notice that generating a .pdf takes twice as long as doing just a .doc, why?
Also, when are you going to fix Aspose.Words so that the Row Headers show up on every page when converted to PDF

Thanks for your request.

  1. I cannot reproduce the problem on my side so I cannot tell you why this occurs. Could you please provide me your code and document that will allow me to reproduce the issue?
  2. Creating PDF document is not trivial task. Aspose.Words has different module for import/export of each supported format. Some formats are processed faster some processed slower.
  3. Currently, I cannot provide you any estimate regarding this issue. This problem will be fixed in one of future release.
    Best regards.