We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Issue with DrawText

Hi,
Following code is not centering the Text.

IPDFGraphicState props = oc.CreateGraphicState();
props.TextAlignment = TextAlignment.Center;
props.BreakBehavior = AutoBreakBehavior.MultiLineTextBreak;
IPDFFont headerFont = doc.Fonts.Add(FontBaseFamily.CourierBold);
headerFont.Size = 14;

I set the Text Alignment to Center but it is still drawing at x=0 position.

Pls provide me the solution asap

doc.LastPage.Graphics.DrawText(0, y, text, headerFont, props);

Thanks
-Kiran




1 Reply

AD Administrator Syncfusion Team October 17, 2007 01:51 PM UTC

Hi Kiran,

In DrawText function it is not possible to apply the alignment, because DrawText function contains the text position as its argument. So, to do center alignment in pdf file, we can use page layout to the page. Inside the layout we can bound a rectangle and can set alignment to the text using StringFormat. Please refer the code snippet below.


[C#]
PdfStringFormat format = new PdfStringFormat();
format.Alignment = PdfTextAlignment.Center;
PdfTextElement element = new PdfTextElement(text, font);
element.Brush = new PdfSolidBrush(Color.Black);
element.StringFormat = format;
PdfLayoutFormat layoutFormat = new PdfLayoutFormat();
layoutFormat.Break = PdfLayoutBreakType.FitPage;
layoutFormat.Layout = PdfLayoutType.Paginate;
RectangleF bounds = new RectangleF(new PointF(10,10),new SizeF(page.Graphics.ClientSize.Width- 20,page.Graphics.ClientSize.Height-10));
PdfTextLayoutResult result = element.Draw(page, bounds, layoutFormat);


I have created a sample to test this issue and it is available in the following link.

http://websamples.syncfusion.com/samples/PDF.Windows/F69149/main.htm

Please try this and let me know if this helps.

Regards,
Prakash

Loader.
Live Chat Icon For mobile
Up arrow icon