【发布时间】:2015-10-01 19:31:33
【问题描述】:
我正在尝试从 HTML 下方生成 PDF,即带有点下划线的文本。 (下面是示例实际 HTML 更大)
<u style="border-bottom: 1px dotted #000;text-decoration: none;"> Hello </u>
如How to convert HTML to PDF using iTextSharp 中所述。输出应该有一条虚线,我可以在 HTML 文件中看到,但是 iTextSharp 生成的 PDF 显示的是正常下划线而不是虚线下划线。这是我的完整方法
public void UsingXMLWorker()
{
Byte[] bytes;
//Create a stream that we can write to, in this case a MemoryStream
using (var ms = new MemoryStream())
{
using (var doc = new Document())
{
//Create a writer that's bound to our PDF abstraction and our stream
using (var writer = PdfWriter.GetInstance(doc, ms))
{
//Open the document for writing
doc.Open();
//sample HTML and CSS
var example_html = @"<u style=""border-bottom: 1px dotted #000;text-decoration: none;"" > Hello </u>";
using (var srHtml = new StringReader(example_html))
{
//Parse the HTML
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
}
//var example_html = @"<u class=""dottedBorder""> Hello </u>";
//var example_css = @".dottedBorder{border-bottom: 1px dotted #000;text-decoration: none;font-size:38px;}";
//using (var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css)))
//{
// using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_html)))
// {
// //Parse the HTML
// iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss);
// }
//}
doc.Close();
}
}
bytes = ms.ToArray();
}
var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
System.IO.File.WriteAllBytes(testFile, bytes);
}
我什至尝试了其他方法,例如下面的代码,但我仍然看到生成的 PDF 带有普通下划线,而不是虚线下划线。我在这里缺少什么?
var example_html = @"<u class=""dottedBorder""> Hello </u>";
var example_css = @".dottedBorder{border-bottom: 1px dotted #000;text-decoration: none;font-size:38px;}";
using (var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css)))
{
using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_html)))
{
//Parse the HTML
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss);
}
}
【问题讨论】:
标签: c# html pdf itextsharp