MeasureString AND DrawString BUG - STRANGE WORD WRAP!!

Apr 13, 2011 at 10:19 AM

BUG (DLL VERSION 1.2.1269.0)


 PdfDocument pdfDocument = new PdfDocument();

    // Create an empty page

           PdfPage pdfPage = pdfDocument.AddPage();

           // Get an XGraphics object for drawingew

           XGraphics xGraphics = XGraphics.FromPdfPage(pdfPage);

            // Create a font

           XFont xFontRegular = new XFont("Times New Roman", 9, XFontStyle.Regular);

             XSize xSize2;

           for (int i = 1; i < 100; i++)


               string text2 = "PIPPO ";

                xSize2 = xGraphics.MeasureString(text2 + y.ToString(), xFontRegular);

               xGraphics.DrawString(text2 + y.ToString(), xFontRegular, XBrushes.Black,

                   new XRect(x, y, xSize2.Width, xSize2.Height), XStringFormats.TopLeft);


               y = y + (xSize2.Height - 2); //CHANGE THIS LINE!!!






You should use xFontRegular.Height instead xSize2.Height!


Jun 4, 2011 at 2:59 PM
Edited Jun 5, 2011 at 11:15 AM


I do not get it what the word-wrap problem would be in this situation.

I do understand that using xFontRegular.Height will give the Height.


However my problem isn't the height, my problem with MeasureString is that it doesn't correctly measure the width.


I want to outline money so that it is outlined on the right side.

To get the X-point value I do:

page.Width - 100 - gfx.MeasureString(item.price.Value.ToString(), font).Width

This should give the X-point where I should draw my string so that the money is outlined on the right at 100 from the page.width.

And no matter what I try it just will not do this correctly, it is not outlined on the right side.

It appears that gfx.MeasureString(item.price.Value.ToString(), font).Width is always zero.


If I add: XStringFormat and use that in my call to gfx.DrawString, like:

PdfSharp.Drawing.XStringFormat xsf = new XStringFormat(); xsf.LineAlignment = PdfSharp.Drawing.XLineAlignment.BaseLine;


Than all over sudden, measurestring produces a result. However there are 2 strange things:

1) If I let it build the same PDF twice. than both are not identical. The first call goes well, than all other calls the money is positioned almost outside the page.

Where as when I let it build the same PDF for the second, third, etc time then this will no longer happen. Using the debugger, the calculated X-position is the same no matter if it builds the PDF for the first or second time. It appears that by just using MeasureString something goes wrong

2) Although it produces different results for different strings. The money isn't outlined on the right-side. 999,99 is about half a character to far to the left than 99,99.

In my samples the money figures in the hundreds are al at the first line and the money figures in tenths are sedond line and more. So theoretically it could be a matter of misalligning the first line (just haven't tested that yet).

I think my one and only solution is to left allign each character in the amount of money. In that way at least all characters will be alligned.



Kind regards,



Clemens Linders