views:

1451

answers:

2

Whether I use XTextFormatter or not, I get the same error about the LayoutRectangle having to have a height of 0 or something like this.

new PdfSharp.Drawing.Layout.XTextFormatter(_gfx).DrawString(text 
    , new PdfSharp.Drawing.XFont(fontName, fontSize, (PdfSharp.Drawing.XFontStyle)fontStyle) 
    , new PdfSharp.Drawing.XSolidBrush(PdfSharp.Drawing.XColor.FromArgb(foreColour)) 
    , new PdfSharp.Drawing.XRect(new PdfSharp.Drawing.XPoint(xPos, yPos), new PdfSharp.Drawing.XPoint(xLimit, yLimit)) 
    , PdfSharp.Drawing.XStringFormats.Default);

fontStyle is of type System.Drawing.FontStyle foreColour is of type System.Drawing.Color I have already predefined _gfx from a PdfPage with Orientation = Landscape, Size = Letter xPos and yPos are parameters of type double, the same with xLimit and yLimit.


I get the runtime error that the LayoutRectangle must have a height of zero (0)...


Per definition a rectangle is meant to have a height, otherwise call it a line! I don't get it!...

I tried with the XGraphics.DrawString() method directly, and I get the same error. It seems that I can't use the LayoutRectangle but have to manage that the text fit within the desired area manually.

var textFont = new PdfSharp.Drawing.XFont(fontName, fontSize, (PdfSharp.Drawing.XFontStyle)fontStyle);

while (xPos + _gfx.MeasureString(text, textFont).Width > xLimit)
    textFont = new PdfSharp.Drawing.XFont(fontName, --fontSize, (PdfSharp.Drawing.XFontStyle)fontStyle);

while (yPos + _gfx.MeasureString(text, textFont).Height > yLimit && fontSize > 0)
    textFont = new PdfSharp.Drawing.XFont(fontName, --fontSize, (PdfSharp.Drawing.XFontStyle)fontStyle);

_gfx.DrawString(text
    , textFont
    , new PdfSharp.Drawing.XSolidBrush(PdfSharp.Drawing.XColor.FromArgb(foreColour))
    , new PdfSharp.Drawing.XPoint(xPos, yPos));

Though the yPos variable value is the exact same value!

*yPos = Page.Height * .4093, either 40,93% of the page's height.*

Herewith an example of what I try to do:

"Hello World!" "Hello World!"

And here is what I get:

                      "Hello World!"

"Hello World!"

And because of different printing area limits and size of the font and the different font style, I can't just write these into one simple sentence including the correct number of spaces.

+1  A: 

Quoting error messages exactly helps others to help you.

The error message reads: "DrawString: With XLineAlignment.BaseLine the height of the layout rectangle must be 0." The text will be aligned at a line, therefore height must be 0. Yes, that's a line. Use a different alignment if you specify a rectangle.

The TextLayout sample shows how to format text.

The best place to ask PDFsharp and MigraDoc Foundation related questions is the PDFsharp forum. The PDFsharp Team will not monitor stackoverflow.com on a regular basis.

PDFsharp Forum:
http://forum.pdfsharp.net/

PDFsharp Website:
http://www.pdfsharp.net/

PDFsharp Team
A: 

While trying to figure out how text positioning works with PdfSharp, I noticed that the DrawString() method writes on top of the Y coordinate that we specify.

If I wish to write at (0, 100)(x, y), this points to the lower-left corner while I thought this was the top-left corner coordinates. As a result, the text string Y coordinate that I should have specified is 100 + string.Height * .6.

PdfDocument pdfDoc = new PdfDocument();
PdfPage pdfPage = new pdfPage();
pdfPage.Size = PageSize.Letter;
pdfPage.Orientation = Orientation.Landscape;
pdfDoc.Pages.Add(pdfPage);

double posX = 0;
double posY = pdfPage.Height * .4093;
string helloString = "Hello"
string worldString = "World!"

XFont helloFont = new XFont("Helvetica", 25, XFontStyle.Regular);
XFont worldFont = new XFont("Helvetica", 270, XFontStyle.Bold);

using(var pdfGfx = XGraphics.FromPdfPage(pdfPage)) { // assuming the default Point UOM
    XSize helloStringSize = pdfGfx.MeasureString(helloString, helloFont);
    XSize worldStringSize = pdfGfx.MeasureString(worldString, worldFont);
pdfGfx.DrawString(helloString
    , helloFont
    , XBrushes.Black
    , posX
    , posY + helloStringSize.Height * .6
    , XStringFormats.Default);
pdfGfx.DrawString(worldString
    , worldFont
    , XBrushes.Black
    , pdfPage.Width * .3978
    , posY + (worldStringSize.Height + helloStringSize.Height) * .6
    , XStringFormats.Default);

}

You'll perhaps wonder why I only add 60% of the string size when I want to get my string written below my Y coordinate? That is because the full height of the font includes somekind of leap on top. So, the computing result will not be what is expected. On the other hand, you don't have to care about a leap if you need one. In my particular case, I don't require leap, so I must take it off the string's height.

If you feel like my explanation needs more accurate details, please feel free to either add them as comments or keep me informed so that I may include them.

Thanks!

Will Marcouiller