Java 为什么我用ApachePDFBox创建的pdf没有';不整合新线路?

Java 为什么我用ApachePDFBox创建的pdf没有';不整合新线路?,java,pdf,pdfbox,Java,Pdf,Pdfbox,我想创建一个多行pdf文档。 我正在使用Apache的PdfBox 我有一个简单的代码: PDDocument document = new PDDocument(); PDPage page = new PDPage(); document.addPage(page); // Retrieving the pages of the document PDPageContentStream contentStrea

我想创建一个多行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(); 
我只得到这样一句简单的话: “呜呜呜呜”

谁能帮帮我吗


谢谢

我想你忘了使用setLeading了吧?在使用
contentStream.newLine()
之前,您需要使用
contentStream.setLeading(float)

(此处主要来源:)

我已编辑了代码的相关部分:

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

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

我想你忘了使用setLeading了?在使用
contentStream.newLine()
之前,您需要使用
contentStream.setLeading(float)

(此处主要来源:)

我已编辑了代码的相关部分:

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

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

你没有领先。因此,blablas应该互相覆盖,而不是互相跟随。@jozinho22如果答案有帮助(我认为是的),请单击复选标记使其成为可接受的答案。如果没有帮助,请在评论中说明。您没有设置引导。因此,blablas应该互相覆盖,而不是互相跟随。@jozinho22如果答案有帮助(我认为是的),请单击复选标记使其成为可接受的答案。如果没有帮助,请在评论中说明。