Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i want to save my page in a pdf file how can i do it
Posted
Updated 22-Mar-17 0:02am

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[^]
 
Share this answer
 
.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.
 
Share this answer
 
Comments
Pradeep_kaushik 21-Aug-12 3:53am    
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
 
Share this answer
 
v2
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[^]
 
Share this answer
 
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:

C#
using iTextSharp.text;

using iTextSharp.text.pdf;


4
Call an inbuilt class in iTextSharp and set the StringBuilder to empty:

C#
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:

C#
string strASPXpath = Server.MapPath("MyASPX.aspx");


6
Set the path for the PDF file to build:

C#
string strPDFpath = Server.MapPath("MyPDF.pdf");


7
Call the data from the HTML file and render the file:

C#
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.

C#
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:

C#
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:

C#
{

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.
 
Share this answer
 
v2
Comments
[no name] 21-Aug-12 11:57am    
Step 0: Download and install iTextSharp from where ever it is that you get it from....
Hey hii,
Use below code

C#
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")); 
} 
 
Share this answer
 
v3
Comments
[no name] 21-Aug-12 14:24pm    
And PdfWriter is what exactly?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900