PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Mar 19, 2024 11:53 am

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 Jul 23, 2013 10:04 am 
Offline

Joined: Tue Jul 23, 2013 9:50 am
Posts: 2
Hi,

I've been using PDFSharp recently and needed to pull in some dynamic text and add it to the end of an existing PDF. The problem I had was knowing how much height the last piece of text used. I'm using XTextFormatter.DrawString and making it word wrap by providing plenty of height for it to work with.

Code:
    var rect = new XRect(textMargin, y, page.Width - (textMargin * 2), page.Height - y);
    textFormatter.DrawString(partText, font, XBrushes.Black, rect, XStringFormats.TopLeft);


As you can see I am giving DrawString the rest of the page to work with.

I've come up with a nasty little hack to work out the height the most recent DrawString used by writing a wrapper around XTextFormatter with a bit of reflection code to get at the private List<Block> blocks field

Code:
        public XSize GetDrawingSize()
        {
            var blocks = GetType()
                .BaseType
                .GetField("blocks", BindingFlags.Instance | BindingFlags.NonPublic)
                .GetValue(this) as IList;

            double x = 0.0, y = 0.0;
            if (blocks != null)
            {
                foreach (var block in blocks)
                {
                    var location =
                        (XPoint) block
                                     .GetType()
                                     .GetField("Location", BindingFlags.Instance | BindingFlags.Public)
                                     .GetValue(block);

                    var width =
                        (double) block
                                     .GetType()
                                     .GetField("Width", BindingFlags.Instance | BindingFlags.Public)
                                     .GetValue(block);

                    x = Math.Max(x, location.X + width);
                    y = Math.Max(y, location.Y);
                }
            }
            return new XSize(x, y + GetFontLineHeight());
        }


I've had to use a foreach loop rather than Linq because the Block class is private to the XTextFormatter class.

Here is the code for GetFontLineHeight ...

Code:
        protected double GetFontLineHeight()
        {
            var font = GetType()
                .BaseType
                .GetField("font", BindingFlags.Instance | BindingFlags.NonPublic)
                .GetValue(this) as XFont;

            return font.GetHeight(_gfx);
        }


It's very hackety hacky, but it does work.

What I'd like to do is provide a fix to PDFSharp, probably in the form of a MeasureString method within the XTextFormatter class. I think I'd just refactor DrawString and MeasureString to use the same block/layout code, the difference being whether or not to Draw the string.

So, my question is - How do I contribute code to PDFSharp?


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 23, 2013 4:13 pm 
Offline

Joined: Tue Jul 23, 2013 9:50 am
Posts: 2
I got the svn repository onto my PC and noticed this at the top of XTextFormatter ...

Code:
  /// <summary>
  /// Represents a very simple text formatter.
  /// If this class does not satisfy your needs on formatting paragraphs I recommend to take a look
  /// at MigraDoc Foundation. Alternatively you should copy this class in your own source code and modify it.
  /// </summary>


Quote:
Alternatively you should copy this class in your own source code and modify it.


So, that's what I've done. I've created my own TextFormatter which simply returns the size of the text it rendered in DrawString. Inside the loop which draws the strings I have this ...

Code:
    width = Math.Max(width, block.Location.X + block.Width);
    height = Math.Max(height, block.Location.Y + _lineHeight);


... and I return the size like so ...

Code:
return new XSize(width, height);


It works a treat :D


Top
 Profile  
Reply with quote  
PostPosted: Mon Jun 02, 2014 4:41 pm 
Offline

Joined: Mon Jun 02, 2014 4:38 pm
Posts: 1
antony_scott wrote:
I got the svn repository onto my PC and noticed this at the top of XTextFormatter ...


So where did you find an SVN repository you could connect to? On the listed sites at Codeplex and SoureForge, I'm only finding a .zip download for the source code. I'd rather pull the SVN repo and submit patches if I fix something / add a feature.

Thanks,

Eric.


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 5 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