Java如何在iText PDF文档中旋转图像?
您可以使用setRotation(finalfloatr)或类的setRotationDegrees(finalfloatdeg)方法旋转iTextpdf文档中的图像com.itextpdf.text.Image。此方法分别以弧度和度为单位设置旋转。让我们看下面的例子:
package org.nhooo.example.itextpdf; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileOutputStream; import java.io.IOException; public class ImageRotateDemo { public static void main(String[] args) { Document doc = new Document(); try { PdfWriter.getInstance(doc, new FileOutputStream("ImageRotation.pdf")); doc.open(); //使用setRotation方法以弧度旋转图像。 String filename = "other-sample/src/main/resources/java.gif"; Image image = Image.getInstance(filename); image.setRotation(90f); doc.add(image); //下一行可以防止“服务器退回"Server returned //HTTP响应代码:403“错误。" error. System.setProperty("http.agent", "Chrome"); //使用setRotationDegree方法按度旋转图像 String url = "https://nhooo.org/wp-content/uploads/2017/01/nhooo.png"; image = Image.getInstance(url); image.setRotationDegrees(90); doc.add(image); } catch (DocumentException | IOException e) { e.printStackTrace(); } finally { doc.close(); } } }
Maven依赖
<!-- http://repo1.maven.org/maven2/com/itextpdf/itextpdf/5.5.10/itextpdf-5.5.10.jar --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.10</version> </dependency>