如何减少段落之间的间距 IText 7?

问题描述

如何使用 "Section 1" 减少 "Alert"IText 7 之间的行距?

enter image description here

这些是存储在数据库表中的值

<h3 style=color:#0000ff;><strong>Section 1</strong></h3>
<h4><strong>- Alert</strong></h4>

我尝试过这些链接没有成功,因为不要更改 "Section 1""Alert" 之间的行距

  1. https://kb.itextpdf.com/home/it7kb/faq/how-to-change-the-line-spacing-of-text
  2. How to adjust spacing between paragraphs in iText7

我的代码如下

if (dt.Rows.Count > 0)
{
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        contents = new Paragraph(dt.Rows[i]["contents"].ToString())
            .SetTextAlignment(TextAlignment.JUSTIFIED)
            .SetFontSize(12)
            .SetMultipliedLeading(0.0f);

        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("<h3 style=color:#0000ff;><strong>Section"))
            {
                contents.SetFontSize(12)
                    .SetBold()
                    .SetFontColor(ColorConstants.BLUE)
                    .SetMultipliedLeading(0.0f);
            }
            else if (dt.Rows[i]["contents"].ToString().StartsWith("<h4><strong>- "))
            {
                contents.SetFontSize(10)
                    .SetBold()
                    .SetFontColor(ColorConstants.BLACK)
                    .SetMultipliedLeading(0.0f);
            }
            else
            {
                contents.SetFontSize(10)
                    .SetFontColor(ColorConstants.BLACK)
                    .SetMultipliedLeading(0.0f);
            }

            document.Add(element);
        }
    }

    dest = filename.ToString();
}

解决方法

您正在从 HTML 字符串创建一个 Paragraph 对象(称为 contents)并向其应用属性,但未将这些对象添加到文档中。您还通过让 HtmlConverter 处理 HTML 字符串来创建元素列表。这些元素被添加到文档中。 因此,在 contents 上设置的所有属性都应该在 PDF 文档中不可见。

您可以简单地依靠 HtmlConverter 来处理 CSS 属性。

String[] htmls = {
    "<h3 style=\"color:#0000ff;\"><strong>Section 1</strong></h3>","<h4><strong>- Alert</strong></h4>"
};

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

for (int i = 0; i < htmls.Length; i++)
{
        IList<IElement> lst = HtmlConverter.ConvertToElements(htmls[i]);
        for (int j = 0; j < lst.Count; j++)
        {
            IBlockElement element = (IBlockElement)lst[j];
            document.Add(element);
        }
}
document.Close();

输出:

PDF output

调整第一个元素的下边距和第二个元素的上边距时:

"<h3 style=\"color:#0000ff;margin-bottom: 0px;\"><strong>Section 1</strong></h3>","<h4 style=\"margin-top: 0px;\"><strong>- Alert</strong></h4>"

输出:

PDF output

如果您更喜欢使用 SetMargin()SetMarginBottom() 等来更改属性而不是 CSS 属性,请确保您在实际添加到文档的对象上执行此操作。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...