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

Rotate a single line of text in PDF with DrawString

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

1 Reply

PH Praveenkumar H Syncfusion Team September 2, 2014 04:45 AM UTC

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

Loader.
Live Chat Icon For mobile
Up arrow icon