Solved

using iTextSharp to create PDF that opens in browser

Posted on 2014-01-03
1
1,498 Views
Last Modified: 2014-01-06
I'm using Itextsharp to call a page and then create a pdf from the contents of that page. I want the file to either open in the browser or allow the user to save. The included code creates a file, but gives the following error when trying to open it.

Adobe Reader could not open '' because it is either not a supported file type or because the file has been damaged.

If I use PdfWriter.GetInstance(document, new FileStream("c:\\Application" + bb + ".pdf", FileMode.Create)); the file is created on my file system perfectly, but since the site will sit on a server, I don't want it to work that way. What can I do to fix this?

try
        {
            string bb = Request.QueryString["pid"];
            Response.ContentType = "application/pdf";
            Response.AppendHeader(
              "Content-Disposition",
              "attachment; filename=test.pdf"
            );
            using (Document document = new Document())
            {
                PdfWriter.GetInstance(document, Response.OutputStream);
                WebClient wc = new WebClient();
                string htmlText = wc.DownloadString("http://myserver/GeneratePDF/CreatePDF.aspx?pid=" + bb);
                Response.Write(htmlText.Replace("\r", "").Replace("\n", "").Replace("  ", ""));
                List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), null);
                for (int k = 0; k < htmlarraylist.Count; k++)
                {
                    document.Add((IElement)htmlarraylist[k]);
                }

            }
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }

Open in new window

0
Comment
Question by:NickMalloy
1 Comment
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 39755226
I've never used iTextSharp, but the documentation at this site says you need to call Close() on your document.

http://www.developerfusion.com/code/5682/create-pdf-files-on-fly-in-c/

If that doesn't solve the problem another option you might consider is rather than writing directly to Response.OutputStream you could try capturing the output into a MemoryStream and then using Response.BinaryWrite to send the file from the MemoryStream.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now