iTextPdf HTML to PDF : how to render HTML at speci

2019-06-03 10:05发布

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.

1条回答
欢心
2楼-- · 2019-06-03 10:51

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.

查看更多
登录 后发表回答