Solved

using iTextSharp to create PDF that opens in browser

Posted on 2014-01-03
1
1,681 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

728 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