Java--Draw Rotated Text in PDF
In certain circumstances, it may be necessary to create a PDF document with rotated text, and this article will share how to achieve this task programmatically using a free Java API.
Installation
The free API used is Free Spire.PDF for Java, and there are 2 methods to install the product.
Method 1: Download and unzip it. Then add the Spire.Pdf.jar file to your project as dependency.
Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
Sample Code
Free Spire.PDF for Java allows users to draw text rotated at different angles clockwise or counterclockwise. The complete sample code is shown below.
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
public class drawRotatedText {
public static void main(String[] args) {
//Create a pdf document
PdfDocument doc = new PdfDocument();
//Add a new page
PdfPageBase page = doc.getPages().add();
//Set text font, size and color
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10f);
PdfSolidBrush brush = new PdfSolidBrush(new PdfRGBColor(Color.BLUE));
PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.RED));
String text = "Draw Rotated text in PDF";
//Set the text position
page.getCanvas().drawString(text, font, brush, 20, 30);
page.getCanvas().drawString(text, font, brush, 20, 200);
//Save graphics state
PdfGraphicsState state = page.getCanvas().save();
Point point1 = new Point(20, 0);
//Draw the text - transform
page.getCanvas().translateTransform(20, 30);
//Rotate 90 degrees clockwise
page.getCanvas().rotateTransform(90);
page.getCanvas().drawString(text, font, brush1, point1);
//Restore graphics
page.getCanvas().restore(state);
//Redrawing a new text requires initializing a new state
PdfGraphicsState state2 = page.getCanvas().save();
Point point2 = new Point(20, 0);
page.getCanvas().translateTransform(20, 200);
//Rotate 90 degrees counterclockwise
page.getCanvas().rotateTransform(-90);
page.getCanvas().drawString(text, font, brush, point2);
//Redrawing a new text requires initializing a new state
PdfGraphicsState state3 = page.getCanvas().save();
Point point3 = new Point(20, 0);
page.getCanvas().translateTransform(20, 0);
//Rotate 60 degrees clockwise
page.getCanvas().rotateTransform(60);
page.getCanvas().drawString(text, font, brush, point3);
//Redrawing a new text requires initializing a new state
PdfGraphicsState state4 = page.getCanvas().save();
Point point4 = new Point(20, 0);
page.getCanvas().translateTransform(20, 0);
//Rotate 30 degrees counterclockwise
page.getCanvas().rotateTransform(-30);
page.getCanvas().drawString(text, font, brush1, point4);
//Save the result file
doc.saveToFile("drawRotatedText.pdf");
}
}