Print pdf file in asp.net by C sharp

In Asp.net print web page in pdf Format By C# :


Generate documents and reports based on data from a web file or a web page in asp.net web application.

Asp.net print web page

 In this post we serve dynamically generated or manipulated PDF documents to a web browser

Convert web page in to pdf file:


            Document doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
            string pdfFilePath = Server.MapPath(".") + "/PDFFiles";

            HTMLWorker htmlparser = new HTMLWorker(doc);
            PdfWriter.GetInstance(doc, Response.OutputStream); 

PDF in ASP.NET - C#:


The following code converts a web page using C# ASP.NET .

Print page in pdf:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="pdf.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            font-size: large;
        }
        .style2
        {
            color: #990000;
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <h1 align="center" class="style2">Asp.net Print in PDF File by C#.</h1>
    <div id="print" runat="server" align="center">
     Write here  which you want to print in pdf file ....
     <br />
    Asp.net tutorial
        <br />
        <table>
        <tr>
        <td>
            &quot;In this Asp.net tutorial Blog we are covering all the most important C# concepts.<br />
&nbsp;This tutorial is primarily for new users of this great web and windows technology.
            <br />
            And we recommend you to go through the entire topic, to get the most out of it as possible.
            <br />
            While each Post can be used without reading the Post chapters,
            <br />
            some of them may reference things done in other Post of asp.net.
            &quot;<br />
            View <a href="http://asp-net-by-parijat.blogspot.in/" target="_blank"><span class="style1">
            <strong>Asp.net tutorial("http://asp-net-by-parijat.blogspot.in/) </strong></span></a>.........</td>
        </tr>
        </table>
        <br />
        <br />
        <br />
    </div>
    <div align="center">
   
        <asp:Button ID="Button1" runat="server" Text="Print in pdf formate"
            Height="47px" onclick="Button1_Click"
            style="color: #FFFFFF; font-weight: 700; background-color: #990000"
            Width="157px" />
   
    </div>
    </form>
</body>
</html> 


C# code for pitting in pdf :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using System.IO;
using System.Text.RegularExpressions;
using iTextSharp.text.html.simpleparser;

namespace pdf
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition",                                                   "attachment;filename=webpage_print.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            StringWriter sw = new StringWriter();

            HtmlTextWriter w = new HtmlTextWriter(sw);
            print.RenderControl(w);

            string htmWrite = sw.GetStringBuilder().ToString();
            htmWrite = Regex.Replace(htmWrite, "</?(a|A).*?>", "");
            htmWrite = htmWrite.Replace("\r\n", "");
            StringReader reader = new StringReader(htmWrite);

            Document doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
            string pdfFilePath = Server.MapPath(".") + "/PDFFiles";

            HTMLWorker htmlparser = new HTMLWorker(doc);
            PdfWriter.GetInstance(doc, Response.OutputStream);

            doc.Open();

            try
            {
                htmlparser.Parse(reader);
                doc.Close();
                Response.Write(doc);
                Response.End();
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('" + ex.Message + "');</script>");
            }
            finally
            {
                doc.Close();
            }     
        }
     }
}

 Print pdf file in asp.net by C sharp
 Print in PDF 


Other Asp.net Related Post :


Comments