locked
ASPX page to PDF format RRS feed

  • Question

  • User-1258070753 posted

    yes  i m using itextsharp.dll in my project


    my requirement is i want to convert my whole page into a pdf format

    the code which i have writtent is converting just the value which i m passing in my label's

    see here e.g

    [IMG]http://i36.tinypic.com/149xixu.jpg[/IMG]

     protected void Page_Load(object sender, EventArgs e)
        {
            Label2.Text = "1";
            Label3.Text = "Rockyy";
            Label4.Text = "Sr.J.Cart";
    
        }


    protected void Button1_Click(object sender, EventArgs e) { var doc1 = new Document(); string path = Server.MapPath("pdf"); PdfWriter.GetInstance(doc1, new FileStream(path + "/doc1.pdf", FileMode.Create)); doc1.Open(); doc1.Add(new Paragraph(" my check")); doc1.Add(new Paragraph(Label1.Text)); doc1.Add(new Paragraph(Label3.Text)); doc1.Add(new Paragraph(Label4.Text)); doc1.Close(); }





    how i can just convert my whole page into a pdf that includes everything on page instead of just passing the whole value

    any idea please ..


    Rockyy

    Saturday, August 28, 2010 7:57 AM

Answers

  • User-821857111 posted

    OK, but you will find questions all over the Internet about using iTextSharp to convert an HTML page to PDF, and the answer is invariably the same: it's very difficult. That's because the HTML parser is not robust enough to handle anything other than valid XML, and is not designed as an HTML parser in the sense you would like it to be. Even Bruno Lowagie (the creator of iText) in his book says it has not been designed for that. Some of the paid-for components can do what you want. Others work by firing up the page in a Web browser type control and taking a screenshot. iTextSharp will not play nicely with css either. You will do so much work configuring styles etc within a document, that you may as well adopt the approach I suggested in the first place.

    I'm just giving you the benefit of my experience with iTextsharp and the hundreds of questions I get emailed to me about using it to convert html to PDF as a result of the articles I've written on it. It's definitely what I would do.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 29, 2010 3:59 AM

All replies

  • User1520641890 posted

    a couple of options:

    [1] write a recursive method to iterate over everything in Page.Controls (static text is stored in LiteralControl objects), and pick the objects you want to write to PDF.

    [2] get the .aspx page with HttpWebRequest or WebClient, then parse the HTML with iTextSharp HTMLWorker. Or use one of the .NET XML parsers and pick the elements/nodes you want to write to PDF

    Saturday, August 28, 2010 9:28 AM
  • User-1258070753 posted

    can someone post a sample example? it will be an easy too understand how i could do that..

    thank you

    Saturday, August 28, 2010 10:22 AM
  • User-821857111 posted

    The HtmlParser in iTextSharp is not very powerful. There is no easy way to replicate a web page using iTextSharp. I'd recommend spending some time generating a tempate PDF with some place holders for dynamic content which can be added at runtime. Invariably, you do not want the whole page replicated in PDF format anyway (such as headers, footers, menus, adverts etc).


    Saturday, August 28, 2010 1:18 PM
  • User-1852462877 posted

    You can use htmlparser.sourceforge.net/ and then somecode behind like

    		// step 1: creation of a document-object
    Document document = new Document(PageSize.A4, 80, 50, 30, 65);

    // step 2:
    // we create a writer that listens to the document
    // and directs a XML-stream to a file
    PdfWriter.getInstance(document, new FileStream("Chap0707.pdf", FileMode.Create));

    // step 3: we parse the document
    HtmlParser.parse(document, "Chap0702.html");
    ...
    http://www.ujihara.jp/iTextdotNET/en/examples.html

    See Ya!
    Saturday, August 28, 2010 3:31 PM
  • User-1258070753 posted

    thanx Candorz for putting up a sample but it's saying HTML.parser doesn't exist in the current context

    i have already included

    these namespace

    using iTextSharp.text.pdf;
    using iTextSharp.text;
    using iTextSharp.text.html;
    using System.IO;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;



    well Mike that will be an issue if there are menu adverts etc on my page

    but my page simply contains a peice of information in blocks of table

    that it's a normal page which holds information with no menu's advert's etc.

    and i want that page to be converted into pdf format

    thank you

    Sunday, August 29, 2010 3:46 AM
  • User-821857111 posted

    OK, but you will find questions all over the Internet about using iTextSharp to convert an HTML page to PDF, and the answer is invariably the same: it's very difficult. That's because the HTML parser is not robust enough to handle anything other than valid XML, and is not designed as an HTML parser in the sense you would like it to be. Even Bruno Lowagie (the creator of iText) in his book says it has not been designed for that. Some of the paid-for components can do what you want. Others work by firing up the page in a Web browser type control and taking a screenshot. iTextSharp will not play nicely with css either. You will do so much work configuring styles etc within a document, that you may as well adopt the approach I suggested in the first place.

    I'm just giving you the benefit of my experience with iTextsharp and the hundreds of questions I get emailed to me about using it to convert html to PDF as a result of the articles I've written on it. It's definitely what I would do.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 29, 2010 3:59 AM
  • User-1852462877 posted

    If you are only having some Text in the form of Tables than why dont you use Crystal Reports

    Let me give you a demo

    Add a CrystalReport.rpt from Add New Item into your Project

    Now add a Dataset from Add New Item, adding this would really simplify your queries

    Drag and drop the Table into the Dataset as shown in (1): http://cid-416caf1ea5cbdfa9.office.live.com/self.aspx/Public/1.JPG

    Now open the CrystalReport.rpt:

    Into the Database fields ->Right Click->Database Expert->Current connections->Dataset->Select your Tables->Add them as shown in (2): http://cid-416caf1ea5cbdfa9.office.live.com/self.aspx/Public/2.JPG

    Drag and Drop Fields onto the Report from the Database Fields as shown in (3): http://cid-416caf1ea5cbdfa9.office.live.com/self.aspx/Public/3.JPG

    You can also add Text, Background, etc.

    Prepare a Word Document and Import it as an OLE Object as shown in (4): http://cid-416caf1ea5cbdfa9.office.live.com/self.aspx/Public/4.JPG

    Thenafter you can drag and drop fields to appropriate places onto the word Document

    Now to the Page that will display the Report, Add a New ASPX Page and to it, CrystalReport Viewer and CrystalReport Source, the form code will look something like:

    <form id="form1" runat="server">
    <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
    AutoDataBind="True" EnableDatabaseLogonPrompt="False" Height="1039px"
    ReportSourceID="CrystalReportSource1" Width="901px" />
    <CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
    <Report FileName="CrystalReport.rpt">
    </Report>
    </CR:CrystalReportSource>
    </form>


    Add something like this in your code behind on the page_load:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim reportDocumentObj As New ReportDocument
    reportDocumentObj.Load(Server.MapPath("~/CrystalReport.rpt"))
    Dim dt As New DataSet1.usrDataTable
    Dim da As New DataSet1TableAdapters.usrTableAdapter
    'Dim dr As dematFormDataset.tblnewDematRow = Session("currentrow")
    da.Fill(dt)
    reportDocumentObj.SetDataSource(CType(dt, DataTable))
    CrystalReportViewer1.ReportSource = reportDocumentObj

    End Sub


    Run the ASPX page, it will look like as in (5):

    http://cid-416caf1ea5cbdfa9.office.live.com/self.aspx/Public/5.JPG



    See Ya!

    Sunday, August 29, 2010 7:12 AM