0

我正在尝试使用 XdocReport 将 docx 文件读入 html 并使用 htmlworker 将该 html 转换为 pdf。一切正常,我可以看到最终 pdf 中的样式部分也可以。

唯一的问题是样式标签的内容显示在 pdf 的顶部。我该如何解决这个问题?

仅供参考...我也尝试过 XMLWorker,但它正在生成一个空白的 pdf。

HTML

<html>
<head>
<style>
p{margin-top:0pt;margin-bottom:1pt;}p.Normal{margin-bottom:0.0pt;}span.Normal{font-size:12.0pt;}p.TableGrid{margin-bottom:0.0pt;}span.XDocReport_Hyperlink{color:#0000ff;text-decoration:underline;}p.XDocReport_Heading_1{margin-top:24.0pt;margin-bottom:0.0pt;}span.XDocReport_Heading_1{font-family:'Calibri Light';font-size:14.0pt;font-weight:bold;color:#365f91;}p.XDocReport_Heading_2{margin-top:10.0pt;margin-bottom:0.0pt;}span.XDocReport_Heading_2{font-family:'Calibri Light';font-size:13.0pt;font-weight:bold;color:#4f81bd;}p.XDocReport_Heading_3{margin-top:10.0pt;margin-bottom:0.0pt;}span.XDocReport_Heading_3{font-family:'Calibri Light';font-weight:bold;color:#4f81bd;}p.XDocReport_Heading_4{margin-top:10.0pt;margin-bottom:0.0pt;}span.XDocReport_Heading_4{font-family:'Calibri Light';font-weight:bold;font-style:italic;color:#4f81bd;}p.XDocReport_Heading_5{margin-top:10.0pt;margin-bottom:0.0pt;}span.XDocReport_Heading_5{font-family:'Calibri Light';color:#243f60;}p.XDocReport_Heading_6{margin-top:10.0pt;margin-bottom:0.0pt;}span.XDocReport_Heading_6{font-family:'Calibri Light';font-style:italic;color:#243f60;}
</style>
</head>
<body>
// some content
</body>
</html>

输出 生成的pdf

Itext 版本 - 2.1.7

代码

OutputStream file = new FileOutputStream(new File("C:\\Users\\Desktop\\pp.pdf"));
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, file); 
document.open();
InputStream is = new ByteArrayInputStream(bytes);
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader(s)); // s has the entire html
document.close(); 

再一次,转换后的 pdf 在保留所有格式的情况下看起来不错。唯一的问题是样式内容显示在顶部。

4

0 回答 0