.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Create PDF file from WebPage

Posted By:Sasi Prabhu       Posted Date: July 30, 2011    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

In this articles we will learn how to Convert the WebPage into PDF file in ASP.Net using C#.
 


PDF File :

Easily convert web pages to PDF
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Samples.aspx.cs" Inherits="Pdf" %>




    


    
    
DotNetSpark


To Convert the Webpage into PDF File use the iTextSharp


The namespaces used are

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;


In The Design Page(.aspx) Use your own content.

Example :



In Code Page(.aspx.cs)

To Create PDF Consist of Two Steps:

Step1:

Create a document object

Step2:

Write the Content

Coding :

  public  void CreatePDFDoc(string HtmlPages)
    {
        string sFileName = HttpContext.Current.Server.MapPath("DemoPage.pdf");
        Document document = new Document();
        PdfWriter.GetInstance(document, new FileStream(sFileName, FileMode.Create));
        StringReader sR = new StringReader(HtmlPages);
        HTMLWorker HW = new HTMLWorker(document);
        document.Open();
        HW.Parse(sR);
        document.Close();
        ShowPdfDoc(sFileName);
    }
To Show Pdf :

public void ShowPdfDoc(string PassFileName)
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "inline;filename=" + PassFileName);
        Response.ContentType = "application/pdf";
        Response.WriteFile(PassFileName));
        Response.Flush();
        Response.Clear();
    }
To Convert the Page in Page Render event

protected override void Render(HtmlTextWriter HTwriter)
    {
        MemoryStream MS= new MemoryStream();
        StreamWriter SW = new StreamWriter(MS);
        HtmlTextWriter myWriter = new HtmlTextWriter(SW);
        base.Render(myWriter);
        myWriter.Flush();
        myWriter.Dispose();
        StreamReader SR = new StreamReader(MS);
        SR.BaseStream.Position = 0;
        string pageContent = SR.ReadToEnd();
        SR.Dispose();
        MS.Dispose();
        HTwriter.Write(pageContent);
        CreatePDFDoc(pageContent);
    }



 Subscribe to Articles

     

Further Readings:

Responses
Author: Hannah         Company URL: http://www.dotnetspark.com
Posted Date: February 03, 2014

You can convert HTML page to PDF and vice versa with Aspose.PDF for .NET Library also.

You can find sample code for HTML to PDF and PDF to HTML conversion on the following pages:

http://www.aspose.com/docs/display/pdfnet/HTML%20to%20PDF%20conversion

http://www.aspose.com/docs/display/pdfnet/Convert%20PDF%20File%20into%20HTML%20Format

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend