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:28 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri Dec 14, 2018 12:22 pm 
Offline

Joined: Fri Dec 14, 2018 12:19 pm
Posts: 2
Hi!

I have a small problem and I'd be more than happy if anyone helped me.
A project I'm currently working on uses MigraDoc PdfSharp. So far it used the latest stable version: 1.32.2608, but we are trying to migrate to a newer, prerelease version (because of a connected project using 1.50.4619-beta4c). And almost everything looks fine, but there is one thing I'm not sure how to get around. There is a function measuring a string's size, looking (to put it simply) something about that:
Code:
protected SSize GetSize(string text, MigraDoc.DocumentObjectModel.Font font)
{
  var doc = new PdfDocument();
  var page = doc.AddPage;
  var sizer = XGraphics.FromPdfPage(page);
  var style = XFontStyle.Regular;
  //some style checks

  var xf = new XFont(font.Name, font.Size, style);
  var st = new XStringFormat();
  st.FormatFlags = XStringFormatFlags.MeasureTrailingSpaces;
  var xs = sizer.MeasureString(text, xf, st);
  return new SSize {
    Height = XUnit.FromPoint(xs.Height * _measureCorrection).Centimeter,
    Width = XUnit.FromPoint(xs.Width * _measureCorrection).Centimeter
  };
}

and the problematic line is:
Code:
st.FormatFlags = XStringFormatFlags.MeasureTrailingSpaces;

as the XStringFormat lost the FormatFlags property, and there is no longer the XStringFormatFlags enum.
Could anyone give me a clue, how to transform it properly to a new version?


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 14, 2018 2:25 pm 
Offline
PDFsharp Expert
User avatar

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

I think I'd try this:
If the string ends with a space, I would add "X" to the string, would also call MeasureString for "X" and use the difference.
If the string does not have trailing spaces, this hack is not needed.

MeasureString has become much faster with version 1.50 and you do not have to measure "X" every time.
So despite this workaround, things should work faster with the new version.

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


Top
 Profile  
Reply with quote  
PostPosted: Sat Dec 15, 2018 8:31 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Your code creates a new PDF document and a new PDF page just to measure a string - for every string.

A better implementation can be found here:
viewtopic.php?f=8&t=3196
This implementation does not have the flags you want, but it shows a more efficient way to get an XGraphics object.

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


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 17, 2018 7:21 am 
Offline

Joined: Fri Dec 14, 2018 12:19 pm
Posts: 2
Thank you, I'll take a look.


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

All times are UTC


Who is online

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