C# .NET - how to Convert web page into PDF - Asked By Abhi Rana on 27-Oct-09 04:47 AM

Now I able to convert datatable into Pdf file with the help of itextSharp.

Now I want to convert webpage into pdf with few label and gridview


Re - Huggy Bear replied to Abhi Rana on 27-Oct-09 04:53 AM

Use the below function

private void ExportToPdf()

{

    Response.ContentType = "application/pdf";

    Response.AddHeader("content-disposition",

        "attachment;filename=GridViewExport.pdf");

    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    StringWriter sw = new StringWriter();

    HtmlTextWriter hw = new HtmlTextWriter(sw);

    GridView1.AllowPaging = false;

    GridView1.DataBind();

    GridView1.RenderControl(hw);

    StringReader sr = new StringReader(sw.ToString());

    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);

    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

    pdfDoc.Open();

    htmlparser.Parse(sr);

    pdfDoc.Close();

    Response.Write(pdfDoc);

    Response.End(); 

}

Lakshmi s replied to Abhi Rana on 27-Oct-09 04:54 AM
you can use UDC

  1. http://www.print-driver.com/download/ and install Universal Document Converter software onto your computer.

  2. Open the web page in Internet Explorer. In main menu, go to Tools->Internet Options->Advanced. In Printing section check the Print Background colors and images option and press OK.

    Check the Print Background colors and images option and press OK.
  3. To remove page header and footer from output PDF file, in main menu press File->Page Setup. Remove any text from Header and Footer fields and press OK.

    Remove any text from header and footer fields.
  4. Press File->Print... in Internet Explorer main menu.

    Press File-Print... in Internet Explorer main menu.

  5. Select Universal Document Converter from the printers list and press Preferencesbutton.

    Select Universal Document Converter from the printers list and press Preferences button.

  6. On the settings panel, click Load Properties.

    On the settings panel, click Load Properties

  7. Use the Open dialog to select "Web page to PDF.xml" and click Open.

    Use the Open dialog to select "Text document to PDF.xml" and click Open

  8. Press Print button to start converting. When the PDF file is ready, it will be saved to the My Documents\UDC Output Files folder by default.

    Converting in progress.

  9. Converted document will then be opened in Adobe Acrobat or another viewer associated with PDF files on your computer.

    Converted document in Adobe Acrobat.


how to Convert web page into PDF NOT HOW TO CONVERT GRIDVIEW INTO PDF

