How to horizontally aligment of non-wrapped text that longer than text rectange's width

Hello,
Here is my code

PdfStringFormat format = new PdfStringFormat

{

    Alignment = horizontalAlign,

    LineAlignment = verticalAlignment,

    LineLimit = false, 

    WordWrap = isMultiline ? PdfWordWrapType.Word : PdfWordWrapType.None,

    NoClip = false,

};


RectangleF rect = new RectangleF(textObject.X.Value, textObject.Y.Value, textObject.Width.Value, textObject.Height.Value);

if (backColorBrush != null)

    graphics.DrawRectangle(backColorBrush, rect);

graphics.DrawString(text, font, foreColorBrush, rect, format);



as you see in my format initilizer, there is a scenario that  WordWrap could be  PdfWordWrapType.None. With this scenario, i need to horizontally align text withing the it's container when it has fixed width:

Let's say container width is 100(fixed ) and cannot be resized with text size and text size is bigger than container's width. Also WordWrap is set to PdfWordWrapType.None and cannot be changed, expected result:
Image_8796_1723146297273

Actaul result is always left aligned:

Image_1986_1723146603354

Is there a way to set it as my expected result?


3 Replies 1 reply marked as answer

JT Jeyalakshmi Thangamarippandian Syncfusion Team August 9, 2024 02:32 PM UTC

Hi ERTEM,

Our PdfGraphics draws text within the provided rectangle area according to the specified alignment. However, it does not clip the text based on the alignment; the alignment property is solely used to position the text within the given rectangle. We recommend using the clip option in PdfGraphics to draw the text, and measuring the text to accurately calculate the clipping position. We've attached a sample for your reference. Please try it out on your end, and feel free to reach out if you need further assistance.

 

Sample:

https://www.syncfusion.com/downloads/support/directtrac/general/ze/Console_Sample1190625782



Regards,

Jeyalakshmi T


Marked as answer

ER ERTEM August 9, 2024 09:49 PM UTC

Thanks, that workaround could be used for my requirements



JT Jeyalakshmi Thangamarippandian Syncfusion Team August 12, 2024 05:57 AM UTC

Hi ERTEM,

Most welcome.

Please get back to us if you need any further assistance on this.


Regards,

Jeyalakshmi T



Loader.
Up arrow icon