嗨我想在我的ASP.NET应用程序中创建pdf并为此使用iTextSharp
。我的想法是从HTML文件中创建PDF。我想,如果我点击一个Button
我读了一个带有背景图像和多种样式的HTML文件。如何使用带有样式的html文件来创建iTextSharp的pdf文件
我的应用程序必须做..
- 从文件夹
App.Data
阅读HTML文件。 (使用System.IO) - 使用此样式创建此HTML文件中的PDF! (
using iTextSharp.text;
) 3.将PDF文件作为电子邮件发送。 (使用System.Net.Mail;采用System.Net.Mime;)
我尝试创建一个PDF,如果可行,但如果我在HTML中使用样式的文件,它不会显示样式:(
这里是我的代码:
string html;
using (StreamReader reader = new StreamReader(Server.MapPath("~/App_Data/mail.html"), System.Text.Encoding.Default))
{
html = reader.ReadToEnd(); //here i read the html file to this string
HTMLtoPdf(html);
}
private void HTMLtoPdf(string HTML)
{
Document doc = new Document();
PdfWriter.GetInstance(doc, new FileStream(Request.PhysicalApplicationPath + "\\test.pdf", FileMode.Create));
doc.Open();
HTMLWorker hw = new HTMLWorker(doc);
hw.Parse(new StringReader(HTML));
doc.Close();
ShowPdf("test.pdf");
}
private void ShowPdf(string s)
{
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=" + s);
Response.ContentType = "application/pdf";
Response.WriteFile(s);
Response.Flush();
Response.Clear();
}
例如我的HTML文件:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html><head>
</head><body styles="background-color:#E0E0E0; font-weight:bold; font-family:Arial; font-size:120%;">
<div>
Test
</div>
</body></html>
在这个例子中我使用的背景颜色,但它不工作:(我只得到了HTML文本:没有样式的测试。
这将是风格不风格 –
是一个小错误,但在背景颜色不工作 – Tarasov
使用样式标签在身体内,不与它一起...可能会工作,试试看... –