跳至正文
首页 » 博客 » How to Rotate Text in PDF using Java

How to Rotate Text in PDF using Java

Various PDF libraries offer you the feature of rotating text in PDF files using Java. However, you don’t get high-speed processing, high-quality output, and an easy-to-code solution at the same time. This article provides you with one of the best and easiest methods to rotate text in PDF using Java following a few simple steps. Also, it covers a couple of ways to rotate PDF text that you can adopt according to your needs. So let’s being and see how to apply rotation on text in a PDF file using Java.

  • Java Library to Rotate Text in PDF
  • Rotate Text inside a PDF File
    • Apply Text Rotation using TextFragment
    • Apply Text Rotation using TextParagraph

Java Library to Rotate Text in PDF#

To rotate text in 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 library or install it using the following Maven configurations.

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.12</version>
</dependency>

How to Rotate Text in PDF in Java#

There are multiple ways to rotate a text in a PDF using Java. You can either rotate a text fragment or the complete paragraph. Let’s see how each of these text rotations works.

Java: Rotate Text in PDF using TextFragment#

The following are the steps to rotate a text fragment in a PDF 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.

Java Code to Rotate Text in PDF#

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

Output#

Apply Text Rotation in PDF using TextParagraph#

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 with Java.

Output#

Free Java Library to Rotate Text in PDF#

You can get a free temporary license and rotate text in PDF files without any limitations.

Explore Java PDF Library#

You can explore more about Java PDF library using the documentation. In case you would have any questions or queries, you can contact us via our forum.

Conclusion#

In this article, you have learned how to rotate text in PDF using Java. We have covered how to rotate text using TextFragment and TextParagraph classes. You can simply install the library and use the provided code samples in your Java application.

See Also#

  • Creating PDF Files from Scratch using Java