C# 如何使用带有样式的html文件使用iTextSharp创建pdf文件
您好,我想在我的ASP.NET应用程序中创建一个pdf,为此我使用C# 如何使用带有样式的html文件使用iTextSharp创建pdf文件,c#,asp.net,html,pdf,itextsharp,C#,Asp.net,Html,Pdf,Itextsharp,您好,我想在我的ASP.NET应用程序中创建一个pdf,为此我使用iTextSharp。我的想法是用HTML文件创建pdf。如果我点击一个按钮,我就会阅读一个带有背景图像和多种样式的HTML文件 我的申请表必须填写 从文件夹App.Data中读取HTML文件。(使用System.IO) 使用此样式从此HTML文件创建PDF!(使用iTextSharp.text; 使用iTextSharp.text.pdf;) 3.以电子邮件形式发送PDF文件。(使用System.Net.Mail;使用Syste
iTextSharp
。我的想法是用HTML文件创建pdf。如果我点击一个按钮
,我就会阅读一个带有背景图像和多种样式的HTML文件
我的申请表必须填写
App.Data
中读取HTML文件。(使用System.IO)使用iTextSharp.text;
使用iTextSharp.text.pdf;
)
3.以电子邮件形式发送PDF文件。(使用System.Net.Mail;使用System.Net.Mime;)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文本:test without styles.您将“style”拼写错了
你应该在这里使用bgcolor而不是背景色这将是样式而不是样式这是一个小错误但背景色不起作用使用正文中的样式标记,不随它一起使用…可能会起作用,试试吧…string html=“Test”?检查一下…我也有同样的问题,如果你得到答案,请发邮件给我。
<body styles="background-color:#E0E0E0;
<body style="background-color:#E0E0E0;