PDF not opening as part of iTextSharp API

Hi,

I am using the following code on button click to return the current web page as a pdf:


<%@ Control Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="iTextSharp.text" %>
<%@ Import Namespace="iTextSharp.text.pdf" %>
<%@ Import Namespace="iTextSharp.text.html" %>
<%@ Import Namespace="iTextSharp.text.html.simpleparser" %>
<%@ Import Namespace="iTextSharp.text.xml" %>

<script runat="server">

    private void buttonPDF_Click(object sender, System.EventArgs e)
    {
        string currentURL = Request.Url.ToString();

        //Set content type to PDF
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=" + currentURL + ".pdf");

        //Create new objects
        Document pdfDocument = new Document();
        StringWriter strWriter = new StringWriter();

        //Create HTML Text Writer allowing us to write HTML
        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(strWriter);

        //Create a writer that listens to the document
        PdfWriter.GetInstance(pdfDocument, Response.OutputStream);
        pdfDocument.Open();

        //Create String Reader to read parts of strings
        StringReader strReader = new StringReader(strWriter.ToString());

        //Create HTML Worker for parsing simple HTML
        HTMLWorker htmlWorker = new HTMLWorker(pdfDocument);
        htmlWorker.Parse(strReader);

        //Stop processing script and return current result
        Response.End();
    }
</script>

<asp:Button runat="server" ID="buttonPDF" OnClick="buttonPDF_Click" CssClass="button" Text="PDF"></asp:Button>

The pdf is being created successfully but Adobe Acrobat reader says it cannot open the file? I think the file is corrupt and not correct?

Can anybody please help?

Thanks,

I have nearly got this working, I have changed to code to the below:


<%@ Control Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="iTextSharp.text" %>
<%@ Import Namespace="iTextSharp.text.pdf" %>
<%@ Import Namespace="iTextSharp.text.html" %>
<%@ Import Namespace="iTextSharp.text.html.simpleparser" %>
<%@ Import Namespace="iTextSharp.text.xml" %>
<%@ Import Namespace="log4net" %>

<script runat="server">

    // Setup logging
    private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    
    private void buttonPDF_Click(object sender, System.EventArgs e)
    { 
        //Get page URL
        string currentURL = Request.Url.ToString();

        //Create new objects
        MemoryStream MStream = new MemoryStream();
        Document document = new Document();
        
        //Attempt to create writer that listens to the document
        try
        {
            PdfWriter writer = PdfWriter.GetInstance(document, MStream);
            document.Open();
            document.Add(new iTextSharp.text.Paragraph("This is a test and must work"));
            document.Close();
        }
        
        //If unsuccessful throw an exception
        catch
        {
            Log.Debug("There has been an error creating a PDF");
        }

        //Set content type to PDF and create filename
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.ContentType = "application/pdf";
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + currentURL + ".pdf");
        HttpContext.Current.Response.BinaryWrite(MStream.GetBuffer());
        HttpContext.Current.Response.End();
    }

</script>

<asp:Button runat="server" ID="buttonPDF" OnClick="buttonPDF_Click" CssClass="button" Text="PDF"></asp:Button>

When I click the button a PDF is created and the text appears “This is a test and must work”

Now all I need to do is instead of writing out that text, I need to return the current web page as a PDF. So this line:


document.Add(new iTextSharp.text.Paragraph("This is a test and must work"));

I need to render out the page, how can I do this?

Is this possible?