Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Sep 21, 2021 7:47 am

Hello,

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.

infra
 
Posts: 18
Joined: Tue Apr 03, 2018 12:05 pm

Tue Sep 21, 2021 9:01 am

Hello,

Thanks for your inquiry.
Please change your code snippet as below to achieve your demand. If there is any question, please feel free to write back.
Code: Select all
 foreach (LineInfo line in result.Lines)
 {
     //Define a rectangle and draw text
     RectangleF rectangle = new RectangleF(position2, new SizeF(tocPage.GetClientSize().Width - 40, result.LineHeight));
     if (line == result.Lines.First())
     {
         tocPage.Canvas.DrawString(line.Text, boldfont, blueBrush, rectangle, format);
     }
     else
     {
         tocPage.Canvas.DrawString(line.Text, font, blueBrush, rectangle, format);
     }
     position2.Y = position2.Y + result.LineHeight;
     y = y + result.LineHeight;
 }

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Tue Sep 21, 2021 12:13 pm

Hello Nina,
thank you for your response. i was able to get my task done with the help of your suggestion. however the last line looks ugly. therefore i did a small amendment to fix that.
i'm sharing below code as a help to anyone who has the same problem. or if you have better suggestion please let me know.

Code: Select all

  PdfStringFormat format_left = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
 foreach (LineInfo line in result.Lines)
                            {
                                if (result.Lines.Count() <= 2)
                                {
                                    if (line == result.Lines.First())
                                    {

                                        tocPage.Canvas.DrawString(line.Text, boldfont, blueBrush, position2, format);
                                    }
                                    else
                                    {
                                        tocPage.Canvas.DrawString(line.Text, font, blueBrush, position2, format);
                                    }

                                }
                                else
                                {
                                    //Define a rectangle and draw text so we can achive the justify effect
                                    RectangleF rectangle = new RectangleF(position2, new SizeF(tocPage.GetClientSize().Width - 40, result.LineHeight));
                                    if (line == result.Lines.First())
                                    {
                                        tocPage.Canvas.DrawString(line.Text, boldfont, blueBrush, rectangle, format);
                                    }
                                    else
                                    {
                                        if (line == result.Lines.Last())
                                        {
                                            tocPage.Canvas.DrawString(line.Text, font, blueBrush, position2, format_left);
                                        }
                                        else
                                        {
                                            tocPage.Canvas.DrawString(line.Text, font, blueBrush, rectangle, format);
                                        }

                                    }
                                }
                                position2.Y = position2.Y + result.LineHeight;
                                y = y + result.LineHeight;
                            }



                            y += 10;


                        }



infra
 
Posts: 18
Joined: Tue Apr 03, 2018 12:05 pm

Wed Sep 22, 2021 1:51 am

Hello,

Thanks for your feedback and your sharing.
Yes, your latest code can indeed make the layout of the text drawn on the PDF file better, even if the text has more than two lines. Your code is very good, we have no other better suggestions for the time being.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.PDF