PDFsharp & MigraDoc Foundation

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

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Nov 01, 2022 3:40 pm 
Offline

Joined: Tue Nov 01, 2022 3:34 pm
Posts: 2
I have the following routine to calculate the fitted width of a string.
Code:
        public string GetFittedStringToPrint(XGraphics gfx, XFont font, XUnit width, string input)
        {
            string output = input;
            XSize stringMeasurement = gfx.MeasureString(input, font);
            if (stringMeasurement.Width > width)
            {
                string inputMinusOneChar = input;
                do
                {
                    inputMinusOneChar = inputMinusOneChar.Substring(0, inputMinusOneChar.Length - 1).Trim();
                    stringMeasurement = gfx.MeasureString(inputMinusOneChar + '…', font);
                } while (stringMeasurement.Width > width);
                output = inputMinusOneChar + '…';
            }
            return output;
        }

But I have a conversion problem from XUnit to XSize. So how can I compare a XUnit value (as centimeter) with a XSize value and check the width in centimeter?
Its because I have all inputs in centimeter - but MeasureString returns XSize...

Thanks in advance.


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 02, 2022 7:58 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Hi!

Just use the "Point" property of XUnit. That's what XSize is using: Points.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 02, 2022 8:44 pm 
Offline

Joined: Tue Nov 01, 2022 3:34 pm
Posts: 2
Thank you very much - it works very well :-)


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

All times are UTC


Who is online

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