i have to render a PDF with 3 columns layout. But i have to know when i dont have enough space for the next paragraph in the column and switch to the next one. I tried to use the XTextFormatterEx what the owner of the library using, but im still don't have accuracy what i need.
Is there a way to calculate Height of the string by provided width with Migradoc/PdfSharp?
245 Views Asked by iFURY At
2
There are 2 best solutions below
1
On
for horizontal size look here : How to determine the size of a string given a font
for vertical size, search for "font metrics" there is plenty of schema in google image. Here is a sample of how to get theses metrics :
//Get "arial Regular" Metrics in DesignUnit
var lFont = new FontFamily("Arial");
var lEm_height = lFont.GetEmHeight(System.Drawing.FontStyle.Regular);
var lAscent = lFont.GetCellAscent(System.Drawing.FontStyle.Regular);
var lDescent = lFont.GetCellDescent(System.Drawing.FontStyle.Regular);
var lLineSpacing = lFont.GetLineSpacing(System.Drawing.FontStyle.Regular);
//in milimeter
double lFontSize = 12; //in pt
var lFontSizeMM = U_Unit.Convert_From_pt.To_mm(lFontSize); //convert Pt to MM, from my personal lib
var lMM_Ascent = lFontSizeMM * (lAscent / lEm_height);
var lMM_Descent = lFontSizeMM * (lDescent / lEm_height);
var lMM_LineSpacing = lFontSizeMM * (lLineSpacing / lEm_height);
var lMM_Em_height = lFontSizeMM * (lEm_height / lEm_height);
//in px
var lFontSizePx = U_Unit.Convert_From_pt.To_px(lFontSize); //convert Pt to Px, from my personal lib
var lPx_Ascent = lFontSizePx * (lAscent / lEm_height);
var lPx_Descent = lFontSizePx * (lDescent / lEm_height);
var lPx_LineSpacing = lFontSizePx * (lLineSpacing / lEm_height);
var lPx_Em_height = lFontSizePx * (lEm_height / lEm_height);
PDFsharp: When using
XTextFormatterExto measure text and to draw text, thenXTextFormatterExis the way to calculate the exact height of text strings.MigraDoc: With MigraDoc you design documents that can be rendered for several outputs, so at the design stage there are no pages and no "text heights".
When MigraDoc is used as intended, there is no need to use MeasureString at all.
MeasureString can be used to determine the height of a single line for PDF, but the height of multi-line paragraphs may still be different.