I am trying to write on PDF using Spire.PDF with following code. everything works fine except my text is not being aligned justify even though i have specified the text alignment to be justify.
can you please let me know what I'm doing wrong here.
Kindly note that this is part of large code base where we are extracting the text from HTML and print is like plain text.
additional calculations you see here is just to correctly calculate the required space for the text
Please refer to the attached screenshot.
- Code: Select all
string finalBrief = string.Empty;
StringBuilder sb = new StringBuilder();
foreach (string node in briefItemCollection)
{
string nodeText = node.Trim();
if ((!string.IsNullOrEmpty(nodeText)) && (nodeText != "\r\n\t\t"))
{
sb.AppendLine(nodeText.Trim());
}
}
finalBrief = sb.ToString();
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font(defualtFont, 9, FontStyle.Regular));
PdfTrueTypeFont boldfont = new PdfTrueTypeFont(new Font(defualtFont, 9, FontStyle.Bold));
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Justify, PdfVerticalAlignment.Middle);
format.LineSpacing = 10f;
PdfStringLayouter stringLayout = new PdfStringLayouter();
PdfStringLayoutResult result = stringLayout.Layout(finalBrief, font, format, new SizeF(tocPage.GetClientSize().Width - 40, 0));
PdfRGBColor color = new PdfRGBColor(33, 65, 91);
PdfSolidBrush blueBrush = new PdfSolidBrush(color);
PointF position2 = new PointF(xx, y);
foreach (LineInfo line in result.Lines)
{
if (line == result.Lines.First())
{
tocPage.Canvas.DrawString(line.Text, boldfont, blueBrush, position2, format);
}
else
{
tocPage.Canvas.DrawString(line.Text, font, blueBrush, position2, format);
}
position2.Y = position2.Y + result.LineHeight;
y = y + result.LineHeight;
}
I will be much glad if you can let me know how to justify the conent.