PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index PDFsharp - moved to http://forum.pdfsharp.net/
Please visit the new PDFsharp forum at http://forum.pdfsharp.net/
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Important Notice: We regret to inform you that our free phpBB forum hosting service will be discontinued by the end of June 30, 2024. If you wish to migrate to our paid hosting service, please contact billing@hostonnet.com.
Measuring multiline strings

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index -> Support - moved to http://forum.pdfsharp.net/
View previous topic :: View next topic  
Author Message
cmosses



Joined: 05 Oct 2007
Posts: 6

PostPosted: Fri Oct 05, 2007 9:19 am    Post subject: Measuring multiline strings Reply with quote

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.
Back to top
View user's profile Send private message
mohali



Joined: 23 Oct 2007
Posts: 2

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

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
Back to top
View user's profile Send private message
cmosses



Joined: 05 Oct 2007
Posts: 6

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

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.
Back to top
View user's profile Send private message
mohali



Joined: 23 Oct 2007
Posts: 2

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

can you give me sample code?

thanks,

rgds,

-mak
Back to top
View user's profile Send private message
cmosses



Joined: 05 Oct 2007
Posts: 6

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

Here you have the code that will do this:
PDFsharp Multiline MeasureString
Back to top
View user's profile Send private message
ACS



Joined: 01 Dec 2007
Posts: 15

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

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.
Back to top
View user's profile Send private message
cmosses



Joined: 05 Oct 2007
Posts: 6

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

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).
Back to top
View user's profile Send private message
ilivewithian



Joined: 07 May 2008
Posts: 4

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

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
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index -> Support - moved to http://forum.pdfsharp.net/ All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © phpBB Group. Hosted by phpBB.BizHat.com


Start Your Own YouTube Clone

Free Web Hosting | Free Forum Hosting | FlashWebHost.com | Image Hosting | Photo Gallery | FreeMarriage.com

Powered by PhpBBweb.com, setup your forum now!
For Support, visit Forums.BizHat.com