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

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;

}

Hi,

Please try adding

response.End();

After Pdf.Save().

Thank you. That did the trick.

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

Hi,

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:

response.Clear();
response.ClearContent();
response.ClearHeaders();

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

response.BinaryWrite(buf);

response.Flush();
response.End();

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.

http://www.velocityreviews.com/forums/t163011-sending-pdf-content-to-the-browser.html

http://blog.csdn.net/Teng_s2000/archive/2006/04/25/676440.aspx

Please let us known if you can resolve this problem.

Hi,
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.

Alan