CONVERT an ASPX page to a PDF file with asp.net c#

Discussion in 'ASP .Net' started by Steven, Aug 27, 2003.

  1. Steven

    Steven Guest

    Hi. I have a dynamic ASPX page written in C# that I want
    to turn into a PDF file and store it for records. Please
    help, because I am not sure where to start. Thank you.
    Steven, Aug 27, 2003
    #1
    1. Advertising

  2. Steven

    Lucas Tam Guest

    "Steven" <> wrote in news:0d1201c36c99$a75e7f50
    $:

    > Hi. I have a dynamic ASPX page written in C# that I want
    > to turn into a PDF file and store it for records. Please
    > help, because I am not sure where to start. Thank you.



    ActivePDF has components which can generate ASPX pgaes.

    Pricy tho.

    --
    Lucas Tam ()
    Please delete "REMOVE" from the e-mail address when replying.
    http://members.ebay.com/aboutme/coolspot18/
    Lucas Tam, Aug 27, 2003
    #2
    1. Advertising

  3. Steven

    florentin Guest

    florentin, May 30, 2007
    #3
  4. Steven

    cmrhema

    Joined:
    Aug 21, 2007
    Messages:
    2
    I tried but it does not work when there are images inside the aspx page
    cmrhema, May 14, 2008
    #4
  5. Steven

    Joined:
    Dec 26, 2008
    Messages:
    3
    Convert .aspx to .PDF

    protected void btn_PDF_Click(object sender, EventArgs e)
    {
    Uri strurl = Request.Url;
    string url = strurl.ToString();
    string text = GetPageText(url);
    string filepath = Server.MapPath("image\\test.htm"); //"c:\\test.htm";
    StreamWriter writer = new StreamWriter(filepath);
    writer.Write(text);
    writer.Close();

    htmltopdf();
    }

    public string GetPageText(string url)
    {
    string htmlText = string.Empty;
    string FILE_NAME = Server.MapPath("image\\test.xml"); //"c:\\test.xml";

    try
    {

    HttpWebRequest requestIP = (HttpWebRequest)WebRequest.Create(url);
    requestIP.Timeout = 10000;
    using (HttpWebResponse responseIP = (HttpWebResponse)requestIP.GetResponse())
    {
    using (Stream streamIP = responseIP.GetResponseStream())
    {
    using (StreamReader readerText = new StreamReader(streamIP))
    {
    htmlText = readerText.ReadToEnd();
    string text = htmlText;

    StreamWriter writer = new StreamWriter( FILE_NAME);
    writer.Write(text);
    writer.Close();
    }
    }
    }
    }
    finally
    {
    }
    return htmlText;
    }


    public void htmltopdf()
    {
    Document doc = new Document();
    PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("image\\test.pdf"), System.IO.FileMode.Create));

    HtmlParser.Parse(doc, Server.MapPath("image\\test.htm"));
    //XmlParser.Parse(doc, Server.MapPath("image\\test.xml"));
    //ITextHandler h = new ITextHandler(doc, new TagMap("c:\\test.xml"));
    //h.Parse("c:\\test.xml");

    if (File.Exists(Server.MapPath("image\\test.htm")))
    File.Delete(Server.MapPath("image\\test.htm"));
    if (File.Exists(Server.MapPath("image\\test.xml")))
    File.Delete(Server.MapPath("image\\test.xml"));
    }


    regards,
    ANKIT CHAMPANERIYA
    (M.SC(IT),MCP,MCTS)
    Software Developer
    Raj s/w ltd
    Mumbai
    , Dec 26, 2008
    #5
  6. Steven

    karabrigham

    Joined:
    Nov 21, 2008
    Messages:
    5
    There is an .net library that exports data to a pdf using .net. but I don't think it supports images.
    datatopdf.com
    karabrigham, Mar 7, 2009
    #6
  7. Steven

    nidhindask88

    Joined:
    Jul 19, 2011
    Messages:
    1
    First add reference to your application (itextsharp.dll)

    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using iTextSharp.text.html;
    using iTextSharp.text.html.simpleparser;

    // Type this code on button's click

    protected void ImgBtnExport_Click(object sender, ImageClickEventArgs e)
    {
    string filename=txtReportName.text;
    string attachment = "attachment; filename=" + filename+ ".pdf";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/pdf";
    StringWriter stw = new StringWriter();
    HtmlTextWriter htextw = new HtmlTextWriter(stw);
    htextw.AddStyleAttribute("font-size", "7pt");
    htextw.AddStyleAttribute("color", "Black");

    Panel_Name.RenderControl(htextw);//Name of the Panel
    Document document = new Document();
    document = new Document(PageSize.A4, 5, 5, 15, 5);
    FontFactory.GetFont("Arial", 50, iTextSharp.text.BaseColor.BLUE);
    PdfWriter.GetInstance(document, Response.OutputStream);
    document.Open();

    StringReader str = new StringReader(stw.ToString());
    HTMLWorker htmlworker = new HTMLWorker(document);
    htmlworker.Parse(str);

    document.Close();
    Response.Write(document);
    }

    //Paste this code some where on page

    public override void VerifyRenderingInServerForm(Control control)
    {

    }

    Regards
    Nidhin Das K (Assyst International).
    nidhindask88, Jul 19, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MadHatter51
    Replies:
    1
    Views:
    847
    Hakan Lambracht
    May 17, 2006
  2. =?Utf-8?B?U3RlcGhhbmU=?=

    How to convert .aspx page to .pdf at runtime

    =?Utf-8?B?U3RlcGhhbmU=?=, May 24, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    1,568
    =?Utf-8?B?U0w=?=
    May 24, 2007
  3. Steve Mauldin
    Replies:
    1
    Views:
    154
    Adrienne
    Jul 28, 2005
  4. Ricardo Pog
    Replies:
    1
    Views:
    396
    Austin Ziegler
    Mar 26, 2008
  5. Sean Nakasone
    Replies:
    1
    Views:
    338
    Farrel Lifson
    Apr 14, 2008
Loading...

Share This Page