问题
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:
Your question isn't a duplicate, but it is related to these questions:
- underline portion of text using iTextSharp
- How to adjust the page height to the content height?
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.
来源:https://stackoverflow.com/questions/30651554/itextpdf-html-to-pdf-how-to-render-html-at-specific-location-in-pdf