Calculate text height based on available width and

2020-02-10 03:54发布

问题:

We are creating PDF documents on the fly from the database using PDFsharp.

I need to know the best way to calculate the height of the text area based on the font used and the available width.

I need to know the height so I can process page breaks when required.

回答1:

In .NET you can call Graphics.MeasureString to find out how large the drawn text is going to be.

Right, but when using PDFsharp you call XGraphics.MeasureString.



回答2:

The PdfSharp.Drawing.XGraphics object has a MeasureString method that returns what you require.

 var pdfDoc = new PdfSharp.Pdf.PdfDocument();
 var pdfPage = pdfDoc.AddPage();
 var pdfGfx = PdfSharp.Drawing.XGraphics.FromPdfPage(pdfPage);
 var pdfFont = new PdfSharp.Drawing.XFont("Helvetica", 20);

 while (pdfGfx.MeasureString("Hello World!").Width > pdfPage.Width)
      --pdfFont.Size;

 pdfGfx.DrawString("Hello World!", pdfFont
      , PdfSharp.Drawing.XBrushes.Black
      , new PdfSharp.Drawing.XPoint(100, 100));

This should help you. Please consider that I didn't test this code as I wrote it on the fly in order to help. It might contain some compile-time errors, but you may get the idea.



回答3:

I had a similiar problem so I implemented this extension method:

public static double MeasureHeight(this PdfSharp.Drawing.XGraphics gfx, string text, PdfSharp.Drawing.XFont font, int width)
{
    var lines = text.Split('\n');

    double totalHeight = 0;

    foreach (string line in lines)
    {
        var size = gfx.MeasureString(line, font);
        double height = size.Height + (size.Height * Math.Floor(size.Width / width));

        totalHeight += height;
    }

    return totalHeight;
}


回答4:

In .NET you can call Graphics.MeasureString to find out how large the drawn text is going to be.



回答5:

I wrote a small extension method to the XGraphic object to do just that : Calclulate the exact text height (and width) by specifiying the maxWidth. See the following gist for the code : https://gist.github.com/erichillah/d198f4a1c9e8f7df0739b955b245512a



回答6:

The OP asked how to calculate text height based on available width and font. Windows .NET provides an API call for this which takes a width argument; the version of PDFsharp I'm using (0.9.653, .NET 1.1) does not.

My solution - use the .NET API call with a Graphics object allocated for a custom-created Bitmap object to get the answer.

What worked for me was to use a Bitmap that had 100 DPI resolution (critical) and happened to be the size of a Portrait page (probably less critical).

Then I just asked .NET what the pixel size would be for painting on that bitmap.

You probably will then want to convert the units from 1/100th of an inch to Points (for PDFsharp).

''' Adapted Code - this not tested or even compiled - Caveat Emptor!
''' Target: Visual Basic, .NET 1.1 (VS2003) [adapt as necessary]

'  '  '  '  '  '  '  '  '  '  '  '  '  '  '  '
'  GraphicsAlt.MeasureString() does substantially what System.Drawing MeasureString(...,Integer) does.
'  '  '  '  '  '  '  '  '  '  '  '  '  '  '  '
Public Module GraphicsAlt

    '
    ' Static data used Only to compute MeasureString() below.
    '
    '     Cache a single copy of these two objects, to address an otherwise unexplained intermittent exception.
    '
    Private Shared myImage As Bitmap = Nothing
    Private Shared myGraphics As Graphics = Nothing

    Public Shared Function GetMeasureGraphics() As Graphics
        If myImage Is Nothing Then
            myImage = New Bitmap(1700, 2200)  '' ... Specify 8.5x11 
            myImage.SetResolution(100, 100)   '' ... and 100 DPI (if you want different units, you might change this)
            myGraphics = Graphics.FromImage(myImage)
        End If
        Return myGraphics
    End Function

    'Given 1/100TH inch max width, return Rect to hold with units 1/100TH inch
    '
    Public Function MeasureString(ByVal text As String, ByVal aFont As System.Drawing.Font, ByVal width As Integer) As System.Drawing.SizeF
        Return (GraphicsAlt.GetMeasureGraphics()).MeasureString(text, aFont, width)
    End Function

