微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iText 7 - Justify 对齐文本和段落

如何解决iText 7 - Justify 对齐文本和段落

我正在尝试对齐文本块,但是使用 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输出

enter image description here

我试过这个 question 没有成功

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

解决方法

我不确定您的代码示例中的 contents 对象是什么。我假设您是从 IElement 生成的 HtmlConverter 列表中以某种方式获取的。如果没有,您应该检查一下,因为您在 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:

PDF output - justify

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

PDF output - justify all

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

PDF output - align right

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。