Articles in this section
Category / Section

How to rotate a single line of text in pdf?

1 min read

The Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can rotate a single line of text in Pdf by saving the current graphics state of the page. Then translate the coordinate system to where you want to draw the text, rotate the text by passing an angle. Finally draw the string and restore the graphics state.

Steps to rotate a single line of text in PDF programmatically:

  1. Create a new Windows Forms application project.wf-56523_img1.png
  2. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from Nuget.org.
    Screenshot (1478).png
  3. Include the following namespace in the Form1.cs file.
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf;
using System;
using System.Drawing;
  1. Add a new button in Form1.Designer.cs to rotate a single line of text in PDF.
private void InitializeComponent()
{
   btnCreate = new Button();
   label = new Label();

   //Label
   label.Location = new System.Drawing.Point(0, 40);
   label.Size = new System.Drawing.Size(426, 35);
   label.Text = "Click the button to generate PDF file by Essential PDF";
   label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

   //Button
   btnCreate.Location = new System.Drawing.Point(180, 110);
   btnCreate.Size = new System.Drawing.Size(85, 26);
   btnCreate.Text = "Create PDF";
   btnCreate.Click += new EventHandler(btnCreate_Click);

   //Create PDF
   ClientSize = new System.Drawing.Size(450, 150);
   Controls.Add(label);
   Controls.Add(btnCreate);
   Text = "Create PDF";
}
  1. Create the btnCreate_Click event and add the below code sample to rotate a single line of text in PDF.
PdfFont pdfFont = new PdfStandardFont(PdfFontFamily.Helvetica, 30f);
       private void btnCreate_Click(object sender, EventArgs e)
   	{
           //Creates a new pdf document
           PdfDocument document = new PdfDocument();
           //Adds the page
           PdfPage page = document.Pages.Add();

           page.Graphics.DrawString("Straight", pdfFont, PdfBrushes.Black, new PointF(10, 10));

           DrawRotatedText("Rotated", new PointF(50, 200), 90, page);

           page.Graphics.DrawString("Already Straight", pdfFont, PdfBrushes.Black, new PointF(10, 300));
           //Save the document and dispose it
           document.Save("verticalText.pdf");
           document.Close();
       }
       private void DrawRotatedText(String text, PointF position, float angle, PdfPage page)
       {
           //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, pdfFont, PdfBrushes.DarkBlue, new PointF(0, 0));

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

A complete working sample can be download from Rotated_Text.zip

By executing the program, you will get the PDF document as follows.Screenshot (1479).png

Take a moment to peruse the documentation, where you can find other options like draw text in PDF using standard and true type fonts, display Unicode text, draw Right-To-Left text, HTML styled text, inserting rich text format content, draw complex script language text, create multicolumn PDF, ordered list and unordered list with code examples.

Refer to this link to explore a rich set of Syncfusion Essential PDF features.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied