PDF is a cross-platform document format that provides a consistent layout across different operating systems. It is widely used for generating and sharing documents, such as research articles, invoices, etc. While generating a PDF files 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 show you how to rotate text inside PDF documents in Java.
Java API to Rotate Text in PDF
To rotate text inside PDF files, we will use Aspose.PDF for Java. It is a powerful library that provides basic as well as advanced PDF manipulation features. You can download the API or install it using the following Maven configurations.
Repository:
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>22.4</version>
</dependency>
Rotate Text inside PDF in Java
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 Java
The following are the steps to rotate a text fragment in a PDF document using Java.
- First, create a new document using the Document class.
- Then, add a page to the document and get its reference using Document.getPages().add() method.
- After that, create a new text fragment using TextFragment class.
- Set text fragment’s position and font.
- Set rotation angle using TextFragment.getTextState().setRotation() method.
- 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 Java.
Output
PDF Text Rotation using TextParagraph in Java
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.getPages().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.getTextState().setRotation() method.
- 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 the 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 Java for free by getting a temporary license.
Conclusion
In this article, you have learned how to rotate text in PDF programmatically using Java. We have covered how to rotate text using TextFragment and TextParagraph classes. In addition, you can explore more about Aspose.PDF for Java using the documentation. In case you would have any questions or queries, you can contact us via our forum.