PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Jul 03, 2024 3:28 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Tue Feb 22, 2011 10:41 pm 
Offline

Joined: Tue Feb 22, 2011 10:20 pm
Posts: 1
I'm trying to estimate the printed width of a run of text while creating a document, so that I can truncate the text to fit on a single line in a table cell.

I've managed to do this for specific situations using the following code:

Code:
private static string FitString(string value, Font font, float availableWidth)
{
   var ruler = new TextMeasurement(font);
   value = value.Trim();
   SizeF size = ruler.MeasureString(value, UnitType.Point);
   if (size.Width > availableWidth) value = value + "\u2026";
   while (size.Width > availableWidth)
   {
      value = value.Substring(0, value.Length - 3).Trim() + "\u2026";
      size = ruler.MeasureString(value, UnitType.Point);
   }
   return value;
}


However, I've found myself in a situation where I won't always know which Font object to grab. For instance, because in previous usages I knew the structure and formatting of the document ahead of time, I knew that the Cell I was truncating would use the Table's font size and name, so I would just grab the Font using something like table.Format.Font.

For my current project, this is no longer the case; I'm trying to generalize my FitString function so that I use it at a higher level, where I can't assume anything about the location of the formatting information.

Is there any way to easily figure out what formatting properties will ultimately be applied to a DocumentObject, or at least a Cell in particular?


Top
 Profile  
Reply with quote  
PostPosted: Wed Feb 23, 2011 8:21 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
This depends on how you use MigraDoc.

We specify Fonts for Styles only and use Styles anywhere else.

You can set the style of a cell, or the style of the table. Maybe row and column also have styles.
You can also use AddFormattedText so that elements in a cell will have different styles.

You can use "InternalsVisibleTo" to make internals of MigraDoc available to your application.
I don't know how MigraDoc determines which font will be used. But if you find the code in MigraDoc, you will be able to use the same routines.

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 36 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:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group