java - 为什么我用 Apache PdfBox 创建的 pdf 没有集成新行?
问题描述
我想创建一个多行 pdf 文档。我正在使用来自 Apache 的 PdfBox。
我有这个简单的代码:
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
// Retrieving the pages of the document
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.beginText();
contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
contentStream.showText("blabla");
contentStream.newLine();
contentStream.showText("blabla");
contentStream.newLine();
contentStream.showText("blabla");
contentStream.newLine();
我只得到这样一条简单的线:“blablablablablabla”
有人可以帮我吗?
谢谢
解决方案
我想你忘记使用 setLeading 了?使用前contentStream.newLine()
您需要使用contentStream.setLeading(float)
(这里的主要来源:https ://www.javatpoint.com/pdfbox-adding-multiple-lines )
我已经编辑了您的代码的相关部分:
contentStream.beginText();
contentStream.newLineAtOffset(20,600); // set starting position
contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
contentStream.setLeading(14.5f); // set the size of the newline to something reasonable
contentStream.showText("blabla");
contentStream.newLine();
contentStream.showText("blabla");
contentStream.newLine();
contentStream.showText("blabla");
contentStream.newLine();
只要其余代码正常工作(正确关闭和打开文件),在我的机器上运行它会产生多行。
推荐阅读
- java - 通过 BLE 将 GZIP 数据从 Android 发送到 NodeJS
- reactjs - 如何在哑 PrivateRoute 组件中调度操作
- c# - Quartz.net 多久加载一次触发设置?
- anylogic - Anylogic - 更改资源中的变量\参数
- python - 使用带有 API 密钥的 Gmail API 从我的地址发送带有附件的电子邮件
- python - python / pandas中R / tidyverse中的等效“传播”和“聚集”?
- python - 将excel文件导入数据框,导致值发生变化
- spring-boot - 如何使纯文本文件应用程序特定于 Spring 云配置服务器?
- git - 如何在 git 存储库上正确提交生成的文档?
- netlify-cms - Netlify CMS 重建 Config.yml 更改