如何使用itextsharp将aspx页面转换为多页的pdf文档 [英] how to convert aspx page to pdf document with multiple pages using itextsharp

查看:134
本文介绍了如何使用itextsharp将aspx页面转换为多页的pdf文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用itextsharp我正在将aspx页面转换为pdf文档,但是在pdf文件中每次只创建一个页面,但仍然在aspx页面中存在数据,而这些页面并非在pdf文件中创建。我正在使用C#使用asp.net ,请尽早给出解决方案



private void ConvertPDF()

{

try

{

string path = Server.MapPath(〜/ Img / Student Photos // PDFFiles //);

path = path + objbllpreadm .objELLPreAdmission.User_id +。pdf;



if(File.Exists(path))

{

File.Delete(路径);

}



attchmt = objbllpreadm.objELLPreAdmission.User_id +。pdf;

string filename1 =;

filename1 = objbllpreadm.objELLPreAdmission.User_id +。pdf;



回复。 ContentType =应用程序ion / pdf;

Response.AddHeader(content-disposition,attachment; filename =+ filename1);

Response.Cache.SetCacheability(HttpCacheability.NoCache );

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);



this.Page.RenderControl(hw);

var output = new FileStream(path,FileMode.Create);



StringReader sr = new StringReader(sw.ToString());



iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A3,0f, 10f,10f,0f);





HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

PdfWriter。 GetInstance(pdfDoc,Response.OutputStream);

iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc,output);



pdfDoc.Open();

htmlparser.Parse(sr);



pdfDoc.Close();

Response.Write(pdfDoc);

HttpContext.Current.ApplicationInstance.CompleteRequest();

}

catch(Exception exp )

{

var ErrorMessage = new JavaScriptSerializer()。Serialize(exp.Message.ToString());

ShowAlertMessage(ErrorMessage);

}

}





我用于转换的上述方法aspx页面到pdf文档,代码完全运行,没有错误抛出,运行代码后将下载一个包含2个页面的pdf文件,另外一个文件将在〜/ Img / Student Photos // PDFFiles //文件夹中创建其中包含单页...剩余数据未来

using itextsharp i am converting aspx page to pdf document,but in pdf file only one page is creating every time but still data is there in aspx page which was not created in pdf file.I am working with asp.net using C#,kindly give the solution as early as possible

private void ConvertPDF()
{
try
{
string path = Server.MapPath("~/Img/Student Photos//PDFFiles//");
path = path + objbllpreadm.objELLPreAdmission.User_id + ".pdf";

if (File.Exists(path))
{
File.Delete(path);
}

attchmt = objbllpreadm.objELLPreAdmission.User_id + ".pdf";
string filename1 = "";
filename1 = objbllpreadm.objELLPreAdmission.User_id + ".pdf";

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + filename1);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);

this.Page.RenderControl(hw);
var output = new FileStream(path, FileMode.Create);

StringReader sr = new StringReader(sw.ToString());

iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A3, 0f, 10f, 10f, 0f);


HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, output);

pdfDoc.Open();
htmlparser.Parse(sr);

pdfDoc.Close();
Response.Write(pdfDoc);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception exp)
{
var ErrorMessage = new JavaScriptSerializer().Serialize(exp.Message.ToString());
ShowAlertMessage(ErrorMessage);
}
}


the above method i am using for converting the aspx page to pdf document,the code completely running,no errors throwing,after running the code one pdf file will download which contains 2 pages and one more file will create in "~/Img/Student Photos//PDFFiles//" folder which contains single page...remaining data not coming

推荐答案

您好,



请参阅以下内容链接。



http://www.dotnetfunda.com/articles/show/ 2326 / converting-web-page-to-pdf-using-itextsharp [ ^ ]



http://www.aspsnippets.com/Articles/Export-ASPNet-Web-Page-with -images-to-PDF-using-ITextsharp.aspx [ ^ ]



希望这会有所帮助。



问候,

Praneet Nadkar
Hi,

Please see the following links.

http://www.dotnetfunda.com/articles/show/2326/converting-web-page-to-pdf-using-itextsharp[^]

http://www.aspsnippets.com/Articles/Export-ASPNet-Web-Page-with-images-to-PDF-using-ITextsharp.aspx[^]

Hope this helps.

Regards,
Praneet Nadkar


这篇关于如何使用itextsharp将aspx页面转换为多页的pdf文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