How to rotate a single line of text in pdf?
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:
- Create a new Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from Nuget.org.
- Include the following namespace in the Form1.cs file.
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf;
using System;
using System.Drawing;
- 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";
}
- 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.
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.