如何正确地将iText5代码转换为iText7?

时间:2018-11-09 11:00:27

标签: java itext7

我是一位SQL / ETL(PowerCenter)/ bash / Python开发人员,对Java的经验很少。我有一个任务-我需要使用.pptx模板,对其进行自定义,然后转换为.pdf文件。我决定从第二步开始,所以我以this为例。我拥有最新版本的库(iText7和POI4),因此必须修改此代码才能进行编译。我能够在“导入”部分中找到移动的软件包,但随后我被卡在这里:

slideImage = Image.getInstance(img, null);

我的新库说 getInstance 不再受支持(找不到符号)。我试图跳过此步骤,并使用

的类似物
table.addCell(new PdfPCell(slideImage, true));

我已更改为

table.addCell(new Cell(img, true));

直接将此bufferedImage添加到单元格中,会引发转换错误,例如“ BufferedImage无法转换为int)。如何将BufferedImage转换为Image?我读到第一个是第二个的子代,所以没有需要转换它,但是它不起作用。下面我提供由我调整的代码。在此先感谢您!


import java.io.FileOutputStream;
import java.io.*;
import java.io.IOException;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hslf.record.Slide;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;

import com.itextpdf.layout.Document;

import com.itextpdf.layout.element.Image;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Table;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.geom.PageSize;

public void createPdf(String inFileName, String outFileName)
    throws IOException 
{
    FileInputStream inputStream = new FileInputStream(inFileName);
    double zoom = 2;
    AffineTransform at = new AffineTransform();
    at.setToScale(zoom, zoom);

    Table table = new Table(1);
    Dimension pgsize = null;
    Image slideImage = null;
    BufferedImage img = null;

    XMLSlideShow ppt = new XMLSlideShow(inputStream);
    pgsize = ppt.getPageSize();

//  PDF part
//  Initialize PDF writer
    PdfWriter writer = new PdfWriter(outFileName);
//  Initialize PDF document
    PdfDocument pdf = new PdfDocument(writer);
//  Initialize document
    Rectangle srcPageSize = new Rectangle((float) pgsize.getWidth(), (float) pgsize.getHeight());
    Document doc = new Document(pdf, new PageSize(srcPageSize));

    List<XSLFSlide> slides = ppt.getSlides();
//  writer.open();
//  pdfDocument.open();
    for (XSLFSlide slide : ppt.getSlides()) {
        img = new BufferedImage((int) Math.ceil(pgsize.width * zoom), (int) Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = img.createGraphics();
        graphics.setTransform(at);

        graphics.setPaint(Color.white);
        graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
        slide.draw(graphics);
        graphics.getPaint();

//  Original start
//      slideImage = Image.getInstance(img, null);
//      table.addCell(new PdfPCell(slideImage, true));
//  Original end


        table.addCell(new Cell(img, true));    
    }
//  pdfDocument.add(table);
//  pdfDocument.close();
//  pdfWriter.close();
    System.out.println("Powerpoint file converted to PDF successfully");

//  catch (IOException e) 
//  {
//      System.err.println("FileStreamsReadnWrite: " + e);
//  }
}

1 个答案:

答案 0 :(得分:1)

您可以尝试以下操作,该操作使用当前的API

// you need to convert the BufferedImage to a byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "png", baos);

ImageData image = ImageDataFactory.create(baos.toByteArray());

table.addCell(new Image(image));

还有其他有趣的方法,例如ImageDataFactory.create(String filename)