iTextPdf HTML到PDF:如何在PDF中的特定位置呈现HTML [英] iTextPdf HTML to PDF : how to render HTML at specific location in PDF
问题描述
我有以下HTML字符串:
< font size =5> < br>< ul>< li>项目符号有帮助< / li>< li>显示关键点< / li>< / ul>< br&
如何在特定位置将HTML转换为PDF?
我在 http://itextpdf.com/sandbox/xmlworker 上查看了一些示例
您的问题不是重复的,但它与这些问题相关:
- 使用iTextSharp 对文本的下划线部分
- 将页面高度调整到内容高度
在这两种情况下,我们将HTML解析为 ElementList
:
ElementList elements = XMLWorkerHelper.parseToElementList(HTML,CSS);
然后我们创建一个 ColumnText
c $ c> ct ,其中我们添加了元素:
{
ct.addElement(element);
}
ct.go();
在第一个问题中,特定位置 AcroForm表单字段:
FieldPosition pos = form.getFieldPositions(Name)。get(0);
我们创建一个 ColumnText
ColumnText ct = new ColumnText(stamper.getOverContent(pos.page));
ct.setSimpleColumn(pos.position);
如果要将HTML呈现到现有PDF,则必须执行类似操作。 / p>
第二个例子有些尴尬,因为我们使用 ColumnText
来确定页面的高度。但是,原则是类似的:
ct = new ColumnText(writer.getDirectContent());
ct.setSimpleColumn(new Rectangle(120,600,240,800));
ct.go();
在这种情况下,我对绝对位置的坐标进行硬编码。它是一个矩形左下角x = 120; y = 600和右上角x = 240; y = 800。
I have below HTML string :
<font size="5">Requirements:</font><br><ul><li>Bullets are helpful</li><li>to display key points</li></ul><br>
How can I render above HTML into PDF at specific location?
I have checked some examples at http://itextpdf.com/sandbox/xmlworker but no where mentioned about this.
Your question isn't a duplicate, but it is related to these questions:
In both cases, we parse the HTML to an ElementList
:
ElementList elements = XMLWorkerHelper.parseToElementList(HTML, CSS);
We then create a ColumnText
object ct
to which we add the elements:
for (Element element : elements) {
ct.addElement(element);
}
ct.go();
In the first question, the specific location is determined by the position of an AcroForm form field:
FieldPosition pos = form.getFieldPositions("Name").get(0);
We create a ColumnText
object like this:
ColumnText ct = new ColumnText(stamper.getOverContent(pos.page));
ct.setSimpleColumn(pos.position);
You'll have to do something similar if you want to render the HTML to an existing PDF.
The second example is somewhat awkward because we use the ColumnText
to determine the height of the page. However, the principle is similar:
ct = new ColumnText(writer.getDirectContent());
ct.setSimpleColumn(new Rectangle(120, 600, 240, 800));
ct.go();
In this case, I hard-coded the coordinates of the absolute position. It's a rectangle with lower-left corner x=120;y=600 and upper-right corner x=240;y=800.
这篇关于iTextPdf HTML到PDF:如何在PDF中的特定位置呈现HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!