为什么我用Apache PdfBox创建的pdf不集成新行?

问题描述 投票:0回答:1

我想创建多行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”

有人可以帮我吗?

谢谢

java apache pdf pdfbox
1个回答
0
投票

我认为您已经忘记使用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();

在我的计算机上运行此命令会产生多行,只要其余代码可以正常工作(正确关闭和打开文件)。

© www.soinside.com 2019 - 2024. All rights reserved.