ASP.NET is an open-source server-side web application framework designed for web development to produce dynamic web pages developed by Microsoft to allow programmers to build dynamic web sites, applications and services.
iTextSharp (now iText) is a library for creating and manipulating PDF files in Java and .NET. iText was written by Bruno Lowagie. The source code was initially distributed as open source under the Mozilla Public License.
In this post, I will show how to convert web page to pdf using
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Pdf.aspx.cs" Inherits="Pdf" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<asp:PlaceHolder ID="PlaceholderPdf" runat="server"></asp:PlaceHolder>
<div>
<table border="1">
<tr>
<td colspan="2">
aspdotnetcodebook
</td>
</tr>
<tr>
<td>
cell1
</td>
<td>
cell2
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblLabel" runat="server" Text="Label Test"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
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();
}
}
60 Comments
Very good job....It helped me a lot..
ReplyDeleteTo simply and fast create pdf file use .rdlc file in VStudio. There is a WYSWIG that help you do it :)
ReplyDeleteHi I am getting following error
ReplyDeleteAny Ideas
The network path was not found.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.IOException: The network path was not found.
Source Error:
Line 52: HTMLWorker obj = new HTMLWorker(document);
Line 53: document.Open();
Line 54: obj.Parse(se);
Line 55: document.Close();
Line 56: ShowPdf(strFileName);
This piece of code is not working verry well tabels are always 100% width and images are not in position.
ReplyDeletesomebody any ideas?
Even I am getting a message and a piece of code is not working. It sstopped at obj.Parse(se);
ReplyDeleteand says Could not find a part of the path 'C:\Images\logo.JPG'.
Please help!!!!
Even I am getting a message and a piece of code is not working. It sstopped at obj.Parse(se);
ReplyDeleteand says Could not find a part of the path 'C:\Images\logo.JPG'.
Please help!!!!
HI
ReplyDeletePlease debug and send me exact error message
HI
ReplyDeletePlease debug and send me exact error message
I am rajesh m Somvanshi
ReplyDeleteI Use iTextSharp.dll for Convert ASPX page in PDF Formate That is Done Successfully But My CSS is Not working in PDF File
Please Help Me....
Hi Rajesh,
ReplyDeleteIf you want to maintain css then you may create table structure manually.For more details check out this link
http://forums.asp.net/t/1433490.aspx
Hi all
ReplyDeleteIf page contain any image control or file then How would we render it to PDF?
Hi GyanPrakash,
ReplyDeleteHave u tried my code?
yes i have tried , it works only simple pages
ReplyDeleteif page contain any images ,it couldnt work.
Ok I will look into this and get back to u soon.
ReplyDeletehi Santhosh,
ReplyDeleteIam getting the following error while runing this sample:
"Access to the path 'C:\' is denied"
The above error raised at "CreatePDFDocument()" method at line "obj.Parse(se); "
Plz help me as soon as posible. its very urgent for me. Any will be highly appriciatable.
Thanks,
kumar
Hi Kumar
ReplyDeleteFrom your error message it is clear that the asp.net user does not have access write to c drive.
Do one thing create a temp folder in c drive and write the newly created pdf to that folder.
string strFileName = @"C:\temp\test.pdf"
Hi Santosh
ReplyDeleteCould you plese suggest me what will be the variable to be used in render method.
I use online service http://www.web2pdfconvert.com/pdf-api.aspx
ReplyDeleteWorks great, I just pass URL of web page which I want to convert using REST and get PDF as stream back. But this solution only works if web pages are accessible from online.
HI Actually your code is very helpfull,But it
ReplyDeletedoesn`t work with images .
it gives error in
obj.Parse(se);
so if there is solution then please tell me .
Hi,
ReplyDeleteis very good,
but one problem itextsharp no found with style,
for example
width:500px
HI Actually your code is very helpfull,But it
ReplyDeletedoesn`t work with images .
it gives error in
obj.Parse(se);
so if there is solution then please tell me .
If u want image u need to write server path of image in img tag like,
ReplyDeletesrc="http://localhost:1060/Project/images/image1.jpg" alt="ImageName"
cheers!!!
i m getting error on this line
ReplyDeleteobj.Parse(se);
error is
1.The best overloaded method match for 'iTextSharp.text.html.simpleparser.HTMLWorker.Parse(System.IO.StreamReader)' has some invalid arguments E:\vikas\reporteg1\WebApplication7\Default.aspx.cs 232 14 WebApplication7
2.Argument '1': cannot convert from 'System.IO.StringReader' to 'System.IO.StreamReader' E:\vikas\reporteg1\WebApplication7\Default.aspx.cs 232 24 WebApplication7
can u just tell me what is wrong
Hi,
ReplyDeleteI m Vinay. I am very much thank you for your Code.
Basic html for styling like bgcolor, width ,border=0.5 ,align , font color , size
ReplyDeleteFor image
iTextSharp.text.Image gif = iTextSharp.text.Image.GetInstance((Server.MapPath("/") + "123451.jpg"));
document.Add(gif);
hi i m using ur code in placeholder1 which contain a table.i got this error please help me
ReplyDeleteSystem.InvalidCastException: Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Rectangle'.
Hi i am using ur code in placeholder which contain 5 or 6 big images i got error
ReplyDeleteHi,
ReplyDeleteI'm using the above code.There is image in my page.While executing,it's giving error as "Input string is not in correct format" on line
obj.Parse(se);
can anybody help me to resolve this?
hi..if there are merged cells in grid view then merging disappears in pdf
ReplyDeletei got a itext error ... would u pleas how to solves this name spaces error ..
ReplyDeleteHave you downloaded itextsharp lib
ReplyDeletehi,i have uploaded the word document using the code provided by your blog
ReplyDeletepublic partial class Default5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnRead_Click(object sender, EventArgs e)
{
ApplicationClass wordApp = new ApplicationClass();
string filePath = FileUpload1.PostedFile.FileName;
object file = filePath;
object nullobj = System.Reflection.Missing.Value;
Document doc = wordApp.Documents.Open(ref file,
ref nullobj,
ref nullobj,
ref nullobj,
ref nullobj,
ref nullobj,
ref nullobj,
ref nullobj,
ref nullobj,
ref nullobj,
ref nullobj,
ref nullobj,
ref nullobj,
ref nullobj,
ref nullobj,
ref nullobj);
Document doc1 = wordApp.ActiveDocument;
string m_Content = doc1.Content.Text;
TextBox1.Text = m_Content;
doc.Close(ref nullobj, ref nullobj, ref nullobj);
}
}
it is working fine,i have a requirement that is,after uploading the word document if i make any modifications in that it should be saved,how can do this?
hi , can you let know off how to extract a specific named div . I want to extract export only a part of the page . Thanks for the help
ReplyDeleteHi, thanks for the code. Can you tell me how you are calling the "Render" method, though? Thanks.
ReplyDeleteconvert aspx to pdf. using css design not appear in pdf
ReplyDeletecan u please tell me that what's the use of Render method here...?
ReplyDeletethnxxxx
ReplyDeleteHi Can you please help me i am getting error as Access to the path 'C:\' is denied. at
ReplyDeletehtmlparser.Parse(sr);
below is my code .Please help its urgent
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
ExportText = FormatText(ExportText);
StringReader sr = new StringReader(ExportText);
Document pdfDoc = new Document(PageSize.A4, 20, 20, 50, 40);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
Good blog.....It help me out thanks again:)
ReplyDeleteThank you for this post, this was very helpful to me in my project. Do you have code about how to disallow anyone from copy image in asp.net?
ReplyDeleteInput string was not in a correct format.
ReplyDeleteDescription: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Line 71: iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
Line 72: iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
Line 73: hw.Parse(new StringReader(HTML));
Line 74: document.Close();
Line 75: ShowPdf("Chap0101.pdf");
Source File: e:\Vijith\SaveData\Default2.aspx.cs Line: 73
Stack Trace:
[FormatException: Input string was not in a correct format.]
System.Number.ParseSingle(String value, NumberStyles options, NumberFormatInfo numfmt) +9422867
System.Single.Parse(String s, IFormatProvider provider) +25
iTextSharp.text.html.simpleparser.CellWrapper..ctor(String tag, ChainedProperties chain) +245
iTextSharp.text.html.simpleparser.HTMLWorker.CreateCell(String tag) +56
iTextSharp.text.html.simpleparser.HTMLTagProcessor_TD.StartElement(HTMLWorker worker, String tag, IDictionary`2 attrs) +135
iTextSharp.text.html.simpleparser.HTMLWorker.StartElement(String tag, IDictionary`2 attrs) +127
iTextSharp.text.xml.simpleparser.SimpleXMLParser.ProcessTag(Boolean start) +75
iTextSharp.text.xml.simpleparser.SimpleXMLParser.Go(TextReader reader) +1745
iTextSharp.text.xml.simpleparser.SimpleXMLParser.Parse(ISimpleXMLDocHandler doc, ISimpleXMLDocHandlerComment comment, TextReader r, Boolean html) +83
iTextSharp.text.html.simpleparser.HTMLWorker.Parse(TextReader reader) +59
Default2.HTMLToPdf(String HTML, String FilePath) in e:\Vijith\SaveData\Default2.aspx.cs:73
Default2.Button1_Click1(Object sender, EventArgs e) in e:\Vijith\SaveData\Default2.aspx.cs:36
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
htmlparser.Parse(sr);
ReplyDeletebelow is my code .Please help its urgent
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
ExportText = FormatText(ExportText);
StringReader sr = new StringReader(ExportText);
Document pdfDoc = new Document(PageSize.A4, 20, 20, 50, 40);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
i m getting html parser error,plz suggets
plz suggest thnx
ReplyDeletei am using itextsharp for converting my asp page to pdf...............i am facing css issue as my page is in div structure..............also getting error also but getting error:
Protected Sub btnExport_Click(sender As Object, e As EventArgs)
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
Me.Page.RenderControl(hw)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0.0F)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.[End]()
End Sub
http://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images
using itextsharp dll for converting
htmlparser.Parse(sr)
is not defining proper path for the images:
Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Images\Layout\an-Logo.png'.
Wow muchisimas gracias me sirvio de mucho, funciona perfectamente y es justo lo que estaba buscando. En verdad muchas gracias.
ReplyDeleteThanks for this code May I knwo cam this work for grid view.In my aspx i have label and gridview alos and check box for label control the data is showing properly but for others it won't show any thing please help me n thans in advance I am tarun Nigam my mail id is nigam_tarun2003@yahoo.co.in
ReplyDeleteHi,
ReplyDeleteIam converting aspx page to pdf by using this..while converting iam getting error "Input string is not in correct format "
Can anyone plz help me
I need that my chart must export the CSV file or Excel file so for that i have to write the Function in ASP.NET MVC and I am the Beginner in MVC.
ReplyDeleteHi Thanks for the code .Actually it works fine but in
ReplyDeleteobj.Parse(se);
it gives error as illegal character .i thought HTML Worker would not able to parse some of the character in string reader.
Any Idea.
i have link in gridview whern i click on pdf_click button then error arise in vb.net coade
ReplyDeleteUnable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'.
how can i do
Hi all,will you provide solution to me "If page contain any image control or file then How would we render it to PDF? "
ReplyDeleteThe process cannot access the file 'H:\AESMSFINAL\AESMS 4-08-2013\test.pdf' because it is being used by another process
ReplyDeleteSam, try closing the document instance if there is an exception
DeleteHi...
ReplyDeleteMy Code
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
but error Unable to cast object of type 'iTextSharp.text.html.simpleparser.IncTable' to type 'iTextSharp.text.ITextElementArray'.
pls help
hi,i want to know how can i convert chart data control to pdf in the above code.
ReplyDeleteI want to convert my chart control with all the controls to pdf.I have done the following updation in thw above code.its givving an error.i am not able to resolve it can anybody help me
ReplyDeleteprotected override void Render(HtmlTextWriter writer)
{
MemoryStream mem = new MemoryStream();
Chart1.SaveImage(mem, ChartImageFormat.Png);
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(mem.GetBuffer());
img.ScalePercent(75f);
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");
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();
}
This comment has been removed by the author.
ReplyDeleteVery Simple Article . Thanks
ReplyDeleteThanks a lot for posting this post, Your post has always been an informative source for me.
ReplyDeleteTiming Belt
The author used iTextSharp to convert web pages into PDF files. It is really great. I often browse the pages of related topics and found the SDK of convert pdf to text c#. It also includes the function of html to pdf. It is also very good. Thanks again. The sharing of bloggers.
ReplyDeleteFrom html to pdf, the methods are similar, but when you use it in practice, there are a lot of bugs. This article is great and explains a lot of difficulties.
ReplyDeletePlease do not post any spam link in the comment box😊