📜  iText-缩放图像

📅  最后修改于: 2020-10-16 02:53:27             🧑  作者: Mango


在本章中,我们将看到如何使用iText库缩放PDF文档中的图像。

缩放PDF中的图像

您可以通过实例化Document类来创建一个空的PDF Document。在实例化此类时,您需要将PdfDocument对象作为参数传递给其构造函数。

要将图像添加到PDF,请创建需要添加图像的对象,然后使用Document类的add()方法添加图像。您可以使用setAutoScale()方法缩放图像。

以下是缩放PDF文档中存在的图像的步骤。

步骤1:创建一个PdfWriter对象

PdfWriter类表示PDF的DocWriter。此类属于包com.itextpdf.kernel.pdf 。此类的构造函数接受一个字符串,该字符串表示要在其中创建PDF的文件的路径。

实例化PdfWriter类,方法是将一个字符串值(代表您需要在其中创建PDF的路径)传递给它的构造函数,如下所示。

// Creating a PdfWriter 
String dest = "C:/itextExamples/autoScale.pdf"; 
PdfWriter writer = new PdfWriter(dest); 

当将此类型的对象传递到PdfDocument(类)时,添加到此文档的每个元素都将被写入指定的文件。

步骤2:创建一个PdfDocument对象

PdfDocument类是表示iText中PDF文档的类。此类属于包com.itextpdf.kernel.pdf 。要实例化此类(以编写模式),您需要将PdfWriter类的对象传递给其构造函数。

通过将上面创建的PdfWriter对象传递给其构造函数来实例化PdfDocument类,如下所示。

// Creating a PdfDocument  
PdfDocument pdfDoc = new PdfDocument(writer); 

创建PdfDocument对象后,您可以使用其类提供的相应方法添加各种元素,如页面,字体,文件附件和事件处理程序。

步骤3:创建Document对象

com.itextpdf.layoutDocument类是创建自足PDF的根元素。此类的构造函数之一接受PdfDocument类的对象。

通过传递在先前步骤中创建的PdfDocument类的对象来实例化Document类,如下所示。

// Creating a Document   
Document document = new Document(pdfDoc);

步骤4:创建图像对象

要创建图像对象,首先,使用ImageDataFactory类的create()方法创建一个ImageData对象。作为此方法的参数,传递代表图像路径的字符串参数,如下所示。

// Creating an ImageData object 
String imageFile = "C:/itextExamples/javafxLogo.jpg"; 
ImageData data = ImageDataFactory.create(imageFile); 

现在,实例化com.itextpdf.layout.element包的Image类。实例化时,将ImageData对象作为参数传递给其构造函数,如下所示。

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

步骤5:缩放图片

您可以使用setAutoScale()方法缩放图像。

// Setting the position of the image to the center of the page 
image.setFixedPosition(100, 250); 

步骤6:将图像添加到文档

现在,使用Document类的add()方法添加在上一步中创建的图像对象,如下所示。

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

步骤7:关闭文档

使用Document类的close()方法关闭文档,如下所示。

// Closing the document 
document.close(); 

以下Java程序演示了如何使用iText库缩放与PDF文档上的文档大小相对应的图像。它创建一个名为autoScale.pdf的PDF文档,向其中添加图像,相对于页面尺寸进行缩放,然后将其保存在路径C:/ itextExamples /中

将此代码保存在名为SettingAutoScale.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 SettingAutoScale {      
   public static void main(String args[]) throws Exception{              
      // Creating a PdfWriter       
      String dest = "C:/itextExamples/positionOfImage.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);                
      
      // Setting the position of the image to the center of the page       
      image.setFixedPosition(100,250); 
      
      // Adding image to the document       
      document.add(image);              
      
      // Closing the document       
      document.close();
      System.out.println("Image Scaled");    
   } 
}  

使用以下命令从命令提示符处编译并执行保存的Java文件。

javac SettingAutoScale.java 
java SettingAutoScale 

执行后,上述程序将创建一个显示以下消息的PDF文档。

Image Scaled

如果验证指定的路径,则可以找到创建的PDF文档,如下所示。

自动缩放