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

How to you center and align right on Pdf header template?

I have created a pdf, and find I can center and right-align text by using the PdfTextElement and Draw with a rectangleF. But when I created a PdfPageTemplateElement to group the items into a header, I cannot center and align-right the text. How do I do this? I need to center a title and right-align and left-align a few PdfTextElements.

this works for items drawn directly on page:
PdfStringFormat strformat = new PdfStringFormat () { WordWrap = PdfWordWrapType.Word, LineLimit = true, Alignment = PdfTextAlignment.Center };
PdfTextElement txtElement = new PdfTextElement(vm.SubmitAllData.TestSheets [vm.SubmitAllData.CurrentTestsheet].printname)
            { Font = FontResources.PdfHeaderFont, Brush= blackbrush, StringFormat = strformat };
PdfLayoutResult result = txtElement.Draw(page, new RectangleF(0, 70, doc.PageSettings.Width, 30));

How do I draw it on the header? I tried this but it doesn't center the text:

RectangleF rect = new RectangleF(0, 0, doc.PageSettings.Width, headheight);
PdfPageTemplateElement header = new PdfPageTemplateElement(rect);
PdfGraphics hg = header.Graphics;

. . .
txtElement.Draw (hg, new PointF(0, 70));

4 Replies

AS Anita Sheffield September 15, 2015 08:53 AM UTC

I figured this out - Using the .Draw method., pass the header.Graphics, and if the PdfTextElement is set to Center - it centers at the position PointF, if it is set to right justify - PointF is used as the right side of the text.
txtElement.Draw (hg, new PointF(0, 70));


KK Karthik Krishnaraj Syncfusion Team September 16, 2015 06:14 AM UTC

Hi Anita,
Thank you for contacting Syncfusion support,
We are happy to know that issue has been resolved at your end, please let me know if you have any other queries.
Thanks,
Karthik.


AA Adnan Ali replied to Anita Sheffield April 10, 2023 03:01 PM UTC

Can you share the solution please, I need to implement this, I mean how can I center a text in the rectangle 



AS Anita Sheffield April 10, 2023 03:46 PM UTC

This was an old post, using a 2015 version of syncfusion, but it is still working code for us. We are also adding a logo on the left side of the header.  I hope this helps. 

RectangleF rect = new RectangleF(0, 0, doc.PageSettings.Width, headheight);

PdfPageTemplateElement header = new PdfPageTemplateElement(rect);

try
{

        PdfGraphics hg = header.Graphics;                

        PdfSolidBrush blackbrush = new PdfSolidBrush(ColorResources.pdfblack);
        PdfStringFormat strformat = new PdfStringFormat(PdfTextAlignment.Center,                 PdfVerticalAlignment.Middle) { WordWrap = PdfWordWrapType.Word, LineLimit = true };

        if (logoImg != null)

        {
               hg.DrawImage(logoImg, 10, 0, 140, 40f);
        } 

        PdfTextElement txtElement = new PdfTextElement("Inventory Report For Location: " + location)
                { Font = FontResources.PdfHeaderFont, Brush = blackbrush, StringFormat = strformat };
        txtElement.Draw(hg, new PointF(doc.PageSettings.Width / 2, 40));


Loader.
Live Chat Icon For mobile
Up arrow icon