📜  使用Java旋转 PDF 文档中的图像

📅  最后修改于: 2022-05-13 01:55:39.081000             🧑  作者: Mango

使用Java旋转 PDF 文档中的图像

在本文中,我们将学习如何使用Java旋转 PDF 文档中的图像。对于旋转 PDF 中的图像,我们将使用 iText 库。这些是使用Java在 PDF 中旋转图像应遵循的步骤。

1. 创建一个 PdfWriter 对象

PdfWriter 类代表 PDF 的 DocWriter。此类的构造函数接受一个字符串,即要创建 PDF 的文件的路径。

// importing the PdfWriter class. 
import com.itextpdf.kernel.pdf.PdfWriter; 

// path where the pdf is to be created. 
String path = "C:/JavaPdf/rotateImage.pdf"; 
PdfWriter pdfwriter = new PdfWriter(path);

2.创建一个 PdfDocument 对象

PdfDocument 类是 iText 中表示 PDF 文档的类,要在写入模式下实例化此类,您需要将类 PdfWriter(即上述代码中的 pdfwriter)的对象传递给其构造函数。

// Creating a PdfDocument object. 
// passing PdfWriter object constructor of pdfDocument. 
PdfDocument pdfdocument = new PdfDocument(pdfwriter); 

3. 创建文档对象

Document 类是创建自给自足的 PDF 时的根元素。此类的构造函数之一接受 PdfDocument 类(即 pdfdocument)类型的对象。

// Creating a Document and passing pdfDocument object 
Document document = new Document(pdfdocument); 

4. 创建一个 Image 对象

我们需要图像对象来管理图像。为了创建一个图像对象,我们需要创建一个 ImageData 对象。我们可以通过将表示图像路径的字符串参数传递给 ImageDataFactory 类的 create() 方法来创建它。现在我们可以通过将 ImageData 对象作为参数传递给 Image 类的构造函数来创建一个图像对象。

// Create an ImageData object 
String imageFile = "F:/JavaPdf/image.jpg"; 
ImageData data = ImageDataFactory.create(imageFile); 

// Creating an Image object 
Image image = new Image(data);

5. 旋转图像

要旋转图像,我们使用 setRotationAngle(),我们必须传递一个整数,表示我们希望旋转图像的旋转角度。

// Rotating the image 
image.setRotationAngle(90);

6. 添加图片到 PDF 文档

使用Document类的add()方法添加图片对象,使用Document类的close()方法关闭文档

// Adding image to the document 
document.add(image); 

// Closing the document  
document.close();

示例:这是帮助我们理解如何使用Java旋转 PDF 文档中的图像的最终代码。

Java
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 RotateImage {
    public static void main(String args[]) throws Exception
    {
  
        try {
            // path where the pdf is to be created.
            String path = "F:/JavaPdf/RotateImage.pdf";
            
            // Creating a PdfWriter
            PdfWriter pdfwriter = new PdfWriter(path);
  
            // Creating a PdfDocument object.
            // passing PdfWriter object constructor of
            // pdfDocument.
            PdfDocument pdfdocument
                = new PdfDocument(pdfwriter);
  
            // Creating a Document and passing pdfDocument
            // object
            Document document = new Document(pdfdocument);
  
            // Create an ImageData object
            String imageFile = "F:/JavaPdf/image.jpg";
            ImageData data
                = ImageDataFactory.create(imageFile);
            
            // Creating an Image object
            Image image = new Image(data);
            
            // Creating an Image object
            Image image = new Image(data);
  
            // Rotating the image
            image.setRotationAngle(90);
  
            // Adding image to the document
            document.add(image);
            
            // Closing the document
            document.close();
  
            System.out.println(
                "Image has been rotated successfully");
        }
        catch (Exception e) {
            System.out.println(
                "failed to rotate the image in the file due to "
                + e);
        }
    }
}


编译和执行

javac RotateImage.java 
java RotateImage

输出

Image has been rotated successfully

PDF

旋转图像