itext 7 pdf:为BufferedImage中的每个图像添加页面到pdf

时间:2020-11-04 15:29:53

标签: itext itext7

因此,我在Itext5中使用了代码来将PPT中的每张幻灯片作为图像添加到PDF。

我发现升级代码后不再编译。 这是我从PowerPoint获取图像的方法,如何将这些图像作为页面添加到pdf?我正在将每张幻灯片转换为缓冲的图像并使用Graphics2D进行绘制。

public void convertPptToPdf(Double scaling, float compression) throws Exception {
    String path = "myfile" + scaling + "-compression" + compression + "-";
    long startTime = System.nanoTime();
    ClassLoader classLoader = getClass().getClassLoader();
    File theDir =
        new File("myfile" + scaling + "-compression" + compression + "/");
    theDir.mkdirs();
    File file = new File(classLoader.getResource("myfile.ppt").getFile());
    FileInputStream is = new FileInputStream(file);
    HSLFSlideShow ppt = new HSLFSlideShow(is);
    is.close();
    Dimension pgsize = ppt.getPageSize();
    Double scaledPageSizeHeightDouble = pgsize.height * scaling;
    Double scaledPageSizeWidthDouble = pgsize.width * scaling;
    Image slideImage = null;
    // convert to images
    int idx = 1;
    for (HSLFSlide slide : ppt.getSlides()) {

      BufferedImage img =
          new BufferedImage(
              pgsize.width * scaling.intValue(),
              pgsize.height * scaling.intValue(),
              BufferedImage.TYPE_INT_RGB);
      Graphics2D graphics = img.createGraphics();
      // these settings make it higher quality
      // without it text is very hard to read
      graphics.setRenderingHint(
          RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
      graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      graphics.setRenderingHint(
          RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
      graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
      graphics.setRenderingHint(
          RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      graphics.setRenderingHint(
          RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
      // clear the drawing area
      graphics.setPaint(Color.white);
      graphics.scale(scaling, scaling);
      graphics.fill(
          new Rectangle2D.Float(
              0,
              0,
              scaledPageSizeWidthDouble.floatValue(),
              scaledPageSizeHeightDouble.floatValue()));
      // render
      slide.draw(graphics);
      idx++;
    }


  }

0 个答案:

没有答案