【问题标题】:Line indentation in iText 7?iText 7中的行缩进?
【发布时间】:2019-12-20 23:44:40
【问题描述】:

我正在开发一个将 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 

如您所见,行以不同的缩进开始。我已经考虑过在行前使用空格,但我发现这非常低效。我该如何解决这个问题?

【问题讨论】:

    标签: itext itext7


    【解决方案1】:

    回答我自己的问题以帮助其他有同样问题的人。

    有3种方法可以解决这个问题:

    1) 由于我想为段落中的每一行设置它,您可以将文本行拆分为不同的块,然后单独设置缩进。完成后,您可以将其添加到您的文档中。

    2) 虽然 iText5 已经过时,但它仍然可以用来解决许多问题。如果您使用 Maven 作为构建工具,那么只需将以下代码添加到您的依赖项中,然后您就可以开始使用左右缩进方法了。

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.5.10</version>
    </dependency>
    

    3) 您还可以使用表格单元格方法,您可以在给定单元格中添加文本并设置适当的边距,见下文。

    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);
    

    这将呈现如下。

    我个人不鼓励使用第二种方法,但如果它可以解决您的问题,那么没有什么能阻止您。

    【讨论】:

      猜你喜欢
      • 2023-02-03
      • 2020-09-08
      • 2021-02-02
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-12
      相关资源
      最近更新 更多