iTextPdf HTML to PDF : how to render HTML at specific location in PDF

三世轮回 提交于 2019-12-08 01:59:23
Bruno Lowagie

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!