如何将PDF中横向文本改为纵向显示?附相关Java代码片段
I currently have Java code that displays text horizontally in a PDF, and I want to modify it to show the text vertically instead. Here's my existing code snippet:
private void stampPdf(InputStream source, OutputStream dest) throws Exception { PdfDocument pdfDoc = new PdfDocument(new PdfReader(source), new PdfWriter(dest)); Document doc = new Document(pdfDoc); Paragraph header = new Paragraph("Received by on") .setFont(PdfFontFactory.createFont(FontConstants.HELVETICA)) .setFontSize(14) .setFontColor(Color.RED) .setRotationAngle(90); for (int i = 1; i <= pdfDoc.getNumberOfPages(); i++) { doc.add(header); } doc.close(); pdfDoc.close(); }How should I adjust this code to make the text display vertically?
Hey there! Let's get that text displaying properly vertically in your PDF. The main issue in your current code is a super common iText pitfall: rotation angles use radians, not degrees. When you pass 90 directly, you're rotating the text 90 radians (that's 14 full rotations plus extra), which is definitely not what you want. Here's how to fix it, plus tips to make sure the text lands where you need it:
Step 1: Fix the Rotation Angle
To get vertical text, convert your desired degree rotation to radians using Math.toRadians(). There are two common vertical orientations to choose from:
- Bottom-to-top vertical: Use
Math.toRadians(90)(clockwise rotation) - Top-to-bottom vertical: Use
Math.toRadians(-90)orMath.toRadians(270)(counter-clockwise rotation)
Update your Paragraph setup like this:
Paragraph header = new Paragraph("Received by on") .setFont(PdfFontFactory.createFont(FontConstants.HELVETICA)) .setFontSize(14) .setFontColor(Color.RED) // Rotate 90 degrees clockwise (vertical, reads bottom-to-top) .setRotationAngle(Math.toRadians(90));
Step 2: Adjust Positioning (Critical!)
When you rotate text, its origin point (the bottom-left corner of the text block) stays in the default flow position. This often leads to the text being cut off or placed awkwardly. Use setFixedPosition() to control exactly where the rotated text goes:
// Grab the page size to calculate a centered position Rectangle pageSize = pdfDoc.getPage(1).getPageSize(); Paragraph header = new Paragraph("Received by on") .setFont(PdfFontFactory.createFont(FontConstants.HELVETICA)) .setFontSize(14) .setFontColor(Color.RED) .setRotationAngle(Math.toRadians(90)) // Position: 15px from left edge, vertical center, 120px "width" (vertical height) .setFixedPosition(15, pageSize.getHeight() / 2 - 40, 120);
The setFixedPosition() parameters break down to:
- Distance from the left edge of the page
- Distance from the bottom edge of the page (we adjust this to center vertically)
- Width of the text block (since it's rotated, this becomes the vertical space it takes up)
Full Modified Code
Here's the complete working version of your method, with proper rotation and positioning:
private void stampPdf(InputStream source, OutputStream dest) throws Exception { PdfDocument pdfDoc = new PdfDocument(new PdfReader(source), new PdfWriter(dest)); Document doc = new Document(pdfDoc); Rectangle pageSize = pdfDoc.getPage(1).getPageSize(); Paragraph header = new Paragraph("Received by on") .setFont(PdfFontFactory.createFont(FontConstants.HELVETICA)) .setFontSize(14) .setFontColor(Color.RED) .setRotationAngle(Math.toRadians(90)) .setFixedPosition(15, pageSize.getHeight() / 2 - 40, 120); for (int i = 1; i <= pdfDoc.getNumberOfPages(); i++) { doc.setPage(i); // Make sure we're adding to the correct page doc.add(header); } doc.close(); pdfDoc.close(); }
Quick Notes
- If you prefer top-to-bottom vertical text, swap the rotation angle to
Math.toRadians(-90) - Always double-check that you're converting degrees to radians—this is the #1 mistake with iText text rotation!
- Tweak the
setFixedPosition()values to fit your specific page layout needs.
内容的提问来源于stack exchange,提问作者Saravanan Sivalingam