Abhi Rana replied to Huggy Bear on 27-Oct-09 05:09 AM
end of post
HTML to PDF - mv ark replied to Abhi Rana on 27-Oct-09 05:12 AM
Try Web2PDF Online ( http://web2.pdfonline.com/getting_started/index.htm ) which is a free service that accepts a URL (the page you want to convert) & generates a PDF file for it.

Alternatively you can try converting it using one of these -
iTextSharp - http://www.codeproject.com/KB/graphics/iTextSharpTutorial.aspx
PDFjet - http://pdfjet.com/net/by-example.html
Report.NET - http://report.sourceforge.net/
Convert web page into PDF - Sagar P replied to Abhi Rana on 27-Oct-09 05:19 AM

The best tool .NET library to convert from http://www.html-to-pdf.net/

PdfConverter pdfConverter = new PdfConverter(); pdfConverter.PdfDocumentOptions.Pdf PageSize = PdfPageSize.A4; pdfConverter.PdfDocumentOptions.Pdf CompressionLevel = PdfCompressionLevel.Normal;
pdfConverter.PdfDocumen tOptions.ShowHeader = true; pdfConverter.PdfDocumentOptions.ShowFooter = true; pdfConverter.PdfDocumentOptions.LeftMargin = 5; pdfConverter.PdfDocumentOptions.RightMargin = 5; pdfConverter.PdfDocumentOptions.TopMargin = 5; pdfConverter.PdfDocumentOptions.BottomMargin = 5; pdfConverter.PdfDocumentOptions.UseMetafileForma t = true; pdfConverter.PdfDocumentOptions.ShowHeader = false;
//pdfConverter.PdfHeaderOptions.HeaderText = "Sample header: " + TxtURL.Text;
//pdfConverter.PdfHeaderOptions.HeaderTextColor = Color.Blue;
//pdfConverter.PdfHeaderOptions.HeaderDescriptionTex t = string.Empty;
//pdfConverter.PdfHeaderOptions.DrawHeaderLine = false;
pdfConverter.PdfFooterOptions.FooterText = "Sample footer: " + TxtURL.Text + ". You can change color, font and other options";
pdfConverter.PdfFooterOptions.FooterTextC olor = Color.Blue; pdfConverter.PdfFooterOptions.DrawFoote rLine = false; pdfConverter.PdfFooterOptions.PageNumberText = "Page"; pdfConverter.PdfFooterOptions.ShowPageNumbe r = true;  pdfConverter.LicenseFilePath = System.IO.Path.Combine(Server.MapPath("~"), "Bin"); byte[] downloadBytes = pdfConverter.GetPdfFromUrlBytes(url);
System.Web.Ht tpResponse response = System.Web.HttpContext.Current.Response;response.C lear();
response.AddHeader("Content-Type", "binary/octet-stream");response.AddHeader("Content-Disposition", "attachment; filename=" + downloadName + "; size=" + downloadBytes.Length.ToString());
response.Flush();
response.BinaryWrite(downloadBytes);
response.Flush ();
response.End();

http://www.codeproject.com/showcase/TallComponents.asp

http://www.websupergoo.com/

CONVERT GRIDVIEW INTO PDF - Sagar P replied to Abhi Rana on 27-Oct-09 05:24 AM

You have to use Crystal Report or any 3rd party tool like pdf.net from tallcomponents to extract the u r datagird data to PDF format.

See these links also;

http://www.dot-net-search.com/export+gridview+to+pdf/

http://www.pdfsharp.com/PDFsharp/index.php?option=com_frontpage&Itemid=1

check this article...

http://www.codeproject.com/KB/graphics/giospdfnetlibrary.aspx

OR

http://geekswithblogs.net/vivek/archive/2006/09/26/92316.aspx

Many times we would like to export our page as an Excel sheet, Word doc or PDF file. This can be simply achieved by changing the ContentType of the Response object and overriding the RenderControl method of the control to be exported:

Page Load()
{
 //bind data to data bound controls and do other stuff

Response.Clear(); //this clears the Response of any headers or previous output
Response.Buffer = true; //make sure that the entire output is rendered simultaneously

///
///Set content type to MS Excel sheet
///Use "application/msword" for MS Word doc files
///"application/pdf" for PDF files
///

Response.ContentType = "application/vnd.ms-excel";
StringWriter stringWriter = new StringWriter(); //System.IO namespace should be used

HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

///
///Render the entire Page control in the HtmlTextWriter object
///We can render individual controls also, like a DataGrid to be
///exported in custom format (excel, word etc)
///
this.RenderControl(htmlTextWriter);
Response.Write(stringWriter.ToString());
Response.End();
} //end page load

For exporting ASP.NET pages as PDF files, you need to know the Adobe PDF specs for generating the PDf correctly. There is already a wonderful component which does the same and its free! See this article along with sample source code on how to export a page to PDF:

http://www.codeproject.com/cs/library/giospdfnetlibrary.asp

http://itextsharp.sourceforge.net/tutorial/index.html

Hope this would be helpful...!

Can we export it with the help of iTextSharp - Abhi Rana replied to Sagar P on 27-Oct-09 05:51 AM
end of post
baby replied to Huggy Bear on 15-Nov-10 08:35 AM
i hav use this coding but i got error in that line

GridView1.RenderControl(hw);

can u plz help  me... how can i clear this error?
Regards,
Hannah mary