Android中的7.1.3的iText方法

时间:2019-01-11 06:54:52

标签: java android itext

我不能使用PdfWriter类的getInstance()方法,也不能使用文档的open()方法...如何解决?

我有iText package 7.1.3

我的代码如下:

PdfWriter.class.getConstructor(document, new FileOutputStream(FILE));

document.open();

我正在将图像转换为pdf

1 个答案:

答案 0 :(得分:1)


这是从tutorialspoint挑选的代码的基本演示,希望对您有帮助。

// Creating a PdfWriter 
String dest = "C:/hellodearcode/imgtest.pdf"; 
PdfWriter writer = new PdfWriter(dest);
// Creating a PdfDocument  
PdfDocument pdfDoc = new PdfDocument(writer); 
// Creating a Document OBJECT
document = new Document(pdfDoc); 
// Creating an ImageData object 
String imageFile = "C:/hellodearcode/tabishrana-blog.jpg"; 
ImageData data = ImageDataFactory.create(imageFile);
// Creating an Image object 
Image img = new Image(data); 
// close document
document.close();

您应该按照此对象创建流程进行成功调用

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 AddingImage {      
   public static void main(String args[]) throws Exception {              

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

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

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

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

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

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

      // Closing the document       
      document.close();              

      System.out.println("Image added");    
   } 
}