Reputation: 497
How can I reduce the line spacing between "Section 1"
and "Alert"
using IText 7
?
These are values stored in the table of database
<h3 style=color:#0000ff;><strong>Section 1</strong></h3>
<h4><strong>- Alert</strong></h4>
I have tried without success these links because don't changing the line spacing between "Section 1"
and "Alert"
My code below
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();
}
Upvotes: 0
Views: 774
Reputation: 4871
You're creating a Paragraph
objects (called contents
) from the HTML strings and applying properties to it, but not adding those objects to the document. You're also creating a List of elements by having HtmlConverter
process the HTML strings. Those elements are added to the documents.
So it's expected that none of the properties that are set on contents
are visible in the PDF document.
You can simply rely on HtmlConverter
to process the CSS properties.
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();
Output:
When adjusting the bottom margin on the first element and the top margin on the second element:
"<h3 style=\"color:#0000ff;margin-bottom: 0px;\"><strong>Section 1</strong></h3>",
"<h4 style=\"margin-top: 0px;\"><strong>- Alert</strong></h4>"
Output:
If you prefer to change the properties using SetMargin()
, SetMarginBottom()
, etc, instead of CSS properties, make sure you're doing that on the objects you're actually adding to the document.
Upvotes: 1