PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Drawstring problem
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1060
Page 1 of 1

Author:  dancer [ Thu Feb 18, 2010 3:16 pm ]
Post subject:  Drawstring problem

Hi,

when I'm using DrawString and use it in a rectangle (for example)

Code:
XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
XRect rect = new XRect(100, 100, 20, 100);
gfx.DrawString("Hello, World!", font, XBrushes.Black, rect, XStringFormats.TopLeft);


no matter what the rectangle size is, it will draw on PDF the entire string.
In GDI+ it will draw only what can be seen inside the bounds of the rectangle.

And one more thing. If the string is empty or a few spaces " " it will produce error. But it should not.

Is there any way to fix this?

Author:  Thomas Hoevel [ Thu Feb 18, 2010 3:42 pm ]
Post subject:  Re: Drawstring problem

dancer wrote:
no matter what the rectangle size is, it will draw on PDF the entire string.

The rectangle is used for alignment only, not for clipping.

Spaces should not cause problems. I must investigate this.

Which version and build do you use?
viewtopic.php?f=2&t=832

Author:  dancer [ Thu Feb 18, 2010 4:01 pm ]
Post subject:  Re: Drawstring problem

Thank you very much for your answer.

this code creates problem

Code:
rect = new XRect(300, 100, 20, 100);
gfx.DrawString("      ", font, XBrushes.Black, rect, XStringFormats.BottomCenter);


the error it produces is: WidthAndHeightCannotBeNegative

The version I'm using is 1.31.1789.0

I understand what you told me about rectangle is used for alignment.
Could you, please, tell me is there any other way to draw only part of the string in the rectangle I want?

Thanks in advance

Author:  DaleStan [ Wed Feb 24, 2010 2:17 pm ]
Post subject:  Re: Drawstring problem

My admittedly-simplistic solution is to measure the string and chop off the last letter if it's too long.
Lather, rinse, repeat.

Depending on how many characters you generally need to drop, a binary search might be a better solution.

Author:  dancer [ Tue Mar 02, 2010 12:08 pm ]
Post subject:  Re: Drawstring problem

Thanks DaleStan for answer. Tried that already, but somehow measure string is not returning the right Size, I have no idea why. I did creating white rectangle over the part that I do not want to be visible. I Found no better solution.

Thomas, did you look at the problem when trying to draw empty string?

Author:  PdfJensen [ Sat Jan 12, 2013 8:44 am ]
Post subject:  Re: Drawstring problem

Hello,
I have the same problem during drawing strings with only whitespaces.
Is it a problem during conversion from float to double?
public static XSize FromSizeF(SizeF size) { return new XSize(size.Width, size.Height); }
public XSize(double width, double height) { ...
Any workaround?
Regards Jens

PdfSharp 1.32.2608.0

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/