求救啊。如何用ITextSharp将html转成PDF啊??好几天了。
MemoryStream m = new MemoryStream();Document document = new Document();
HTMLWorker worker = new HTMLWorker(document);
System.Net.WebClient MyWebClient = new System.Net.WebClient();
MyWebClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
Byte[] pageData = MyWebClient.DownloadData("http://localhost/Web/products/itemshow.aspx?espno="+BaseRequest.GetString("espno"));
string pageHtml = System.Text.Encoding.Default.GetString(pageData);
TextReader fileReader = new StringReader(pageHtml);
//Response.Write(pageHtml);
//Response.End();
try {
Response.ContentType = "application/pdf";
Response.AddHeader("Content-disposition", "attachment; filename="+BaseRequest.GetString("espno")+".pdf");
// FileReader fileReader = new FileReader("chinese.html");
PdfWriter.GetInstance(document, m);
//BaseFont bf = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\ARIAL.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//Font font1 = new Font(bf, 12, Font.NORMAL);
document.Open();
StyleSheet styles = new StyleSheet();
styles.LoadTagStyle("td", "face", "Gulim");
styles.LoadTagStyle("td", "encoding", "Identity-H");
styles.LoadTagStyle("td", "leading", "12,0");
styles.LoadTagStyle("body", "face", "SIMHEI");
styles.LoadTagStyle("body", "encoding", "Identity-H");
styles.LoadTagStyle("body", "leading", "12,0");
Hashtable hashpars = new Hashtable();
hashpars.Add("img_baseurl", "http://localhost/Web/products/");
worker.Style = styles;
worker.Inte易做图ceProps = hashpars;
worker.Parse(fileReader);
}
catch (DocumentException ex)
{
Console.Error.WriteLine(ex.StackTrace);
Console.Error.WriteLine(ex.Message);
}
// step 5: Close document
document.Close();
//Response.End();
// // step 6: Write pdf bytes to outputstream
Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
--------------------编程问答-------------------- 我这段代码生成不了啊。。。怎么办啊。。。。
求救啊。。。 --------------------编程问答-------------------- 救命啊。高手啊。。。 --------------------编程问答-------------------- 帮顶 --------------------编程问答-------------------- 谢谢,救命 --------------------编程问答-------------------- 呵呵
I coming --------------------编程问答-------------------- 不晓滴。。UP。。。。。 --------------------编程问答-------------------- 提供思路
读取源HTML文件
byte[] downloadBytes = SomeMethond(url);获取源文件
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
response.AddHeader("Content-Type", "binary/octet-stream");
response.AddHeader("Content-Disposition",
"attachment; filename=" + downloadName + "; size=" + downloadBytes.Length.ToString());
response.Flush();
response.BinaryWrite(downloadBytes);
response.Flush();
response.End(); --------------------编程问答-------------------- 可以不 --------------------编程问答-------------------- 关注... --------------------编程问答-------------------- 关注一下 --------------------编程问答-------------------- 偶也不晓滴 --------------------编程问答-------------------- 不可以。郁闷。
--------------------编程问答-------------------- 还有没有人晓得啊。 --------------------编程问答-------------------- 先把html转成word. 然后处理.
using WebSupergoo.ABCpdf4;
using WebSupergoo.ABCpdf4.Objects;
using WebSupergoo.ABCpdf4.Atoms;
//这个是要生成pdf文档的源文件的地址
string strUrl = "http://localhost/PR/TempFiles/PrintDetail.aspx";
string strFolder = "D:\test_pdf\";
string strPageName = "test.pdf";
Doc doc = new Doc();
doc.Rect.Inset(72,144);
l_intTemp = doc.AddImageUrl(strUrl,true,0,true);
while (true)
{
doc.FrameRect();
if (doc.GetInfo(l_intTemp, "Truncated") != "1")
break;
doc.Page = doc.AddPage();
l_intTemp = doc.AddImageToChain(l_intTemp);
}
for (int i = 1; i <= doc.PageCount; i++)
{
doc.PageNumber = i;
doc.Flatten();
}
doc.Save(strFolder + strPageName);
doc.Clear(); --------------------编程问答-------------------- 不能直接解析HTML到pdf么?
itextsharp里不是有解析的东西么?
ITextSharp.Text.html.易做图parser;
这里面不是有HTMLWorker类么?
他里面有Parse解析功能吧? --------------------编程问答-------------------- 顶啊。。。。 --------------------编程问答-------------------- 顶啊。。。。顶啊。。。。 --------------------编程问答-------------------- 期待结果!
补充:.NET技术 , C#