Rotate Text inside PDF Documents in Java

Rotate Text inside PDF Documents in Java

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.

The following code sample shows how to rotate text in a PDF document in Java.

Output

PDF Text Rotation using TextFragment in Java

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.

The following code sample shows how to rotate text inside a paragraph in PDF programmatically.

Output

PDF Text Rotation using TextParagraph in Java

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.

See Also