我是否必须在 PDFBox 的每一行上设置偏移量或光标?
Do I have to set the offset or cursor on everyline in PDFBox?
我正在尝试学习 PDFBox v2.0,但似乎没有什么有用的示例和教程可以入门。
我想创建一个只有文本的简单 PDF(没有图像和花哨的东西),它看起来如下:
1- Introduction
This is an intro to the document
2- Data
2.1- DataPart1
This is some text here....
2.2- DataPart2
This is another text right here!
3- More Information
Some important informational text here...
我写了下面的代码:
PDPage firstPage = getNewPage();
PDPageContentStream firstContentStream = new PDPageContentStream(document, firstPage);
firstContentStream.setFont(HEADING_FONT, HEADING_FONT_SIZE);
firstContentStream.setNonStrokingColor(Color.BLUE);
firstContentStream.beginText();
firstContentStream.newLineAtOffset(MARGIN, firstPage.getMediaBox().getHeight() - MARGIN);
firstContentStream.showText("1- Introduction");
firstContentStream.endText();
firstContentStream.setFont(TEXT_FONT, TEXT_FONT_SIZE);
firstContentStream.setNonStrokingColor(Color.BLACK);
firstContentStream.beginText();
firstContentStream.showText("This document lists all the impacts that have been observed during the QA validation of the version v3.1 build");
firstContentStream.endText();
firstContentStream.close();
document.addPage(firstPage);
文本"This is the document intro"出现在页面末尾!
我的问题:我是否必须在每一行上设置光标或 offset
???!
这看起来很辛苦。这是否意味着我必须计算字符串宽度并将其与页面宽度进行比较,并在文本填满整行时跳转到新行?
我真的不懂
以及如何让它在当前页面已满时生成新页面?
能否请您举一个简单的例子?提前致谢
我不能做类似的事情吗:
contentStream.addTextOnNewLine("Some text here...");
首先请注意,PDFBox 仅具有非常 low-level 的文本绘图 API,即您直接调用的大多数方法对应于 PDF 内容流中的单个指令。特别是 PDFBox 不带有(可公开访问的)布局功能,但您基本上必须在代码中进行布局。这既是一种自由(你不受现有机器的限制)也是一种负担(你必须自己做所有事情)。
在您的情况下,这意味着:
Does this mean I have to compute the string width and compare it to the page width and jump to new lines whenever the text fills the entire line?
是的。 @Tilman 为您链接给出答案
- How to generate multiple lines in PDF using Apache pdfbox
- Creating a new PDF document using PDFBOX API
并在 Whosebug 上搜索更多内容,有关于该主题的不同变体的进一步答案,例如对于对齐的文本块 here.
但是您的代码中有一个细节,在您的情况下可能会使事情变得比必要的更难:
My question: do I have to set the cursor or the offset on every line???!
虽然您确实必须为每一行 re-position,但如果您仍在同一文本中,则从上一行的开头偏移 re-position 就足够了 object, 即如果您没有调用 endText()
和 beginText()
in-between。例如:
PDPage firstPage = getNewPage();
PDPageContentStream firstContentStream = new PDPageContentStream(document, firstPage);
firstContentStream.setFont(HEADING_FONT, HEADING_FONT_SIZE);
firstContentStream.setNonStrokingColor(Color.BLUE);
firstContentStream.beginText();
firstContentStream.newLineAtOffset(MARGIN, firstPage.getMediaBox().getHeight() - MARGIN);
firstContentStream.showText("1- Introduction");
// removed firstContentStream.endText();
firstContentStream.setFont(TEXT_FONT, TEXT_FONT_SIZE);
firstContentStream.setNonStrokingColor(Color.BLACK);
// removed firstContentStream.beginText();
firstContentStream.newLineAtOffset(0, -TEXT_FONT_SIZE); // reposition, tightly packed text lines
firstContentStream.showText("This document lists all the impacts that have been observed during the QA validation of the version v3.1 build");
firstContentStream.endText();
firstContentStream.close();
document.addPage(firstPage);
newLineAtOffset(0, -TEXT_FONT_SIZE)
将文本插入位置设置为与上一行开头相同的 x 坐标,并降低 TEXT_FONT_SIZE
个单位。
(在此处使用 TEXT_FONT_SIZE
会导致文本行设置得非常紧凑;您可能需要使用更高的值,例如字体大小的 1.4 倍。)
考虑到您的描述,您可能希望在标题后的行中使用正 x 偏移值。
The text "This is the document intro" appears at the end of the page!
这是因为firstContentStream.beginText()
生成的PDF指令将文本插入位置重置为原点(0, 0),默认为左下角。
And also how do I make it generate new pages whenever the current page is full?
好吧,它不会自动发生,当您想要切换到新页面时,您必须明确地这样做。您所做的或多或少与您创建第一页完全一样。
我正在尝试学习 PDFBox v2.0,但似乎没有什么有用的示例和教程可以入门。
我想创建一个只有文本的简单 PDF(没有图像和花哨的东西),它看起来如下:
1- Introduction
This is an intro to the document
2- Data
2.1- DataPart1
This is some text here....
2.2- DataPart2
This is another text right here!
3- More Information
Some important informational text here...
我写了下面的代码:
PDPage firstPage = getNewPage();
PDPageContentStream firstContentStream = new PDPageContentStream(document, firstPage);
firstContentStream.setFont(HEADING_FONT, HEADING_FONT_SIZE);
firstContentStream.setNonStrokingColor(Color.BLUE);
firstContentStream.beginText();
firstContentStream.newLineAtOffset(MARGIN, firstPage.getMediaBox().getHeight() - MARGIN);
firstContentStream.showText("1- Introduction");
firstContentStream.endText();
firstContentStream.setFont(TEXT_FONT, TEXT_FONT_SIZE);
firstContentStream.setNonStrokingColor(Color.BLACK);
firstContentStream.beginText();
firstContentStream.showText("This document lists all the impacts that have been observed during the QA validation of the version v3.1 build");
firstContentStream.endText();
firstContentStream.close();
document.addPage(firstPage);
文本"This is the document intro"出现在页面末尾!
我的问题:我是否必须在每一行上设置光标或 offset
???!
这看起来很辛苦。这是否意味着我必须计算字符串宽度并将其与页面宽度进行比较,并在文本填满整行时跳转到新行?
我真的不懂
以及如何让它在当前页面已满时生成新页面?
能否请您举一个简单的例子?提前致谢
我不能做类似的事情吗:
contentStream.addTextOnNewLine("Some text here...");
首先请注意,PDFBox 仅具有非常 low-level 的文本绘图 API,即您直接调用的大多数方法对应于 PDF 内容流中的单个指令。特别是 PDFBox 不带有(可公开访问的)布局功能,但您基本上必须在代码中进行布局。这既是一种自由(你不受现有机器的限制)也是一种负担(你必须自己做所有事情)。
在您的情况下,这意味着:
Does this mean I have to compute the string width and compare it to the page width and jump to new lines whenever the text fills the entire line?
是的。 @Tilman 为您链接给出答案
- How to generate multiple lines in PDF using Apache pdfbox
- Creating a new PDF document using PDFBOX API
并在 Whosebug 上搜索更多内容,有关于该主题的不同变体的进一步答案,例如对于对齐的文本块 here.
但是您的代码中有一个细节,在您的情况下可能会使事情变得比必要的更难:
My question: do I have to set the cursor or the offset on every line???!
虽然您确实必须为每一行 re-position,但如果您仍在同一文本中,则从上一行的开头偏移 re-position 就足够了 object, 即如果您没有调用 endText()
和 beginText()
in-between。例如:
PDPage firstPage = getNewPage();
PDPageContentStream firstContentStream = new PDPageContentStream(document, firstPage);
firstContentStream.setFont(HEADING_FONT, HEADING_FONT_SIZE);
firstContentStream.setNonStrokingColor(Color.BLUE);
firstContentStream.beginText();
firstContentStream.newLineAtOffset(MARGIN, firstPage.getMediaBox().getHeight() - MARGIN);
firstContentStream.showText("1- Introduction");
// removed firstContentStream.endText();
firstContentStream.setFont(TEXT_FONT, TEXT_FONT_SIZE);
firstContentStream.setNonStrokingColor(Color.BLACK);
// removed firstContentStream.beginText();
firstContentStream.newLineAtOffset(0, -TEXT_FONT_SIZE); // reposition, tightly packed text lines
firstContentStream.showText("This document lists all the impacts that have been observed during the QA validation of the version v3.1 build");
firstContentStream.endText();
firstContentStream.close();
document.addPage(firstPage);
newLineAtOffset(0, -TEXT_FONT_SIZE)
将文本插入位置设置为与上一行开头相同的 x 坐标,并降低 TEXT_FONT_SIZE
个单位。
(在此处使用 TEXT_FONT_SIZE
会导致文本行设置得非常紧凑;您可能需要使用更高的值,例如字体大小的 1.4 倍。)
考虑到您的描述,您可能希望在标题后的行中使用正 x 偏移值。
The text "This is the document intro" appears at the end of the page!
这是因为firstContentStream.beginText()
生成的PDF指令将文本插入位置重置为原点(0, 0),默认为左下角。
And also how do I make it generate new pages whenever the current page is full?
好吧,它不会自动发生,当您想要切换到新页面时,您必须明确地这样做。您所做的或多或少与您创建第一页完全一样。