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.

Rotate a single line of text in PDF with DrawString

Thread ID:

Created:

Updated:

Platform:

Replies:

117178 Aug 28,2014 05:09 AM Sep 2,2014 12:45 AM Windows Forms 1
loading
Tags: PDF
Luca Cavicchioli
Asked On August 28, 2014 05:09 AM

Hello, I need to rotate a single line of text, of a specified angle, while I'm composing a PDF document, by reading fields from a database.
A code snippet that explains what I want to achieve is something like this:

// Rows contains a collection of objects that represents all the element of the final PDF file
foreach (var row in rows)
{
    // Considering only the string objects (the only with a rotation angle)
    if(row.Type == "S")
        DrawString(row.X, row.Y, row.Text, row.RotationAngle, [...]);
    else if(row.Type == "P")
        [...Do other stuff...]    
}

// For simplicity, all the stuff for create font and format are omitted
private void DrawString(float posX, float posY, string text, int rotationAngle, [...])
{
     var font = new PdfTrueTypeFont([...]);
     var format = new PdfStringFormat { Alignment = PdfTextAlignment.Left, WordWrap = PdfWordWrapType.Word };

    _page.Graphics.DrawString(text, font, PdfBrushes.Black, new PointF(X, Y), format);

// And what about the rotation angle???
// If 90 is passed as RotationAngle I'like to have ONLY this string rotated by 90 degrees.

}

For completeness, if I have these data:
X = 10, Y = 10, Text = "Straight", RotationAngle = 0
X = 20, Y = 10, Text = "Rotated", RotationAngle = 90
X = 30, Y = 10, Text = "Already Straight", TotationAngle = 0
I would like to have a PDF that looks like the picture attached

Attachment: Sample_e396ddda.zip

Praveenkumar H [Syncfusion]
Replied On September 2, 2014 12:45 AM

Hi Luca,

Thank you for using syncfusion products,

We can rotate the text as follow.

 //save the current graphics states
            PdfGraphicsState state = page.Graphics.Save();

            //Translate the coordinate system’s to where you want draw the text position
            page.Graphics.TranslateTransform(position.X,position.Y);
            //Rotate the coordinate system’s
            page.Graphics.RotateTransform(angle);
            //Draw the string at the origin
            page.Graphics.DrawString(text, bigFont, PdfBrushes.DarkBlue, new PointF(0, 0));

            //Restore the graphics state
            page.Graphics.Restore(state);

Wee have attached sample project for your reference.

Please let us know if you need further assistance.

With Regards,

Praveen


Attachment: VerticalText_5cfdca5f.zip

CONFIRMATION

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.

;