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

Free Support Forum - aspose.com

PDF Files are Damaged

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();


// 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)


// 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;



Please try adding


After Pdf.Save().

Thank you. That did the trick.

Although this works, it raises an undesirable ThreadAbortException. Is there a clean way around that?


I am not sure why this exception is thrown. No other customers reported this problem. In order to find the problem, you can save the PDF into a stream and put it into the web browser by your self like the following:


if(saveType == SaveType.OpenInAcrobat)
response.AddHeader("content-disposition","attachment; filename=" + fileName);
response.AddHeader("content-disposition","inline; filename=" + fileName);
response.ContentType = "application/pdf";



I am not sure if all the code about response is needed. Maybe some of the lines causes the problem in your application.

That doesn't help. Same error.

If you search the forum, you will see other people have had this problem as well.

I also receive the following error:

Server cannot clear headers after HTTP headers have been sent.

Right now, I am just trapping these errors and continuing, but that certainly impacts performance, and is something of a kludge.

The problem is that I can’t reproduce this error. You can refer to other articles and try to modify the code.



Please let us known if you can resolve this problem.

I think you can try to recover the corrupt pdf. I once used a utility called [Advanced PDF Repair] to repair my corrupt PDF file. It works rather well. You can just try the free demo, if necessary.