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>
"In this Asp.net tutorial Blog we are
covering all the most important C# concepts.<br />
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.
"<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();
}
}
}
}
Other Asp.net Related Post :
- Dynamically create data table and bind
- Example of Templatefield in gridview
- How to fill data into drop down list by Sql.
- Show grid view row details in to tooltip.
- How to Bind Gridview Form database.
- Introduction of Asp.net grid view Control.
- How to use asp ListView control in asp.net:
- how to use Captcha in asp.net Second post
- how to add captcha in Asp.net programming
- Example of DropDownList inside GridView
- Introduction of Asp.net grid view Control.
- Example of Templatefield in asp.net gridview.
- Show gridview Row Details And Give Example.
- Ckeck box list example using javascript in grid.
- Example of DropDownList inside GridView control
- Example of Crystal report(Crystal_report_in asp.net programming )
- How to Make a HTML Table by C# code in asp.net programming
- Example of C# for Bind Data to asp.net Textbox inside gridview control
- JQuery UI Datepicker (Calendar) with asp.net textbox
- What is the Ajax colorpicker,How to use Ajax colorpicker
- Ajax ColorPickerExtender in ASP.NET Example
- jquery tooltip with css in asp.net web page
- Drag and Drop Sortable Lists using jQueryUI
- jQuery modal dialog with postbacks
- Get the current page url by C#
Comments
Post a Comment