PDFBox 重叠文本
PDFBox Overlapped Text
我已经使用 PDFBox 库为 Android 实现了一种在页面上绘制文本的算法。问题是每当我添加新页面时,文本都会重叠,如下图所示。我确定我使用的是 PDPageContentStream.newLine()
方法,但结果与预期不符。
我还漏掉了什么吗?
这是我的代码片段
PDPage page1 = new PDPage();
getInstance().getAnnexe().addPage(page1);
PDPageContentStream contentStream1 = new
PDPageContentStream(getInstance().getAnnexe(), page1, true, true);
contentStream1.beginText();
contentStream1.newLineAtOffset(100F, 650F);
contentStream1.setFont(font, fontSize);
printMultipleLines(subSet, contentStream1);
contentStream1.endText();
contentStream1.close();
这就是printMultipleLines()
方法
private void printMultipleLines(ArrayList<String> lines, PDPageContentStream contentStream) {
try {
for (String line :
lines) {
if (line.length() > 110) {
// Print line as 2 lines
contentStream.showText(line.substring(0, 90));
contentStream.newLine();
contentStream.showText(line.substring(90, line.length()));
} else
// Print line as a whole
contentStream.showText(line);
// Print Carriage Return
contentStream.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
感谢@TilmanHausherr,问题出在 TL 操作员身上。每个新创建的页面都有 TL 等于零用户默认单位。我只需要设置文本前导偏移量。
这是更新后的代码:
PDPage page1 = new PDPage();
getInstance().getAnnexe().addPage(page1);
PDPageContentStream contentStream1 = new
PDPageContentStream(getInstance().getAnnexe(), page1, true, true);
// Set the Text Leading (TL operator) here!!!!
contentStream1.setLeading(12);
contentStream1.beginText();
contentStream1.newLineAtOffset(100F, 650F);
contentStream1.setFont(font, fontSize);
printMultipleLines(subSet, contentStream1);
contentStream1.endText();
contentStream1.close();
感谢@TilmanHausherr 快速准确的回答。
我已经使用 PDFBox 库为 Android 实现了一种在页面上绘制文本的算法。问题是每当我添加新页面时,文本都会重叠,如下图所示。我确定我使用的是 PDPageContentStream.newLine()
方法,但结果与预期不符。
我还漏掉了什么吗?
这是我的代码片段
PDPage page1 = new PDPage();
getInstance().getAnnexe().addPage(page1);
PDPageContentStream contentStream1 = new
PDPageContentStream(getInstance().getAnnexe(), page1, true, true);
contentStream1.beginText();
contentStream1.newLineAtOffset(100F, 650F);
contentStream1.setFont(font, fontSize);
printMultipleLines(subSet, contentStream1);
contentStream1.endText();
contentStream1.close();
这就是printMultipleLines()
方法
private void printMultipleLines(ArrayList<String> lines, PDPageContentStream contentStream) {
try {
for (String line :
lines) {
if (line.length() > 110) {
// Print line as 2 lines
contentStream.showText(line.substring(0, 90));
contentStream.newLine();
contentStream.showText(line.substring(90, line.length()));
} else
// Print line as a whole
contentStream.showText(line);
// Print Carriage Return
contentStream.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
感谢@TilmanHausherr,问题出在 TL 操作员身上。每个新创建的页面都有 TL 等于零用户默认单位。我只需要设置文本前导偏移量。 这是更新后的代码:
PDPage page1 = new PDPage();
getInstance().getAnnexe().addPage(page1);
PDPageContentStream contentStream1 = new
PDPageContentStream(getInstance().getAnnexe(), page1, true, true);
// Set the Text Leading (TL operator) here!!!!
contentStream1.setLeading(12);
contentStream1.beginText();
contentStream1.newLineAtOffset(100F, 650F);
contentStream1.setFont(font, fontSize);
printMultipleLines(subSet, contentStream1);
contentStream1.endText();
contentStream1.close();
感谢@TilmanHausherr 快速准确的回答。