java 如何使用 PDFBox drawString 插入换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7598099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:37:58  来源:igfitidea点击:

How to Insert a Linefeed with PDFBox drawString

javapdfpdf-generationpdfbox

提问by Francesco

I have to make a PDF with a Table. So far it work fine, but now I want to add a wrapping feature. So I need to insert a Linefeed.

我必须用表格制作PDF。到目前为止它工作正常,但现在我想添加一个包装功能。所以我需要插入一个换行符。

contentStream.beginText();  
contentStream.moveTextPositionByAmount(x, y);  
contentStream.drawString("Some text to insert into a table.");  
contentStream.endText();  

I want to add a "\n" before "insert". I tried "\u000A" which is the hex value for linefeed, but Eclipse shows me an error.

我想\n在“插入”之前添加一个“ ”。我试过“ \u000A”,这是换行的十六进制值,但 Eclipse 显示了一个错误。

Is it possible to add linefeed with drawString?

是否可以使用 drawString 添加换行符?

采纳答案by Erik

The pdf format doesn't know line breaks. You have to split the string and move the text position to the next line, using moveTextPositionByAmount.

pdf 格式不知道换行符。您必须使用 moveTextPositionByAmount 拆分字符串并将文本位置移动到下一行。

This is not a special "pdfbox-feature", it is due to the pdf format definition; so there is no way for drawString and there are also no other methods to be called that support linefeeds.

这不是一个特殊的“pdfbox-feature”,这是由于pdf格式定义;所以 drawString 没有办法,也没有其他方法可以调用支持换行。

回答by Lukas

The PDF format allows line breaks, but PDFBox has no build in feature for line breaks.

PDF 格式允许换行,但 PDFBox 没有内置的换行功能。

To use line breaks in PDF you have to define the leading you want to use with the TL-operator. The T*-operator makes a line break. The '-operator writes the given text into the next line. (See PDF-spec for more details, chapter "Text". It′s not that much.)

要在 PDF 中使用换行符,您必须定义要与TL-operator一起使用的行距。-T*运算符进行换行。本'-运算符写入给定文本到下一行。(有关更多详细信息,请参阅 PDF 规范,“文本”一章。并没有那么多。)

Here are two code snippets. Both do the same, but the first snippet uses 'and the second snippet uses T*.

这是两个代码片段。两者都做同样的事情,但第一个片段使用',第二个片段使用T*.

private void printMultipleLines(
    PDPageContentStream contentStream,
    List<String> lines,
    float x,
    float y) throws IOException {
  if (lines.size() == 0) {
    return;
  }
  final int numberOfLines = lines.size();
  final float fontHeight = getFontHeight();

  contentStream.beginText();
  contentStream.appendRawCommands(fontHeight + " TL\n");
  contentStream.moveTextPositionByAmount(x, y);
  contentStream.drawString(lines.get(0));
  for (int i = 1; i < numberOfLines; i++) {
    contentStream.appendRawCommands(escapeString(lines.get(i)));
    contentStream.appendRawCommands(" \'\n");
  }
  contentStream.endText();
}

private String escapeString(String text) throws IOException {
  try {
    COSString string = new COSString(text);
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    string.writePDF(buffer);
    return new String(buffer.toByteArray(), "ISO-8859-1");
  } catch (UnsupportedEncodingException e) {
    // every JVM must know ISO-8859-1
    throw new RuntimeException(e);
  }
}

Use T*for line break:

用于T*换行:

private void printMultipleLines(
    PDPageContentStream contentStream,
    List<String> lines,
    float x,
    float y) throws IOException {
  if (lines.size() == 0) {
    return;
  }
  final int numberOfLines = lines.size();
  final float fontHeight = getFontHeight();

  contentStream.beginText();
  contentStream.appendRawCommands(fontHeight + " TL\n");
  contentStream.moveTextPositionByAmount( x, y);
  for (int i = 0; i < numberOfLines; i++) {
    contentStream.drawString(lines.get(i));
    if (i < numberOfLines - 1) {
      contentStream.appendRawCommands("T*\n");
    }
  }
  contentStream.endText();
}

To get the height of the font you can use this command:

要获取字体的高度,您可以使用以下命令:

fontHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;

You might want to multiply it whit some line pitch factor.

您可能想将它与一些线间距因子相乘。

回答by Ed Staub

Because the PDF model often isn't the best model for the task at hand, it often makes sense to write a wrapper for it that adds support for whatever's "missing" in your case. This is true for both reading and writing.

因为 PDF 模型通常不是手头任务的最佳模型,所以为它编写一个包装器来增加对您案例中“缺失”的任何支持通常是有意义的。这对阅读和写作都是如此。