Click here to Skip to main content
Sign Up to vote bad
good
See more: C#ASP.NET
i want to save my page in a pdf file how can i do it
Posted 20 Aug '12 - 21:46


8 solutions

The only way to do this; would be for a user, to have a third-party application that lets them print to a PDF file.
 
Most versions of Adobe will install such a "printer", and there are a number of other products like PDF Complete that work well.
 
Have a look on similar answer: Convert aspx page to PDF[^]
 
..and more similar threads on CP[^]
  Permalink  
you could convert a page (or a control) to PDF using ITEXTSHARP or wkhtmltopdf. both those ways are clearly explained in detail in these articles.
Export GridView Data to PDF using ITEXTSHARP
Export ASP.NET PAGE to PDF using wkhtmltopdf
  Permalink  
.Net does not have a inbuilt PDF support. You need to use a third party component for this. Check out Google[^], there are lot of free/paid components available.
  Permalink  
Comments
Pradeep_kaushik - 21 Aug '12 - 3:53
ok thank u
Try the url
http://csharp-source.net/open-source/pdf-libraries[^]
 
Moreover to save your time, there is already an article with source is provided in codeproject,
 
Tutorials on creating PDF files using C# 2.0[^]
 
chhers
  Permalink  
You need to use iTextSharp.dll which easily converts your page to pdf.
See some code here..
http://www.velocityreviews.com/forums/t63572-convert-an-aspx-page-to-a-pdf-file-with-asp-net-c.html[^]
also see it..
Creating PDF Documents in ASP.NET[^]
  Permalink  
Instructions
1
Open a C# editor.
 
2
Create a C# file and add the following code.
 
3
Use a name space to call the iTextSharp library:
 
using iTextSharp.text;
 
using iTextSharp.text.pdf;
 
4
Call an inbuilt class in iTextSharp and set the StringBuilder to empty:
 
Document document = new Document(PageSize.A4, 80, 50, 30, 65);
 
StringBuilder strData = new StringBuilder(string.Empty);
 
5
Add a Path for the Aspx to be generated from GridView content:
 
string strASPXpath = Server.MapPath("MyASPX.aspx");
 
6
Set the path for the PDF file to build:
 
string strPDFpath = Server.MapPath("MyPDF.pdf");
 
7
Call the data from the HTML file and render the file:
 
StringWriter sw = new StringWriter();
 
sw.WriteLine(Environment.NewLine);
 
sw.WriteLine(Environment.NewLine);
 
sw.WriteLine(Environment.NewLine);
 
sw.WriteLine(Environment.NewLine);
 
HtmlTextWriter htw = new HtmlTextWriter(sw);
 
gvSerchResult.AllowPaging = false;
 
gvSerchResult.AllowSorting = false;
 
BindGridView();
 
gvSerchResult.RenderControl(htw);
 
StreamWriter strWriter = new StreamWriter(strASPXpath, false, Encoding.UTF8);
 
strWriter.Write("<html><head></head><body>" + htw.InnerWriter.ToString() + "</body></html>");
 
strWriter.Close();
 
strWriter.Dispose();
8
Use the parser to convert the ASPX content to a PDF:
 
iTextSharp.text.html.simpleparser.
 
StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
 
styles.LoadTagStyle("ol", "leading", "16,0");
 
PdfWriter.GetInstance(document, new FileStream(strPDFpath, FileMode.Create));
 
document.Open();
9
Set the font styles for the elements on page and add the page items:
 
ArrayList objects;
 
styles.LoadTagStyle("li", "face", "garamond");
 
styles.LoadTagStyle("span", "size", "8px");
 
styles.LoadTagStyle("body", "font-family", "times new roman");
 
styles.LoadTagStyle("body", "font-size", "12px");
 
document.NewPage();
 
objects = iTextSharp.text.html.simpleparser.
 
HTMLWorker.ParseToList(new StreamReader(strASPXpath, Encoding.Default), styles);
 
for (int k = 0; k < objects.Count; k++)
 
{
 
document.Add((IElement)objects[k]);
 
}
10
Clear all the variables used from memory and close:
 
{
 
document.Close();
 
Response.Write(Server.MapPath("~/" + strPDFpath));
 
Response.ClearContent();
 
Response.ClearHeaders();
 
Response.AddHeader("Content-Disposition", "attachment; filename=" + strPDFpath);
 
Response.ContentType = "application/octet-stream";
 
Response.WriteFile(Server.MapPath("~/" + strPDFpath));
 
Response.Flush();
 
Response.Close();
 
if (File.Exists(Server.MapPath("~/" + strPDFpath)))
 
{
 
File.Delete(Server.MapPath("~/" + strPDFpath));
 
}
 
}
11
Run the C# file to create the PDF file from the ASPX file.
  Permalink  
Comments
Wes Aday - 21 Aug '12 - 11:57
Step 0: Download and install iTextSharp from where ever it is that you get it from....
Hey hii,
Use below code
 
protected void lbtn_PDF_Click(object sender, EventArgs e) 
{ 
   Uri strurl = Request.Url; 
   string url = strurl.ToString(); 
   string text = GetPageText(url); 
   string filepath = Server.MapPath("test.html"); 
   StreamWriter writer = new StreamWriter(filepath); 
   writer.Write(text); 
   writer.Close(); 
   htmltopdf(text); 
} 
 
public string GetPageText(string url) 
{ 
   string htmlText = string.Empty; 
   string FILE_NAME = Server.MapPath("test.xml"); 
   try 
   { 
      HttpWebRequest requestIP = (HttpWebRequest)WebRequest.Create(url); 
      CookieContainer cc = new CookieContainer(); 
      requestIP.CookieContainer = cc; 
      requestIP.Timeout = 100000; 
      using (HttpWebResponse responseIP = (HttpWebResponse)requestIP.GetResponse()) 
      { 
         using (Stream streamIP = responseIP.GetResponseStream()) 
         { 
            using (StreamReader readerText = new StreamReader(streamIP)) 
            { 
               htmlText = readerText.ReadToEnd(); 
               string text = htmlText; 
               StreamWriter writer = new StreamWriter(FILE_NAME); 
               writer.Write(text); 
               writer.Close(); 
            } 
         } 
      } 
   } 
   return htmlText; 
} 
 
public void htmltopdf(string strHtml) 
{ 
   Document doc = new Document(); 
   StringWriter sw = new StringWriter(); 
   StringReader sr = new StringReader(sw.ToString()); 
   HTMLWorker HTMLParser = new HTMLWorker(doc); 
   PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("test.pdf"), System.IO.FileMode.Create)); 
   HTMLParser.Parse(sr); 
   if (File.Exists(Server.MapPath("test.htm"))) 
      File.Delete(Server.MapPath("test.htm")); 
   if (File.Exists(Server.MapPath("test.xml"))) 
      File.Delete(Server.MapPath("test.xml")); 
} 
  Permalink  
Comments
Wes Aday - 21 Aug '12 - 14:24
And PdfWriter is what exactly?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 296
1 Mohammed Hameed 183
2 Sergey Alexandrovich Kryukov 143
3 Santhosh G_ 108
4 Mayur_Panchal 98
0 Sergey Alexandrovich Kryukov 8,216
1 OriginalGriff 6,271
2 CPallini 3,528
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 21 Aug 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid