PDFsharp & MigraDoc Foundation

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

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Fri Oct 05, 2007 9:19 am 
Offline

Joined: Fri Oct 05, 2007 9:08 am
Posts: 6
Hello, great component, but I have this problem:

I need to draw a number of items in a list, and the list is quite narrow, so the items typically don't fit on a single line. I would like to measure how many lines a text fills when wrapped, but XGraphics.MeasureString does not have an overload that lets me specify maximum line width. It can only tell me the width of the text on a single line, not when broken into multiple lines.

I can do an XGraphics.DrawString and specify a layout rectangle, and that works fine, wrapping the lines perfectly, but afterwards I have no idea how many lines were filled. Thus, I don't know where to start the next list item on the page.

Is there any other way I could do this? Any ideas?

Can anyone please help? Thanks a lot in advance.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Oct 23, 2007 7:24 pm 
Offline

Joined: Tue Oct 23, 2007 7:20 pm
Posts: 2
I've a same problem. Did you measure no of lines. I need to know when the what is next line. Also when string is long.

Thanks,

-mak


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Oct 24, 2007 1:50 am 
Offline

Joined: Fri Oct 05, 2007 9:08 am
Posts: 6
mohali wrote:
I've a same problem. Did you measure no of lines. I need to know when the what is next line. Also when string is long.

Thanks,

-mak


I wrote a method as a workaround, which reliably measures multiline strings within a maximum layout rectangle. I use the primitive MeasureString that we are given, measuring a single line as I gradually add words to the end of it. When it is full within the max line width, I start a new line and start measuring again, and so forth. The height is the number of lines times the font height. Hope this helps.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Oct 24, 2007 1:05 pm 
Offline

Joined: Tue Oct 23, 2007 7:20 pm
Posts: 2
can you give me sample code?

thanks,

rgds,

-mak


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Oct 30, 2007 1:18 am 
Offline

Joined: Fri Oct 05, 2007 9:08 am
Posts: 6
Here you have the code that will do this:
PDFsharp Multiline MeasureString


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sat Dec 08, 2007 2:43 am 
Offline

Joined: Sat Dec 01, 2007 7:22 pm
Posts: 15
Thanks for the code, cmosses.
However there is a problem with that code that causes the method to return the wrong height (it will be off by 1 lineCount.)

The bug is not with the code itself, but rather with the .NET Framework's MeasureString method, which is used by PDFSharp's MeasureString method. The problem is that MeasureString will automatically trim the string being measured, eliminating any spaces. This means that " Hello, world! " will be measured as "Hello, world!". This is not a problem for single-line strings, but for multiple-line strings it may cause incorrect results.

I discovered this recently during a project and I found a cheapo way to resolve it, and that is by using a period character instead of a space, which is approximately the same width (at least with the few fonts that I've tested.) This will result in perfect MeasureString results each time.

If anyone's interested in this code, let me know and I'll post it.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Dec 13, 2007 8:43 am 
Offline

Joined: Fri Oct 05, 2007 9:08 am
Posts: 6
ACS wrote:
Thanks for the code, cmosses.
However there is a problem with that code that causes the method to return the wrong height (it will be off by 1 lineCount.)

The bug is not with the code itself, but rather with the .NET Framework's MeasureString method, which is used by PDFSharp's MeasureString method. The problem is that MeasureString will automatically trim the string being measured, eliminating any spaces. This means that " Hello, world! " will be measured as "Hello, world!". This is not a problem for single-line strings, but for multiple-line strings it may cause incorrect results.

I discovered this recently during a project and I found a cheapo way to resolve it, and that is by using a period character instead of a space, which is approximately the same width (at least with the few fonts that I've tested.) This will result in perfect MeasureString results each time.

If anyone's interested in this code, let me know and I'll post it.


Pass an XStringFormat with the flag XStringFormatFlags.MeasureTrailingSpaces to solve this (I think).


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed May 07, 2008 1:55 pm 
Offline

Joined: Wed May 07, 2008 1:25 pm
Posts: 4
I've noticed a small bug with the multiline MeasureString, it doesn't take into account \n in strings, so the number of lines will be wrong if a new line is in the code.

_________________
My wife the classical singer


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

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 153 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