Click here to Skip to main content
15,888,325 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
How to Convert Whole aspx page with Textbox, label , Button and gridview[All the controls] to pdf.?

Any help would be really appreciated.
Posted
Updated 21-Mar-17 23:45pm
Comments
[no name] 25-Jul-13 8:50am    
It would really help us help you if you told us why the answers to the exact same question you are asking now did not help you. So we do not end up repeating ourselves with solutions that you have tried already and they did not work for you.
vinay.tatipamula 25-Jul-13 8:53am    
refer http://stackoverflow.com/questions/2822843/itextsharp-html-to-pdf
Majesss 3-Jan-17 6:09am    
My solution depends on this C# library for PDF format, it has a capability for straightforward conversion of ASPX pages to PDF files.

1 solution

Their are many solutions to this ...

1. You can print it to a .PDF

2. Using iTextSharp you can use the following code (originally found on http://aspdotnetcodebook.blogspot.com/2009/04/how-to-convert-web-page-to-pdf.html[^]

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text.RegularExpressions;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.xml;
using System.Xml;
using iTextSharp.text.html.simpleparser;
public partial class Pdf : System.Web.UI.Page
{
    protected override void Render(HtmlTextWriter writer)
    {
        MemoryStream mem = new MemoryStream();
        StreamWriter twr = new StreamWriter(mem);
        HtmlTextWriter myWriter = new HtmlTextWriter(twr);
        base.Render(myWriter);
        myWriter.Flush();
        myWriter.Dispose();
        StreamReader strmRdr = new StreamReader(mem);
        strmRdr.BaseStream.Position = 0;
        string pageContent = strmRdr.ReadToEnd();
        strmRdr.Dispose();
        mem.Dispose();
        writer.Write(pageContent);
        CreatePDFDocument(pageContent);


    }
    public  void CreatePDFDocument(string strHtml)
    {

        string strFileName = HttpContext.Current.Server.MapPath("test.pdf");
        // step 1: creation of a document-object
        Document document = new Document();
        // step 2:
        // we create a writer that listens to the document
        PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
        StringReader se = new StringReader(strHtml);
        HTMLWorker obj = new HTMLWorker(document);
        document.Open();
        obj.Parse(se);
        document.Close();
        ShowPdf(strFileName);
     
   
     
    }
    public void ShowPdf(string strFileName)
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
        Response.ContentType = "application/pdf";
        Response.WriteFile(strFileName);
        Response.Flush();
        Response.Clear();
    }
}


3. (The worst way in my opinion) You can save the web pages as a JPEG and create a PDF from those images.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900