请问有谁做过用itextsharp实现aspx(html)转pdf的
该如何实现啊,下了最新的4.0.5,相关例子chap0706官网提供的没有了,谁知道怎么实现,最好有例子.网上有大量求助的,就是没有一个正经能解决的,望高手告之! --------------------编程问答-------------------- 没做过,楼主真可怜,都没人回帖 --------------------编程问答-------------------- 我是lz,用HtmlParser.Parse(document,"d:\\a.htm");可以,但是如果导出的PDF文件不能识别中文;
如果要支持中文
BaseFont bfHei = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\STFANGSO.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font font = new iTextSharp.text.Font(bfHei, 12);
PdfPTable table = new PdfPTable(3);
table.AddCell(new Paragraph("asda撒大苏打上", font));
document.Add(table);
这样可以,但是我的html很复杂,这样去写会累死人的,谁有类似HtmlParser.Parse的中文解决方案.
高人们,请伸出你们的援手. --------------------编程问答-------------------- using System;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Text;
using System.IO;
using MacIntfc.CF.AppBase.Shr.ValueObject;
using MacIntfc.CF.Component.Web;
using MacIntfc.CF.AppBase.Shr.Exceptions;
using iTextSharp.text;
using iTextSharp.text.pdf;
using TRANSCOSMOS.DM.ValueObject;
namespace TRANSCOSMOS.DM.ValueObject
{
public class PDF_Print_Out
{
// * FONTをセットする
private const string T_FONT = "textfont";
// the name of the Text in the Temp
// 普通だFont
private string[] FIED_NM_OHOR ={
"DMD_ADRSE_ZIP", "DMD_ADRSE_ADDR",
"CO_NM","DMD_ADRSE_POST_NM",
"DMD_ADRSE_MNGP_NM","DMD_ADRSE_CHRG_NM",
"BILL_NLT_DT","ORDER_CODE",
"DMD_DATE","APPLICATION_ID",
"PRT_ADDR_NM","ORG_NM",
"PRT_P_NO","PRT_F_NO",
"EIGYO_EMP_NM","SERVICE_START_DATE",
"SERVICE_END_DATE","PAY_METHOD",
"JYUTYU_KINGAKU","INSTALLMENT_COUNT",
"DMD_COUNT","MATTER_NAME",
"DMD_TOTAL","DETAIL_REMARKS_COLUMN",
"EMP_KJ_L_NM"
};
private HttpServerUtility Server = null;
// * TEMPLATE_NM 社員名簿PDFのTEMPLATE名
private String TEMPLATE_NM = null;
public PDF_Print_Out(HttpServerUtility server)
{
this.Server = server;
this.TEMPLATE_NM = Server.MapPath("~/pdf_out_prict.pdf");
}
/// <summary>
/// Create PDF File
/// </summary>
/// <param name="values"></param>
public void Creat_PDF_File(HttpResponse Response, string[] values)
{
this.createPDF_OHOR(Response, FIED_NM_OHOR, values);
}
private void createPDF_OHOR(HttpResponse Response, string[] FIELD_NM, string[] values)
{
PdfReader reader = null;
PdfStamper stamp = null;
Stream file = null;
string sname = DateTime.Now.ToString("yyMMddhhmmss");
Random Rand = new Random(4);
sname = sname + Convert.ToString(Rand.Next(100)) + ".pdf";
try
{
reader = new PdfReader(this.TEMPLATE_NM);
file = new FileStream(@"D:\" + sname, FileMode.OpenOrCreate);
stamp = new PdfStamper(reader, file);
BaseFont baseFont = this.getFont();
AcroFields form = stamp.AcroFields;
//cb = stamp.GetOverContent(1);
for (int i = 0; i < values.Length; i++)
{
form.SetFieldProperty(FIELD_NM[i], T_FONT, baseFont, null);
form.SetField(FIELD_NM[i], values[i]);
}
stamp.FormFlattening = true;
stamp.Close();
long FileSize;
FileStream PDFfile = new FileStream(@"D:\" + sname, FileMode.Open);
FileSize = PDFfile.Length;
byte[] buffer = new byte[(int)FileSize];
PDFfile.Read(buffer, 0, (int)FileSize);
PDFfile.Close();
//File.Delete(@"D:\WINSOR\PDF_OUT\PDF\PDF_OUT" + sname);
Response.AddHeader("Content-Disposition", "attachment");
Response.ContentType = "application/pdf";
Response.BinaryWrite(buffer);
Response.Flush();
Response.Close();
}
catch (Exception ex)
{
if (stamp != null)
{
stamp.Close();
}
throw ex;
}
finally
{
if (File.Exists(@"D:\" + sname))
{
File.Delete(@"D:\" + sname);
}
}
}
// 普通だ
private BaseFont getFont()
{
BaseFont font = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\msmincho.ttc",BaseFont.IDENTITY_H , BaseFont.EMBEDDED);
return font;
// Return the base font to create the pdf document
}
}
}
--------------------编程问答-------------------- 号称中日韩都可以,楼主试试 --------------------编程问答-------------------- dragonlyf兄,感谢,我试着用html导PDF,目前已经导简单的页还行.可是遇到复杂的页面(大量的css或者js或书写不规范),itext会读文件错误.
有什么好的方法吗?读出这些页面(例子:sina.com),据说itext是通过html-xml-pdf本身html里很多代码是无序的要转xml就会有问题.看了一些商业的解决方案是通过目标网页截图加载进PDF文件.大家讨论下不截图怎么取PDF.有没一个详细点的例子.
我的代码范例如下
Rectangle rect = new Rectangle(PageSize.A4);
rect.BackgroundColor = new Color(238, 238, 238);
Document document = new Document(rect, 50, 50, 50, 50);
try
{
StyleSheet styles = new StyleSheet();
styles.LoadTagStyle("ol", "leading", "16,0");
PdfWriter.GetInstance(document, new FileStream("d:\\html3.pdf", FileMode.Create));
document.Open();
System.Collections.ArrayList objects;
FontFactory.Register("c:\\windows\\fonts\\simhei.ttf");
styles.LoadTagStyle("li", "face", "SIMHEI");
styles.LoadTagStyle("span", "size", "8px");
styles.LoadStyle("sf", "color", "blue");
styles.LoadStyle("sf", "b", "");
styles.LoadStyle("classic", "color", "red");
styles.LoadStyle("classic", "i", "");
styles.LoadTagStyle("td", "face", "SIMHEI");
styles.LoadTagStyle("td", "encoding", "Identity-H");
styles.LoadTagStyle("td", "leading", "18,0");
styles.LoadTagStyle("body", "face", "SIMHEI");
styles.LoadTagStyle("body", "encoding", "Identity-H");
styles.LoadTagStyle("body", "leading", "10,0");
objects = HTMLWorker.ParseToList(new StreamReader("d:\\c.html", Encoding.Default), styles);
for (int k = 0; k < objects.Count; ++k)
document.Add((IElement) objects[k]);
}
catch{}
document.Close(); --------------------编程问答-------------------- dragonlyf兄,类似您和我的方法只能解决简单html,复杂点的页面如sina就不行了.
谁有比较完整的itext解决方案? --------------------编程问答-------------------- mark --------------------编程问答-------------------- mark一下 --------------------编程问答-------------------- mark --------------------编程问答-------------------- 收藏之 --------------------编程问答-------------------- 好东西啊,珍藏了……
--------------------编程问答-------------------- Mark --------------------编程问答-------------------- http://www.codeproject.com/KB/cs/pdfizer.aspx
http://stackoverflow.com/questions/564650/convert-html-to-pdf-in-net --------------------编程问答-------------------- 每天回帖即可获得10分可用分! --------------------编程问答-------------------- 我7月份做过html转pdf的,但是不是用ITextSharp做的,这个不好用,太复杂了。你可以去百度“html页面生成pdf”或者直接进我的博客http://blog.csdn.net/nlx0201/archive/2010/09/15/5885711.aspx里面有专门的介绍 --------------------编程问答--------------------
学习了~谢谢~ --------------------编程问答-------------------- 最近也正在做这方面的研究,貌似ItextSHarp重新封装了itextSharp.xmlWorker --------------------编程问答-------------------- 项目要用到,免费的只能简单html,烦啊
补充:.NET技术 , ASP.NET