简体   繁体   English

使用iText的PDF格式的HTML表格

[英]HTML table in PDF using iText

I am parsing some html code using HTMLWorker in Java and then inserting it to PDF using iText. 我正在使用Java中的HTMLWorker解析一些HTML代码,然后使用iText将其插入PDF。 I create document by calling new Document(PageSize.A4, 40, 40, 40, 40); 我通过调用new Document(PageSize.A4, 40, 40, 40, 40);创建文档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? 这应该在所有方面指定边距40px,但是当我插入包含比页面更宽的表格的解析的html代码时,右边距不起作用并且表格到达页面的右边界...除了正确的边缘之外所有边距都可以..任何建议?

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. 您能否提供有关iText版本以及HTML代码外观的信息。 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. 与HTMLWorker一起使用的表总是获得其容器的100%的宽度。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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