iText 7 中的行缩进?
Line indentation in iText 7?
我正在开发一个将 txt 文件转换为 pdf 的程序,其中对行缩进进行了大量更改。但是,我无法在 iText 7 中找到可以实现此目的的确切命令。我知道在 iText 5 中有段落对象的 setIndentationLeft()
和 setIndentationRight()
等方法允许显式缩进,但是这在最新版本中不可用。最新版本只提供 setFirstLineIndent()
不能满足我的需求。
这就是我想要实现的目标:
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
如您所见,行以不同的缩进开始。我已经考虑过在行前使用空格,但我发现效率很低。我该如何解决这个问题?
回答我自己的问题以帮助其他有同样问题的人。
有3种方法可以解决这个问题:
1) 由于我想为段落中的每一行设置缩进,您可以将文本行拆分为不同的块,然后分别为其设置缩进。完成后,您可以将其添加到您的文档中。
2) 虽然iText5已经过时了,但它仍然可以用来解决很多问题。如果您使用 Maven 作为构建工具,那么只需将以下代码添加到您的依赖项中,然后您就可以开始使用左右缩进方法了。
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
3) 您还可以使用 table 单元格方法,您可以在给定单元格内添加文本并设置适当的边距,请参见下文。
Table table = new Table(1);
Paragraph right = new Paragraph("This is right, because we create a paragraph with an indentation to the left and as we are adding the paragraph in composite mode, all the properties of the paragraph are preserved.");
right.setMarginLeft(20);
Cell rightCell = new Cell().add(right);
table.addCell(rightCell);
doc.add(table);
这将呈现如下。
我个人不赞成使用第二种方法,但如果它可以解决您的问题,那么没有什么能阻止您。
我正在开发一个将 txt 文件转换为 pdf 的程序,其中对行缩进进行了大量更改。但是,我无法在 iText 7 中找到可以实现此目的的确切命令。我知道在 iText 5 中有段落对象的 setIndentationLeft()
和 setIndentationRight()
等方法允许显式缩进,但是这在最新版本中不可用。最新版本只提供 setFirstLineIndent()
不能满足我的需求。
这就是我想要实现的目标:
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
如您所见,行以不同的缩进开始。我已经考虑过在行前使用空格,但我发现效率很低。我该如何解决这个问题?
回答我自己的问题以帮助其他有同样问题的人。
有3种方法可以解决这个问题:
1) 由于我想为段落中的每一行设置缩进,您可以将文本行拆分为不同的块,然后分别为其设置缩进。完成后,您可以将其添加到您的文档中。
2) 虽然iText5已经过时了,但它仍然可以用来解决很多问题。如果您使用 Maven 作为构建工具,那么只需将以下代码添加到您的依赖项中,然后您就可以开始使用左右缩进方法了。
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
3) 您还可以使用 table 单元格方法,您可以在给定单元格内添加文本并设置适当的边距,请参见下文。
Table table = new Table(1);
Paragraph right = new Paragraph("This is right, because we create a paragraph with an indentation to the left and as we are adding the paragraph in composite mode, all the properties of the paragraph are preserved.");
right.setMarginLeft(20);
Cell rightCell = new Cell().add(right);
table.addCell(rightCell);
doc.add(table);
这将呈现如下。
我个人不赞成使用第二种方法,但如果它可以解决您的问题,那么没有什么能阻止您。