Hi Thomas,
Cool. Thanks a lot for your help. I struggled a bit mixing the PDFSharp and MigraDoc worlds, but below is my working code solving the problem - in case others could benefit from it.
(Note: If you call this multiple times, you might want to move the first 3 lines of dummy-creation out of the routine).
Thanks again,
Lars (
LarsBoKlitgaard@gmail.com)
Code:
private Paragraph AddTextToCell(string instring, Cell cell, Unit fontsize)
{
PdfDocument pdfd = new PdfDocument();
PdfPage pg = pdfd.AddPage();
XGraphics oGFX = XGraphics.FromPdfPage(pg);
Unit maxWidth = cell.Column.Width-(cell.Column.LeftPadding+cell.Column.RightPadding);
Paragraph par;
XFont font = new XFont(document.Styles["Table"].Font.Name, fontsize);
if (oGFX.MeasureString(instring, font).Width < maxWidth.Value)
{
par = cell.AddParagraph(instring);
}
else // String does not fit - start the truncation process...
{
int stringlength = instring.Length;
for (int i = 0; i<3; i++)
{
if (oGFX.MeasureString(instring.Substring(0, stringlength) + '\u2026', font).Width > maxWidth.Value)
stringlength -= (int)Math.Ceiling(instring.Length * Math.Pow(0.5f, i));
else
if (i<2)
stringlength += (int)Math.Ceiling(instring.Length * Math.Pow(0.5f, i));
}
par = cell.AddParagraph(instring.Substring(0, stringlength) + '\u2026');
}
par.Format.Font.Size = fontsize;
return par;
}