依赖 iText 5,renderer生成pdf横向
时间: 2023-07-13 22:40:51 浏览: 136
以下是使用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将添加到当前页面,而不是新页面。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![java](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)