有没有办法在iText7中的文本之前保留空格?

Fra*_*ken 4 java itext itext7

我在段落对象中添加了以空格开头的Text对象,

但是在iText7(7.0.4)中删除了段落的空白。

看起来像左修剪。这是段落的规范吗?

有什么办法可以在文本前保留空格?

Paragraph p = new Paragraph().add(new Text("  abc")); // Only "abc" appears in pdf
Run Code Online (Sandbox Code Playgroud)

Jor*_*ens 5

iText将修剪空格。
但这不会删除不间断的空格。

File outputFile = new File(System.getProperty("user.home"), "output.pdf");
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outputFile));
Document layoutDocument = new Document(pdfDocument);

layoutDocument.add(new Paragraph("\u00A0\u00A0\u00A0Lorem Ipsum"));
layoutDocument.add(new Paragraph("Lorem Ipsum"));
layoutDocument.close();
Run Code Online (Sandbox Code Playgroud)

  • 没有正确回答所提出的问题。澄清如何添加不间断空格,我将投反对票。 (2认同)
  • NBSP 不能使用,因为它占用空间,但我可以通过在段落前添加一个 NULL 字符来防止左修剪。你的建议对我很有帮助。感谢您的友好回答。<code> Paragraph p = new Paragraph().add("\u0000").add(new Text("abc")); </code> (2认同)

小智 5

请按照步骤:

  1. 扩展 TextRenderer 类
  2. 覆盖trimFirst方法(但不执行任何操作)
  3. 将此类的对象传递给 Text.setTextRenderer 方法