如何使用Java在PDF文档中旋转图像。(How to rotate an image in a PDF document using Java.)

优质
小牛编辑
127浏览
2023-12-01

问题描述 (Problem Description)

如何使用Java在PDF文档中旋转图像。

解决方案 (Solution)

以下是使用Java在PDF中旋转图像的程序。

import com.itextpdf.io.image.ImageData; 
import com.itextpdf.io.image.ImageDataFactory; 
import com.itextpdf.kernel.pdf.PdfDocument; 
import com.itextpdf.kernel.pdf.PdfWriter; 
import com.itextpdf.layout.Document; 
import com.itextpdf.layout.element.Image;  
public class RotatingImage { 
   public static void main(String args[]) throws Exception {     
      //Creating a PdfWriter 
      String dest = "C:/itextExamples/rotatingImage.pdf"; 
      PdfWriter writer = new PdfWriter(dest); 
      //Creating a PdfDocument 
      PdfDocument pdfDoc = new PdfDocument(writer); 
      //Creating a Document  
      Document document = new Document(pdfDoc); 
      //Creating an ImageData object 
      String imFile = "C:/itextExamples/logo.jpg"; 
      ImageData data = ImageDataFactory.create(imFile); 
      //Creating an Image object
      Image image = new Image(data);   
      //Rotating the image 
      image.setRotationAngle(45);      
      //Adding image to the document 
      document.add(image); 
      //Closing the document 
      document.close(); 
      System.out.println("Image rotated"); 
   }  
}

输出 (Output)

以PDF格式旋转图像