If you are generating PDF files programmatically, you may need to rotate PDF text at a specific angle. Changing the orientation and position of a text in a PDF could be tricky. However, the solution provided in this article has made rotating text in PDF using C# quite easy. So let’s dig down and check how to rotate text in a PDF in C#.
C# Library to Rotate Text in PDF
To rotate text in PDF, we will use Aspose.PDF for .NET. It is a C# class library that provides basic as well as advanced PDF manipulation features for .NET applications. Using the library, you can generate PDF documents with simple or complex layouts seamlessly.
You can either download the library’s DLL or install it using NuGet.
PM> Install-Package Aspose.PDF
How to Rotate Text in PDF in C#
There are multiple ways to rotate text in a PDF file using C#. You can either rotate a text fragment or the complete paragraph. Let’s see how each of these text rotations works.
C# Rotate Text in PDF using TextFragment
The following are the steps to rotate a text fragment in a PDF using C#.
- First, create a new document using the Document class.
- Then, add a page to the document and get its reference using Document.Pages.Add() method.
- After that, create a new text fragment using TextFragment class.
- Set text fragment’s position and font.
- Set rotation angle using TextFragment.TextState.Rotation property.
- Create a new TextBuilder object and initialize it with the Page object.
- Use TextBuilder.AppendText(TextFragment) method to add text to the page.
- Finally, save the PDF document using Document.Save(string) method.
The following code sample shows how to rotate text in a PDF in C#.
// Initialize document object | |
Document pdfDocument = new Document(); | |
// Get particular page | |
var pdfPage = pdfDocument.Pages.Add(); | |
// Create text fragment | |
TextFragment textFragment1 = new TextFragment("main text"); | |
textFragment1.Position = new Position(100, 600); | |
// Set text properties | |
textFragment1.TextState.FontSize = 12; | |
textFragment1.TextState.Font = FontRepository.FindFont("TimesNewRoman"); | |
// Create rotated text fragment | |
TextFragment textFragment2 = new TextFragment("rotated text"); | |
textFragment2.Position = new Position(200, 600); | |
// Set text properties | |
textFragment2.TextState.FontSize = 12; | |
textFragment2.TextState.Font = FontRepository.FindFont("TimesNewRoman"); | |
textFragment2.TextState.Rotation = 45; | |
// Create rotated text fragment | |
TextFragment textFragment3 = new TextFragment("rotated text"); | |
textFragment3.Position = new Position(300, 600); | |
// Set text properties | |
textFragment3.TextState.FontSize = 12; | |
textFragment3.TextState.Font = FontRepository.FindFont("TimesNewRoman"); | |
textFragment3.TextState.Rotation = 90; | |
// create TextBuilder object | |
TextBuilder textBuilder = new TextBuilder(pdfPage); | |
// Append the text fragment to the PDF page | |
textBuilder.AppendText(textFragment1); | |
textBuilder.AppendText(textFragment2); | |
textBuilder.AppendText(textFragment3); | |
// Save document | |
pdfDocument.Save("Rotation_TextFragment.pdf"); |
Output
Rotate PDF Text using TextParagraph in C#
You can also apply rotation to the text while creating a new paragraph. This can be achieved using TextParagraph class. The following are the steps to apply text rotation in PDF using TextParagraph class.
- First, create a new document using the Document class.
- Then, add a page to the document and get its reference using Document.Pages.Add() method.
- After that, create a new TextParagraph object.
- Create a new text fragment using TextFragment class and set the text and font.
- Set rotation angle using TextFragment.TextState.Rotation property.
- Add text to paragraph using TextParagraph.AppendLine(TextFragment) method.
- Create a new TextBuilder object and initialize it with the Page object.
- Use TextBuilder.AppendParagraph(TextParagraph) method to add paragraph to the page.
- Finally, save the PDF document using Document.Save(string) method.
The following code sample shows how to rotate text in a paragraph of PDF in C#.
// Initialize document object | |
Document pdfDocument = new Document(); | |
// Get particular page | |
var pdfPage = pdfDocument.Pages.Add(); | |
TextParagraph paragraph = new TextParagraph(); | |
paragraph.Position = new Position(200, 600); | |
// Create text fragment | |
TextFragment textFragment1 = new TextFragment("rotated text"); | |
// Set text properties | |
textFragment1.TextState.FontSize = 12; | |
textFragment1.TextState.Font = FontRepository.FindFont("TimesNewRoman"); | |
// Set rotation | |
textFragment1.TextState.Rotation = 45; | |
// Create text fragment | |
TextFragment textFragment2 = new TextFragment("main text"); | |
// Set text properties | |
textFragment2.TextState.FontSize = 12; | |
textFragment2.TextState.Font = FontRepository.FindFont("TimesNewRoman"); | |
// Create text fragment | |
TextFragment textFragment3 = new TextFragment("another rotated text"); | |
// Set text properties | |
textFragment3.TextState.FontSize = 12; | |
textFragment3.TextState.Font = FontRepository.FindFont("TimesNewRoman"); | |
// Set rotation | |
textFragment3.TextState.Rotation = -45; | |
// Append the text fragments to the paragraph | |
paragraph.AppendLine(textFragment1); | |
paragraph.AppendLine(textFragment2); | |
paragraph.AppendLine(textFragment3); | |
// Create TextBuilder object | |
TextBuilder textBuilder = new TextBuilder(pdfPage); | |
// Append the text paragraph to the PDF page | |
textBuilder.AppendParagraph(paragraph); | |
// Save document | |
pdfDocument.Save("Rotation_TextParagraph.pdf"); |
Output
Free C# Library to Rotate Text in PDF
You can get a free temporary license and rotate text in PDF files without any limitations.
Explore C# PDF Library
You can explore other features of the C# PDF library using the documentation. In case you would have any questions or queries, you can contact us via our forum.
Conclusion
PDF automation is widely adopted to create and manipulate PDF documents from within the web or desktop applications. In this article, you have learned how to rotate text in PDF using C#. We have explicitly covered how to rotate text using TextFragment and TextParagraph classes. You can easily use the provided code samples in your applications after integrating our C# PDF library.