gpt4 book ai didi

java - itext 将 xhtml 转换为 pdf

转载 作者:行者123 更新时间:2023-12-04 05:43:50 26 4
gpt4 key购买 nike

我有一个带有文本字段和复选框的 XHTML 文件;当我尝试使用 ITextRenderer 将我的 xhtml 转换为 pdf 时; iText 正在删除表单字段(如 pdf 中的复选框和文本框)。

我的代码看起来

ITextRenderer renderer = new ITextRenderer ();
renderer.setDocument(new File("input.xhtml").toURI().toURL.toString());
renderer.layout();
renderer.createPDF("outputstream");

有人可以提供示例代码片段来使用 iText 处理上述情况吗?我正在使用 iText 5.1.3 版本。

我的 XHTML 看起来像
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>FULL</title>
<style media="screen" type="text/css">
fieldset table { width:98%;border: 1px groove; margin:0 auto; }
fieldset table tr { border: 1px groove; }
fieldset table th { border: 1px groove; }
fieldset table td { border: 1px groove; }
fieldset &gt; table [id=inspectionStatus] thead {border: 1px groove;background:#446BEC;text-align:center;}
fieldset &gt; table [id=inspectionStatus] tr { border: 1px groove; }
fieldset &gt; table [id=inspectionStatus] th { border: 1px groove; }
fieldset &gt; table [id=inspectionStatus] td { border: 1px groove; }
fieldset table thead {border: 1px groove;background:#446BEC;text-align:center;}
fieldset table tbody tr th {text-align:left;background:#C6DEFF;width:28%}
fieldset table tbody tr td {text-align:center} [type=text] {margin-left:5%;} *[type=text]{width:90%;}
fieldset table tbody tr td [type=checkbox] {margin:0 auto;} *[type=checkbox]{width:90%;}
fieldset table caption {font-weight:bold;color:#0840F8;}
fieldset {width:98%;font-weight:bold;border:1px solid #446BEC;}
</style>
</head>
<body>
<fieldset id="PersonTable">
<legend>Person Information</legend>
<table id="PersonDisplay">
<thead>
<tr>
<th>Identifier</th>
<th>Name</th>
<th>Comment</th>
<th>Pass</th>
<th>Fail</th>
</tr>
</thead>
<tbody>
<tr>
<td>1234</td>
<td> William Jones</td>
<td>
<input type="text"/>
</td>
<td>
<input type="checkbox"/>
</td>
<td>
<input type="checkbox"/>
</td>
</tr>
</tbody>
</table>
</fieldset>
</body>
</html>

最佳答案

最简单的方法是使用 pdfHTML。
它是一个 iText7 插件,可将 HTML5 (+CSS3) 转换为 pdf 语法。

代码非常简单:

    HtmlConverter.convertToPdf(
"<b>This text should be written in bold.</b>", // html to be converted
new PdfWriter(
new File("C://users/lsamudrala/output.pdf") // destination file
)
);

要了解更多信息,请转至 http://itextpdf.com/itext7/pdfHTML

关于java - itext 将 xhtml 转换为 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10939237/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com