Solved

using iTextSharp to create PDF that opens in browser

Posted on 2014-01-03
1
1,588 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

829 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