我試圖從下面的HTML生成PDF,即帶有虛線下劃線的文本。 (下面是樣品的實際HTML是大得多)使用iTextSharp將HTML樣式(虛線下劃線)轉換爲PDF
<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);
}
}
謝謝你的更新克里斯。我嘗試過使用不同的HTML標籤,但問題仍然存在。例如
就像我說的,你只能改變表格單元格上的邊框樣式,沒有別的,所以div不會幫你。你需要把它和另一個混合起來(我忘了我甚至寫過)! –