Free Support Forum - aspose.com

Open pdf in client Browser


#1

Hi,

I’ve created a document using Document Builder and converted it into pdf format.
When i try to open this pdf file in browser, I get the error “An exception of type ‘System.Xml.XmlException’ occurred in System.Xml.dll but was not handled in user code Additional information: Data at the root level is invalid. Line 1, position 1.”

Here is the code:

        MemoryStream stream=new MemoryStream();
        Word.Saving.PdfSaveOptions options=new Word.Saving.PdfSaveOptions();
        options.OutlineOptions.HeadingsOutlineLevels=2;
        doc.Save(stream,options);


        //stream.Seek(0, SeekOrigin.Begin);

        //Load the document into an XmlDocument
        XmlDocument xmlDoc=new XmlDocument();
        xmlDoc.Load(stream); //--- The error rises here ---
        //Load the XML document into Aspose.Pdf
        Aspose.Pdf.Generator.Pdf pdf=new Aspose.Pdf.Generator.Pdf();
        pdf.IsBookmarked=true;
        //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(xmlDoc,null);
        //Aspose.Pdf font cache, see comments below.
        pdf.IsTruetypeFontMapCached = false;
        HttpResponse Response=null;
        pdf.Save("Report.pdf",Aspose.Pdf.Generator.SaveType.OpenInAcrobat,Response);

#2

@Srujana

Thanks for your inquiry. You can save the Word document to PDF and send it to client browser using Aspose.Words. Please refer to the following article.
Sending to a Client Browser

If you want to use Aspose.PDF, please read following article.
Sending Pdf to Browser for Download


#3

Hi tahir,

I tried ‘sending pdf to Browser for download’
case 1:
Aspose.Pdf.Document pdfdoc = new Aspose.Pdf.Document();
MemoryStream stream = new MemoryStream();
doc.Save(stream, Word.SaveFormat.Pdf);
pdfdoc.Save(stream);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Charset = “UTF-8”;
Response.AddHeader(“content-length”, stream.Length.ToString());
Response.AddHeader(“content-disposition”, String.Format(“attachment;filename=TestDocument.pdf”, “FileName”));
Response.ContentType = “application/pdf”; Response.BinaryWrite(stream.ToArray());
Response.Flush();
Response.End();

The file with pdf format is downloading onto harddisk without showing preview on browser. The first page of document is missing. And also, when i try to open this file with Adobe Acrobat, the document seems empty and says: There was a problem reading this document (14). But, when i try to open this document in browser, it is opened with 4 pages out of 5 and the first page is missing.

case 2:
When I tried to comment Response.AddHeader statements. The pdf file is opened in same page of the browser, with no first page. And then when i download and open it,the document seems empty and says: There was a problem reading this document (14). But, when i try to open this document in browser, it is opened with 4 pages out of 5 and the first page is missing.

Thankyou,
Srujana


#4

Hi Tahir,

I figured it out.
String.Format(“attachment;filename=TestDocument.pdf”, “FileName”) was getting the document as attachment. I changed it to String.Format(“inline; filename=MyDocument.pdf”, “FileName”) which was opening the document in browser. (i realized it was default format)

The firstpage in the document is missing because after creating and loading the doc with documentbuilder, instead of saving the doc as pdf format, I created another Pdf.Document, which was actually creating an issue to read the file in adobe.
I just used doc.Save(stream, Word.SaveFormat.Pdf); and response statements. Now, everything is awesome!


Open created document in Browser
#5

@Srujana

Thank you for your kind feedback.

We are glad to know that your problem has been resolved. Please keep using our API and in event of any further query, feel free to ask.


#6

Hi Farhan,

Can i know if there is code to open the created pdf on new tab of the browser?
Thankyou in advance.

Regards,
Srujana


#7

@Srujana

You may please explore Response Class if that can help you achieve these requirements because, we are afraid, Aspose APIs may not be relevant in this context.