PdfSharp: text height / positioning issue
Whether I use XTextFormatter or not, I get the same error that the LayoutRectangle must have a height of 0 or something like that.
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 already predefined _gfx from PdfPage with Orientation = Landscape, Size = Letter xPos and yPos are double parameters, same with xLimit and yLimit .
I am getting a runtime error that the LayoutRectangle must have a height of zero (0) ...
To define the rectangle must have a height, otherwise call it a string! I do not understand!...
I tried using the XGraphics.DrawString () method and I am getting the same error. It seems that I cannot use LayoutRectangle, but I need to control that the text fits into 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));
Although the value of the yPos variable is the same value!
* yPos = Page.Height * .4093, or 40.93% of the page height. *
Here's an example of what I'm trying to do:
"Hello World!" "Hello World!"
And this is what I get:
"Hello World!"
"Hello World!"
And due to the different print area boundaries and font size and different font style, I cannot just write them down in one simple sentence including the correct amount of spaces.
source to share
Bug quotes are sure to help others help you.
Error message: "DrawString: with XLineAlignment.BaseLine the height of the layout rectangle must be 0." The text will be line-aligned, so the height must be 0. Yes, it's a line. Use a different alignment if you specify a rectangle.
The TextLayout example shows how to format text.
Edit: The sample graphics also show how the layout text (single lines of text, automatic line breaks, the method shown in the TextLayout sample handles line breaks automatically using the XTextFormatter class).
source to share
While trying to figure out how text positioning works with PdfSharp, I noticed that the DrawString () method overwrites the specified Y coordinate.
If I want to write in (0, 100) (x, y) it points to the bottom left corner when I thought it was the coordinates of the top left corner. As a result, the Y coordinate of the text string I had to specify 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 might be wondering why I only add 60% of the line size when I want my line to be written below my Y coordinate? This is because the full height of the font includes some leap from the top. Thus, the result of the calculation will not be what is expected. On the other hand, you don't need to take care of the jump if you need to. In my particular case, I do not require a jump, so I have to shoot it from line height.
If you feel that my explanation requires more specific details, please feel free to add them as comments or leave me alone so that I can include them.
Thank!
source to share