Solved

using iTextSharp to create PDF that opens in browser

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Different Delete Messages 7 37
Error when loading the database 16 56
Error in script 11 45
free scanner TWAIN can be operated with a Web application 9 38
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

912 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

19 Experts available now in Live!

Get 1:1 Help Now