iText 7 - 对齐文本和段落

iText 7 - Justify align text and paragraphs

我正在尝试对齐一段文本,但是使用 iText7 得到的结果不一致

这是我存储在数据库中的文本块:

<p style="text-align: justify;">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>  

这是我的代码:

List<IElement> lst = HtmlConverter.ConvertToElements(dt.Rows[i]["contents"].ToString()).ToList();

for (int j = 0; j < lst.Count; j++)
{
    IBlockElement element = (IBlockElement)lst[j];

    if (dt.Rows[i]["contents"].ToString().StartsWith("<br /><br /><h3 style=color:#0000ff;margin: 0 3px 0 3px;><strong>"))
    {
        contents.SetFontSize(12)
            .SetBold()
            .SetFontColor(ColorConstants.BLUE);
    }
    else if (dt.Rows[i]["contents"].ToString().StartsWith("<h4><strong>- "))
    {
        contents.SetFontSize(10)
            .SetBold()
            .SetFontColor(ColorConstants.BLACK);
    }
    else
    {
        contents.SetTextAlignment(TextAlignment.JUSTIFIED_ALL)
            .SetFontSize(10)
            .SetFontColor(ColorConstants.BLACK);

    }

    element.SetProperty(Property.LEADING, new Leading(Leading.MULTIPLIED, -1f));
    document.Add(element);
}

这是 PDF 输出:

我试过这个question但没有成功

我已经查看了 iText7 文档,但有没有办法做到这一点?

我不确定您的代码示例中的 contents 对象是什么。我假设您是从 HtmlConverter 生成的 IElement 列表中以某种方式获取它的。如果不是,您应该检查一下,因为您在 contents 上设置属性,但将 element 添加到文档中。如果它们不引用同一个对象,contents 上的属性将丢失。

也就是说,HtmlConverter 已经应用了 text-align: justify:

定义的文本对齐方式 属性
String html = "<p style=\"text-align: justify;\">Lorem Ipsum is simply dummy " +
    "text of the printing and typesetting industry. Lorem Ipsum has been the " +
    "industry&#39;s standard dummy text ever since the 1500s, when an unknown " +
    "printer took a galley of type and scrambled it to make a type specimen " +
    "book. It has survived not only five centuries, but also the leap into " +
    "electronic typesetting, remaining essentially unchanged. It was " +
    "popularised in the 1960s with the release of Letraset sheets containing " +
    "Lorem Ipsum passages, and more recently with desktop publishing software " +
    "like Aldus PageMaker including versions of Lorem Ipsum.</p>";

PdfWriter writer = new PdfWriter("SO66970672.pdf");
PdfDocument pdfDoc = new PdfDocument(writer);
Document document = new Document(pdfDoc);

IList<IElement> lst = HtmlConverter.ConvertToElements(html);

for (int j = 0; j < lst.Count; j++)
{
    IBlockElement element = (IBlockElement)lst[j];

    // Text alignment is already applied by HtmlConverter
    //Paragraph contents = (Paragraph)element;
    //contents.SetTextAlignment(TextAlignment.JUSTIFIED_ALL)
    //.SetFontSize(10)
    //.SetFontColor(ColorConstants.BLACK);

    element.SetProperty(Property.LEADING, new Leading(Leading.MULTIPLIED, -1f));
    document.Add(element);
}
document.Close();

生成的 PDF:

contents 上设置属性时(上面代码示例中的注释代码):

为了说明 HtmlConverter 尊重 text-align 属性,这是 HTML 片段中带有 text-align: right 的输出: