PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Sep 07, 2024 9:10 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Fri May 21, 2010 4:36 pm 
Offline

Joined: Thu Feb 25, 2010 2:44 pm
Posts: 14
Let's say I'm putting text into a table in a MigraDoc document. The text is going into a column with a fixed width, 2 inches in this case. I would like to be able to measure how high that row will become after the text is formatted (i.e., after word wrapping takes place). Does MigraDoc provide this ability? If the TextMeasurement class accepted a Rectangle (or a max width) in the MeasrureString() method then I would go that route, but it doesn't.


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 30, 2010 10:11 pm 
Offline

Joined: Wed Jun 30, 2010 10:08 pm
Posts: 4
Good question - I'm looking for the answer to a very similar question. Did you ever find an answer?


Top
 Profile  
Reply with quote  
PostPosted: Fri Aug 27, 2010 8:48 pm 
Offline

Joined: Fri Aug 27, 2010 8:31 pm
Posts: 1
Hi Folks,

That's why I created my own Word Wrapping class, so I was able to measure the height of the wrapped textblock. I'm using this class to add text to a table cell, and resize the height of the cell to fit the wrapped content.

Create an instance of the PdfWordWrapper (see attachment) and feed the class with textblocks by calling the Add method. For each call to this method you can supply a different font and/or brush. After all textblocks have been added, call the Process method.

After that you can read the Size property to check the Height of the wrapped text, and use the Draw method to finally draw the output to an XGraphics object.

Example:
Code:
PdfWrodWrapper wrapper = new PdfWordWrapper(g, XUnit.FromMillimeter(100));
wrapper.Add("Title", titleFont, XBrushes.Black);
wrapper.Add("Content sdflm sldkf smdlfk smdflk sdmflksdmflk sdlfm", contentFont, XBrushes.Black);
wrapper.Process();
double height = wrapper.Size.Height;
wrapper.Draw(g, XUnit.FromMillimeter(71), itemTop + XUnit.FromMillimeter(0.5), PdfWrodWrapper.Alignment.Left);


I hope this stuff is of any use :)

Grtz,
Wouter Huysentruit


Attachments:
PdfWordWrapper.zip [1.93 KiB]
Downloaded 734 times
Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 31, 2012 9:30 am 
Offline

Joined: Wed Oct 31, 2012 9:23 am
Posts: 1
Hi all, I faced a bug in the PdfWordWrapper of Wouter Huysentruit:

If you try to put a large string (without spaces) inside a 'too small' area, it gave an exception. I fixed it with next code change in the CreateBlocks function:

Code:
while (blockSize.Width > width)
{
    for (int i = value.Length - 1; i > 0; i--)
    {
        string part = value.Substring(0, i);

        blockSize = graphics.MeasureString(part, font);
        blockSize.Height = lineHeight;

        if (blockSize.Width > width)
            continue;

        blocks.Add(new Block(part, BlockType.Text, font, brush, blockSize, descent));

        value = value.Substring(i, value.Length - i);

        // *** This is the bug fix *** variable 'i' needs to get the value of the new length of the shortened string
        i = value.Length + 1;  //  will be decremented by 1 on next iteration

        if (value.Length == 0)
        {
            blockSize = XSize.Empty;
            break;
         }
    }
}


I hope this fix will help others,

Greetings, Krabbie

Improved readability - Forum Administration


Top
 Profile  
Reply with quote  
PostPosted: Fri Apr 19, 2013 4:58 pm 
Offline
User avatar

Joined: Sun Apr 22, 2012 3:47 pm
Posts: 26
I need to do this with a TextFrame. I need to calculate the height of it based on long a string of data is. The width of the TextFrame is static and doesn't change. Just the height.

In your example you show that you are passing a XGraphic parameter to the constructor of the class. What value does that hold?

_________________
Thanks,
George


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 28 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