itext7 - 如何减少段落之间的间距 IText 7?
问题描述
如何减少"Section 1"
和"Alert"
使用之间的行距IText 7
?
这些是存储在数据库表中的值
<h3 style=color:#0000ff;><strong>Section 1</strong></h3>
<h4><strong>- Alert</strong></h4>
"Section 1"
我尝试了这些链接但没有成功,因为不改变和之间的行距"Alert"
我的代码如下
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();
输出:
调整第一个元素的下边距和第二个元素的上边距时:
"<h3 style=\"color:#0000ff;margin-bottom: 0px;\"><strong>Section 1</strong></h3>",
"<h4 style=\"margin-top: 0px;\"><strong>- Alert</strong></h4>"
输出:
SetMargin()
如果您更喜欢使用,等来更改属性SetMarginBottom()
,而不是 CSS 属性,请确保您在实际添加到文档中的对象上执行此操作。
推荐阅读
- python - 对元组列表的重复元素求和
- python - 当要求输入然后将该变量导入另一个文件时,原始文件会运行两次
- html - 如何让我的字体显示在我的网站上?
- java - Java - 逐级遍历嵌套的 JSON
- lua - 显示从表中选择的随机对象
- python-3.x - Python 3.9.1 - VSC 调试:没有名为 schedule.__main__ 的模块;'schedule' 是一个包,不能直接执行
- c - 如何在 macOS 上使用 SDL 基本上在窗口上绘图?
- javascript - 在 java、javascript 和 c# 的循环中使用注释是否存在任何性能问题?
- php - 跨数据库加入 Codeigniter
- php - 动态创建 MySQL 表列