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.
Unfortunately, activation email could not send to your email. Please try again.

Issue with DrawText

Thread ID:





69149 Oct 16,2007 02:18 PM Oct 17,2007 09:51 AM Windows Forms 1
Tags: PDF
Kiran Guptha
Asked On October 16, 2007 02:18 PM

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);


Administrator [Syncfusion]
Replied On October 17, 2007 09:51 AM

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.

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.


Please try this and let me know if this helps.



This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.