Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

itext: Change font size of XMLWorker-parsed element

I'm adding Elements from an ElementList to a PdfPCell. These elements can be anything from simple text phrases to bulletpoint lists. However, the font with which these elements are printed to the pdf is too big size. So, my question is: How to adjust the default font size for elements which are parsed to the pdf.

I know how to do it with paragrhaps, where you can enter the string and the font as parameters, but how to do that when adding elements to the cell as follows:

PdfPCell cell = new PdfPCell();
for (Element e : XMLWorkerHelper.parseToElementList(HTML, CSS)) {   
    cell.addElement(e);
}
table.addCell(cell);
like image 597
Steve Waters Avatar asked Feb 02 '26 16:02

Steve Waters


1 Answers

I found at least one way to do it by getting the chunks out of each element and setting the font for that chunk. ie:

Font f = new Font();
f.setSize(8);

for (Element e : XMLWorkerHelper.parseToElementList(content, null)) {
    for (Chunk c : e.getChunks()) {
        c.setFont(f);
    }
    cell.addElement(e);
}    
    table.addCell(cell); 
}
like image 87
Steve Waters Avatar answered Feb 04 '26 06:02

Steve Waters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!