PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

TextMeasurement class for use with PDFsharp 1.50 beta 2
https://forum.pdfsharp.net/viewtopic.php?f=8&t=3196
Page 1 of 1

Author:  TH-Soft [ Tue Sep 22, 2015 4:46 pm ]
Post subject:  TextMeasurement class for use with PDFsharp 1.50 beta 2

MigraDoc 1.32 included a class TextMeasurement. This class is no longer included with version 1.50 beta 2.

The original implementation uses the Graphics class to measure text.

My modified version uses the XGraphics class of PDFsharp to measure text. I tested it successfully with the GDI+ and WPF builds of 1.50 beta 2. I didn't test other builds yet - I don't know if it will work under Silverlight, but why not?.

Copy the code from here and paste it into a TextMeasurement.cs file you add somewhere in your project.
You also need the MigraDoc package from NuGet ("PDFsharp + MigraDoc (GDI) 1.50.3915-beta2" and "PDFsharp + MigraDoc (WPF) 1.50.3915-beta2" were tested successfully).

Code:
#region MigraDoc - Creating Documents on the Fly
//
// Authors:
//   Stefan Lange
//   Klaus Potzesny
//   David Stephensen
//
// Copyright (c) 2001-2009 empira Software GmbH, Cologne (Germany)
//
// http://www.pdfsharp.com
// http://www.migradoc.com
// http://sourceforge.net/projects/pdfsharp

// Modifications by:
//   Thomas Hövel
//   Copyright (c) 2015 TH Software, Troisdorf (Germany), http://developer.th-soft.com/developer/
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Diagnostics;
using System.ComponentModel;
using PdfSharp.Drawing;

namespace MigraDoc.DocumentObjectModel
{
    /// <summary>
    /// Provides functionality to measure the width of text during document design time.
    /// </summary>
    public sealed class TextMeasurement
    {
        /// <summary>
        /// Initializes a new instance of the TextMeasurement class with the specified font.
        /// </summary>
        public TextMeasurement(Font font)
        {
            if (font == null)
                throw new ArgumentNullException("font");

            _font = font;
        }

        /// <summary>
        /// Returns the size of the bounding box of the specified text.
        /// </summary>
        public XSize MeasureString(string text, UnitType unitType)
        {
            if (text == null)
                throw new ArgumentNullException("text");

            if (!Enum.IsDefined(typeof(UnitType), unitType))
                throw new InvalidEnumArgumentException();

            XGraphics graphics = Realize();

            XSize size = graphics.MeasureString(text, _gdiFont/*, new XPoint(0, 0), StringFormat.GenericTypographic*/);
            switch (unitType)
            {
                case UnitType.Point:
                    break;

                case UnitType.Centimeter:
                    size.Width = (float)(size.Width * 2.54 / 72);
                    size.Height = (float)(size.Height * 2.54 / 72);
                    break;

                case UnitType.Inch:
                    size.Width = size.Width / 72;
                    size.Height = size.Height / 72;
                    break;

                case UnitType.Millimeter:
                    size.Width = (float)(size.Width * 25.4 / 72);
                    size.Height = (float)(size.Height * 25.4 / 72);
                    break;

                case UnitType.Pica:
                    size.Width = size.Width / 12;
                    size.Height = size.Height / 12;
                    break;

                default:
                    Debug.Assert(false, "Missing unit type");
                    break;
            }
            return size;
        }

        /// <summary>
        /// Returns the size of the bounding box of the specified text in point.
        /// </summary>
        public XSize MeasureString(string text)
        {
            return MeasureString(text, UnitType.Point);
        }

        /// <summary>
        /// Gets or sets the font used for measurement.
        /// </summary>
        public Font Font
        {
            get { return _font; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");
                if (_font != value)
                {
                    _font = value;
                    _gdiFont = null;
                }
            }
        }
        Font _font;

        /// <summary>
        /// Initializes appropriate GDI+ objects.
        /// </summary>
        XGraphics Realize()
        {
            if (_graphics == null)
                _graphics = XGraphics.CreateMeasureContext(new XSize(2000, 2000), XGraphicsUnit.Point, XPageDirection.Downwards);

            //this.graphics.PageUnit = GraphicsUnit.Point;

            if (_gdiFont == null)
            {
                XFontStyle style = XFontStyle.Regular;
                if (_font.Bold)
                    style |= XFontStyle.Bold;
                if (_font.Italic)
                    style |= XFontStyle.Italic;

                _gdiFont = new XFont(_font.Name, _font.Size, style/*, System.Drawing.GraphicsUnit.Point*/);
            }
            return _graphics;
        }

        XFont _gdiFont;
        XGraphics _graphics;
    }
}

Author:  yellowmosquito [ Fri May 18, 2018 3:15 am ]
Post subject:  Re: TextMeasurement class for use with PDFsharp 1.50 beta 2

Sorry for the extreme delay in responding, but I wanted to let you know that this TextMeasurement class appears to be working great as a replacement for the missing 1.32 functionality with the latest release (1.50.4845-RC2a). Thanks again!

Author:  NewUserHere [ Wed Mar 06, 2019 9:54 am ]
Post subject:  Re: TextMeasurement class for use with PDFsharp 1.50 beta 2

yellowmosquito wrote:
Sorry for the extreme delay in responding, but I wanted to let you know that this TextMeasurement class appears to be working great as a replacement for the missing 1.32 functionality with the latest release (1.50.4845-RC2a). Thanks again!


I can confirm that, too :)

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/