PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 5:12 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Thu Dec 13, 2012 7:50 pm 
Offline

Joined: Thu Dec 13, 2012 6:35 pm
Posts: 1
Hello,

I noticed that the XTextFormatter doesn't stay within the vertical bounds of the XRect container.
This partially pertains to the post: http://forum.pdfsharp.net/viewtopic.php?f=3&t=1039&hilit=xtextformatter
I have been able to fix the issue by copying the class into my project, and making a modification to the code.

I don't know for sure if this is actually an issue, or if I am just doing something wrong.

Some background info pertaining to what I am doing to see the issue.
I have developed an asp.net page with a multiline text box.
The information in this text box gets saved to a SQL database.
I proceed to pull the data from the SQL database to put the info on a PDF document using a XTextFormatter.

Here is the XTextFormatter.DrawString, so that you can see the size of the bounding box used.
Code:
xTF.DrawString(strText, xfontArial12, xbrushBlack, New XRect(100.0, 100.0, 100.0, 100.0), XStringFormats.TopLeft)

What I have observed, is that the text will wrap correctly, and cut off at the end of the XRect container, when the text wraps.
A LOT OF TEXT IN THE TEXT BOX THAT WILL WRAP WITHIN THE BOUNDING CONTAINER. THIS TEXT WILL GET CUT OFF WHEN THE TEXT FILLS THE BOUNDING CONTAINER.
The above text gets cut off at THIS TEXT WILL
When the text has a CRLF in within the data, it will continue outside of the vertical bounds of the text box.
SHORT LINE 1
SHORT LINE 2
SHORT LINE 3
SHORT LINE 4
SHORT LINE 5
SHORT LINE 6
SHORT LINE 7
SHORT LINE 8
SHORT LINE 9

The above grouping of SHORT LINE # should have stopped at line 7, and not shown line 8 and line 9.

After looking through posts, and noticing the above linked post, I looked through the XTextFormatter class and found:
Original Code:
Code:
    void CreateLayout()
    {
      double rectWidth = this.layoutRectangle.width;
      double rectHeight = this.layoutRectangle.height - this.cyAscent - this.cyDescent;
      int firstIndex = 0;
      double x = 0, y = 0;
      int count = this.blocks.Count;
      for (int idx = 0; idx < count; idx++)
      {
        Block block = (Block)this.blocks[idx];
        if (block.Type == BlockType.LineBreak)
        {
          if (Alignment == XParagraphAlignment.Justify)
            ((Block)this.blocks[firstIndex]).Alignment = XParagraphAlignment.Left;
          AlignLine(firstIndex, idx - 1, rectWidth);
          firstIndex = idx + 1;
          x = 0;
          y += this.lineSpace;
        }
        else
        {
          double width = block.Width; //!!!modTHHO 19.11.09 don't add this.spaceWidth here
          if ((x + width <= rectWidth || x == 0) && block.Type != BlockType.LineBreak)
          {
            block.Location = new XPoint(x, y);
            x += width + spaceWidth; //!!!modTHHO 19.11.09 add this.spaceWidth here
          }
          else
          {
            AlignLine(firstIndex, idx - 1, rectWidth);
            firstIndex = idx;
            y += lineSpace;
            if (y > rectHeight)
            {
              block.Stop = true;
              break;
            }
            block.Location = new XPoint(0, y);
            x = width + spaceWidth; //!!!modTHHO 19.11.09 add this.spaceWidth here
          }
        }
      }
      if (firstIndex < count && Alignment != XParagraphAlignment.Justify)
        AlignLine(firstIndex, count - 1, rectWidth);
    }


Modified code:
Code:
    void CreateLayout()
    {
      double rectWidth = this.layoutRectangle.width;
      double rectHeight = this.layoutRectangle.height - this.cyAscent - this.cyDescent;
      int firstIndex = 0;
      double x = 0, y = 0;
      int count = this.blocks.Count;
      for (int idx = 0; idx < count; idx++)
      {
        Block block = (Block)this.blocks[idx];
        if (block.Type == BlockType.LineBreak)
        {
          if (Alignment == XParagraphAlignment.Justify)
            ((Block)this.blocks[firstIndex]).Alignment = XParagraphAlignment.Left;
          AlignLine(firstIndex, idx - 1, rectWidth);
          firstIndex = idx + 1;
          x = 0;
          y += this.lineSpace;
          // MODIFIED  By: Andrew N
          //    Added if statement.
          if (y > rectHeight)
          {
              block.Stop = true;
              break;
          }
        }
        else
        {
          double width = block.Width; //!!!modTHHO 19.11.09 don't add this.spaceWidth here
          if ((x + width <= rectWidth || x == 0) && block.Type != BlockType.LineBreak)
          {
            block.Location = new XPoint(x, y);
            x += width + spaceWidth; //!!!modTHHO 19.11.09 add this.spaceWidth here
          }
          else
          {
            AlignLine(firstIndex, idx - 1, rectWidth);
            firstIndex = idx;
            y += lineSpace;
            if (y > rectHeight)
            {
              block.Stop = true;
              break;
            }
            block.Location = new XPoint(0, y);
            x = width + spaceWidth; //!!!modTHHO 19.11.09 add this.spaceWidth here
          }
        }
      }
      if (firstIndex < count && Alignment != XParagraphAlignment.Justify)
        AlignLine(firstIndex, count - 1, rectWidth);
    }


As seen in the modified code, I added the additional if statement to check against the the vertical bounds of the XRect container.

As previously mentioned, I don't know for sure if this is actually an issue with the XTextFormatter or not.

Sorry for such an extensive post. I figure the more info the better. If you need additional info from me on the above, or screenshots, just ask.

Thank you for your time in looking at this. Any info on this you can provide is appreciated.

Andrew N.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 52 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group