Reputation: 497
I'm trying to justify align a block of text, however getting inconsistent results using iText7
Here my block of text stored in a database:
<p style="text-align: justify;">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry'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>
Here is my code:
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);
}
I've trying this question without success
I've checked the iText7 documentation, but is there a way to do this ?
Upvotes: 0
Views: 1319
Reputation: 4871
I'm not sure what the contents
object is in your code sample. I assume you're getting it somehow from the list of IElement
s that's generated by HtmlConverter
. If not, you should review that, because you're setting properties on contents
, but adding element
to the document. If those do not refer to the same object, the properties on contents
will be lost.
That being said, HtmlConverter
will already apply the text alignment property defined by 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'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();
Resulting PDF:
When setting the properties on contents
(commented code in code sample above):
To illustrate that HtmlConverter
respects the text-align
property, this is the output with text-align: right
in the HTML snippet:
Upvotes: 1