简体   繁体   中英

HTML table in PDF using iText

I am parsing some html code using HTMLWorker in Java and then inserting it to PDF using iText. I create document by calling new Document(PageSize.A4, 40, 40, 40, 40); which should specify margin 40px on all sides, but when I insert parsed html code that contains table wider than page, right margin dont work and table reaches right border of page... All margins are ok except the right one.. any suggestions?

relevant code:

Document document = new Document(PageSize.A4, 40, 40, 40, 40);

HashMap<String, Object> map = new HashMap<String, Object>();
map.put(HTMLWorker.FONT_PROVIDER, new MyFontFactory10());

HTMLWorker worker = new HTMLWorker(document);
worker.setProviders(map);

worker.parse(new StringReader(VARIABLE_CONTAINING_HTML_CODE));

It should work. Can you provide info about iText version and how the HTML code looks like. Images can make the table expand beyond margins (it´s not pixels by the way).

Try this code and see if the problem still remains. The table should not expand beyond the page margins. Tables used with HTMLWorker always get a width of 100% of its container.

Document document = new Document(PageSize.A4, 40, 40, 40, 40);         
PdfWriter writer   = PdfWriter.getInstance(document, new FileOutputStream("c:/TEST.pdf"));
document.open();
HTMLWorker worker = new HTMLWorker(document);
String code = "<table border=1><tr><td>Test</td><td>Test</td></tr></table>"; 
worker.parse(new StringReader(code));
document.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM