CONVERT an ASPX page to a PDF file with asp.net c#

S

Steven

Hi. I have a dynamic ASPX page written in C# that I want
to turn into a PDF file and store it for records. Please
help, because I am not sure where to start. Thank you.
 
L

Lucas Tam

Hi. I have a dynamic ASPX page written in C# that I want
to turn into a PDF file and store it for records. Please
help, because I am not sure where to start. Thank you.


ActivePDF has components which can generate ASPX pgaes.

Pricy tho.
 
Joined
Dec 26, 2008
Messages
3
Reaction score
0
Convert .aspx to .PDF

protected void btn_PDF_Click(object sender, EventArgs e)
{
Uri strurl = Request.Url;
string url = strurl.ToString();
string text = GetPageText(url);
string filepath = Server.MapPath("image\\test.htm"); //"c:\\test.htm";
StreamWriter writer = new StreamWriter(filepath);
writer.Write(text);
writer.Close();

htmltopdf();
}

public string GetPageText(string url)
{
string htmlText = string.Empty;
string FILE_NAME = Server.MapPath("image\\test.xml"); //"c:\\test.xml";

try
{

HttpWebRequest requestIP = (HttpWebRequest)WebRequest.Create(url);
requestIP.Timeout = 10000;
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();
}
}
}
}
finally
{
}
return htmlText;
}


public void htmltopdf()
{
Document doc = new Document();
PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("image\\test.pdf"), System.IO.FileMode.Create));

HtmlParser.Parse(doc, Server.MapPath("image\\test.htm"));
//XmlParser.Parse(doc, Server.MapPath("image\\test.xml"));
//ITextHandler h = new ITextHandler(doc, new TagMap("c:\\test.xml"));
//h.Parse("c:\\test.xml");

if (File.Exists(Server.MapPath("image\\test.htm")))
File.Delete(Server.MapPath("image\\test.htm"));
if (File.Exists(Server.MapPath("image\\test.xml")))
File.Delete(Server.MapPath("image\\test.xml"));
}


regards,
ANKIT CHAMPANERIYA
(M.SC(IT),MCP,MCTS)
Software Developer
Raj s/w ltd
Mumbai
 
Joined
Nov 21, 2008
Messages
5
Reaction score
0
There is an .net library that exports data to a pdf using .net. but I don't think it supports images.
datatopdf.com
 
Joined
Jul 19, 2011
Messages
1
Reaction score
0
First add reference to your application (itextsharp.dll)

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;

// Type this code on button's click

protected void ImgBtnExport_Click(object sender, ImageClickEventArgs e)
{
string filename=txtReportName.text;
string attachment = "attachment; filename=" + filename+ ".pdf";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/pdf";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
htextw.AddStyleAttribute("font-size", "7pt");
htextw.AddStyleAttribute("color", "Black");

Panel_Name.RenderControl(htextw);//Name of the Panel
Document document = new Document();
document = new Document(PageSize.A4, 5, 5, 15, 5);
FontFactory.GetFont("Arial", 50, iTextSharp.text.BaseColor.BLUE);
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();

StringReader str = new StringReader(stw.ToString());
HTMLWorker htmlworker = new HTMLWorker(document);
htmlworker.Parse(str);

document.Close();
Response.Write(document);
}

//Paste this code some where on page

public override void VerifyRenderingInServerForm(Control control)
{

}

Regards
Nidhin Das K (Assyst International).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top