End Module


回答7:

In case anyone still wants to find an answer, I've implemented a reasonably easy-to-understand method to find out the height of the resulting text.

Public Function PrintString(text As String, ft As XFont, rect As XRect, graph As XGraphics, b As SolidBrush, Optional tf As XTextFormatter = Nothing) As Integer
    If Not IsNothing(tf) Then
        tf.DrawString(text, ft, b, rect)
    Else
        Dim drawLeft As New XStringFormat
        drawLeft.Alignment = XStringAlignment.Near

        graph.DrawString(text, ft, b, rect, drawLeft)
    End If

    Dim width As Double = graph.MeasureString(text, ft).Width
    Dim multiplier As Integer = 0

    While width > 0
        multiplier += 1

        width -= rect.Width
    End While

    Dim height As Double = (graph.MeasureString(text, ft).Height) * multiplier
    Return height
End Function

Explaining the code:

First, print the text. I included an Optional XTextFormatter called tf because I use either XGraphics or XTextFormatters interchangeably in my application.

Then, calculate how long the text was by MeasureString().Width.

Then, calculate how many lines of text there were. This is done by dividing the total length of the text found earlier by the width of the provided rectangle (box) where the tax is printed. I did it with a while loop here.

Multiply the height of the text (using graph.MeasureString().Height) by the number of lines there were. This is the final height of your text.

Return the height value. Now, calling the PrintString() function will print the text provided out while returning the height of the printed text afterward.



回答8:

PDFsharp includes a class XTextFormatter that can be used to draw text with linebreaks.

However it can not determine the height needed for the text. Inspired by a comment from @Wakka02 I improved this class, generating class XTextFormatterEx.
In my opinion it also answers the original question, therefore I post an answer.
I know this is an old question and the answer may not help the OP, but it is a frequently asked question and the answer may help others.

The new class has 500 lines of code - and I think this would be too much for this post.

The source code can be found on the PDFsharp forum:
http://forum.pdfsharp.net/viewtopic.php?p=9213#p9213

It can also be found in my humble blog:
http://developer.th-soft.com/developer/pdfsharp-improving-the-xtextformatter-class-measuring-the-height-of-the-text/

When using the new class, you can first call PrepareDrawString to find out how much of the text fits and which height the fitting text has. Then your decoder can draw the prepared text or prepare another text or prepare the same text with a different rectangle.

My new class at work: XTextFormatterEx tf = new XTextFormatterEx(gfx); int lastCharIndex; double neededHeight;

// Draw the text in a box with the optimal height
// (magic: we know that one page is enough).
XRect rect = new XRect(40, 100, 250, double.MaxValue);
//tf.Alignment = ParagraphAlignment.Left;
tf.PrepareDrawString(text, font, rect,
                     out lastCharIndex, out neededHeight);
rect = new XRect(40, 100, 250, neededHeight);
gfx.DrawRectangle(XBrushes.SeaShell, rect);
// Both variants should look the same.

// Optimized version: draw the prepared string.
tf.DrawString(XBrushes.Black, XStringFormats.TopLeft);

Preparing the text invokes MeasureString many times. Later the prepared text can be drawn without invoking MeasureString again.

As of today (Juli 17, 2015) the class XTextFormatterEx (like the original XTextFormatter) uses internal fields of the XFont class. This requires special treatment when compiling the class. I decided to copy my XTextFormatterEx class into the PDFsharp folder after downloading the complete source package for PDFsharp 1.32.
Anybody trying to modify either the XTextFormatter or XTextFormatterEx class will face the same problem.
I hope this issue will be solved with future versions of PDFsharp, allowing modified versions of these classes to be included in the application project.