XTextFormatter and box height
PDFsharp is a .NET library for creating and modifying PDF documents.
Brought to you by:
pdfsharp,
stefan_lange
Would love to be able to get the height of the box. Currently the program counts the number of lines to be printed, and uses that to calculate the height of the printed text. Pretty cumbersome tho.
Here is a function I added to the class XTextFormatter to calculate the size of the printed text :
/// <summary>
/// Get bounding box for the text drawn with the last call of this instance's DrawString().
/// </summary>
public XRect GetLastBoundingBox()
{
double dx = layoutRectangle.Location.x;
double dy = layoutRectangle.Location.y;
double max_x = 0, max_y = 0, min_x = 0, min_y = 0;
foreach (var block in this.blocks)
{
if (block.Location.x < min_x)
{
min_x = block.Location.x;
}
if (block.Location.y < min_y)
{
min_y = block.Location.y;
}
if ((block.Location.x + block.Width) > max_x)
{
max_x = block.Location.x + block.Width;
}
if ((block.Location.y + lineSpace) > max_y)
{
max_y = block.Location.y + lineSpace;
}
}
return (new XRect(min_x + dx, min_y + dy, max_x - min_x, max_y - min_y));
}