I receive the following error message when converting a Word document to PDF and attempting display in a web browser:
There was an error opening this document. The file is damaged and could not be repaired.
I am using the latest versions of Aspose.Words and Aspose.PDF with the code below. There is no problem with PrepareDocument. It works fine if saving the PDF to disk.
public void SaveDocToPdfWeb(string fileName, HttpResponse response)
{
// Prepare the document
Aspose.Pdf.Pdf pdf = PrepareDocument();
// Save the result
pdf.Save(string.Format("{0}.pdf", fileName), Aspose.Pdf.SaveType.OpenInAcrobat, response);
}
private Pdf PrepareDocument()
{
// Set the Word doc margins
_Builder.PageSetup.PaperSize = PaperSize.Letter;
_Builder.PageSetup.TopMargin = this.TopMargin;
_Builder.PageSetup.LeftMargin = this.LeftMargin;
_Builder.PageSetup.BottomMargin = this.BottomMargin;
_Builder.PageSetup.RightMargin = this.RightMargin;
// First save the Word document
MemoryStream dstStream = new MemoryStream();
_Doc.Save(dstStream, SaveFormat.AsposePdf);
dstStream.Seek(0, SeekOrigin.Begin);
//Load the document into an XmlDocument
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(dstStream);
// Create a new pdf object
Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();
// Bind content from the named xml file.
pdf.BindXML(xmlDoc, null);
// Page setup
pdf.Sections[0].PageInfo.PageHeight = Aspose.Pdf.PageSize.LetterHeight;
pdf.Sections[0].PageInfo.PageWidth = Aspose.Pdf.PageSize.LetterWidth;
pdf.Sections[0].PageInfo.Margin.Top = this.TopMargin;
pdf.Sections[0].PageInfo.Margin.Left = this.LeftMargin;
pdf.Sections[0].PageInfo.Margin.Bottom = this.BottomMargin;
pdf.Sections[0].PageInfo.Margin.Right = this.RightMargin;
if(pdf.Sections.Count > 1)
pdf.Sections.RemoveAt(1);
// Security
Security security = new Security();
security.IsContentsModifyingAllowed = false;
security.IsCopyingAllowed = false;
security.IsDocumentAssemblyingAllowed = false;
security.Is128BitsEncrypted = true;
pdf.Security = security;
// Dispose of any images
pdf.IsImagesInXmlDeleteNeeded = true;
return pdf;
}