我正在尝试将以下HTML导出为带有ITextSharp的PDF,但无论我尝试多少种方式,边框都不显示。
HTML显示底部边框,但一旦导出到PDF中,它就会在翻译中丢失。我可以伪造底部边界的任何想法或方法吗?
由于
HTML:
<Table >
<tr>
<td class="test123">Test</td>
<td>Test</td>
</tr>
<tr>
<td>Test</td>
<td>Test</td>
</tr>
<tr>
<td>Test</td>
<td>Test</td>
</tr>
<tr>
<td>Test</td>
<td>Test</td>
</tr>
<tr>
<td>Test</td>
<td>Test</td>
</tr>
</Table>
C#:
string htmWrite = File.ReadAllText(Server.MapPath("~/Sample/Test.html"));
Document document = new Document();
StyleSheet css = new StyleSheet();
css.LoadStyle("test123", HtmlTags.HR, "1");
css.LoadStyle("test123", "border-bottom-style", "solid");
css.LoadStyle("test123", "border-bottom-width", "2px");
PdfWriter.GetInstance(document, new FileStream(@"C:\test\MySamplePDF.pdf", FileMode.Create));
document.Open();
iTextSharp.text.html.simpleparser.HTMLWorker hw =
new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.SetStyleSheet(css);
hw.Parse(new StringReader(htmWrite));
document.Close();
答案 0 :(得分:2)
使用XMLWorkerHelper。 边框应根据需要在HTML文件中正确设置。 XMLWorkerHelper自动处理应用该样式。对于底部边框使用如下:
border-bottom-color: Black;
border-bottom-width : 1px;
border-bottom-style : solid;
答案 1 :(得分:1)
使用XMLWorkerHelper的解决方法
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"C:\test\MySamplePDF.pdf", FileMode.Create));
document.Open();
TextReader r = new StreamReader(Server.MapPath("~/Sample/Test.html"));
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, r);document.Close();