依赖 iText 5,renderer生成pdf横向
时间: 2023-07-13 19:40:51 浏览: 139
以下是使用iText 5和renderer生成横向PDF的示例代码:
```java
Document document = new Document(new RectangleReadOnly(792, 612));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("horizontal.pdf"));
document.open();
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
String html = "<html><body><p>This is a horizontal PDF document.</p></body></html>";
InputStream is = new ByteArrayInputStream(html.getBytes());
worker.parseXHtml(writer, document, is);
document.setPageSize(new RectangleReadOnly(612, 792));
document.newPage();
html = "<html><body><p>This is the second page.</p></body></html>";
is = new ByteArrayInputStream(html.getBytes());
worker.parseXHtml(writer, document, is);
document.close();
```
在这个示例中,我们首先创建一个大小为792x612的Document对象,并将其写入到文件“horizontal.pdf”中。然后,我们使用XMLWorkerHelper将包含HTML标记的字符串转换为PDF文档。我们添加了一个段落,并使用setPageSize()方法将页面大小更改为612x792。最后,我们在第二页添加另一个段落,并关闭文档。
请注意,我们在第二个页面上添加了一个段落,因为我们已经更改了页面大小,并且默认情况下,iText将添加到当前页面,而不是新页面。
阅读全文
相关推荐












