PDF has become a ruling document format in the digital world. Its cross-platform support has made it a widely adopted format for generating and sharing documents, such as research articles, invoices, etc. While generating a PDF programmatically, you often need to change the position and orientation of the text according to the layout of the document. In this article, we will demonstrate how to rotate text inside PDF documents programmatically in C# .NET.
C# .NET API to Rotate Text in PDF
Aspose.PDF for .NET is a C# class library that provides basic as well as advanced PDF manipulation features for .NET applications. Using the API, you can generate PDF documents with simple or complex layouts seamlessly. We will use this API to rotate text inside PDF documents. You can either download the API’s DLL or install it using NuGet.
PM> Install-Package Aspose.PDF
Rotate Text inside PDF in C#
There are multiple ways to rotate a text inside a PDF document. You can either rotate a text fragment or the complete paragraph. Let’s see how each of these text rotations works.
PDF Text Rotation using TextFragment in C#
The following are the steps to rotate a text fragment in a PDF document 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 document in C#.
Output
PDF Text Rotation 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 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 inside a paragraph in PDF programmatically.
Output
Get a Free API License
You can try Aspose.PDF for .NET for free by getting a temporary license.
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 programmatically using C#. We have explicitly covered how to rotate text using TextFragment and TextParagraph classes. Besides, you can explore other features using the documentation of Aspose.PDF for .NET. In case you would have any questions or queries, you can contact us via our forum.