在java中将图像写入pdf文件 [英] Writing image into pdf file in java

查看:68
本文介绍了在java中将图像写入pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写代码将 Microsoft power-point(ppt) 幻灯片转换为图像并将生成的图像写入 pdf 文件.以下代码生成并将图像写入 pdf 文件,但我面临的问题是,当我将图像写入 pdf 文件时,它的大小超过了 pdf 页面大小,我只能查看 75% 的图像其余部分是不可见的.这里要注意的另一件事是,pdf 文件中的书面图像看起来像缩放或扩展.看看下面的代码片段:

I'm writing a code to convert Microsoft power-point(ppt) slides into images and to write the generated images into pdf file. Following code generates and writes the images into pdf file but the problem i'm facing is, when i write image into pdf file it's size is exceeding the pdf page size and i can view only 75% of the image rest is invisible. One more thing to notice here is, written images in pdf file look like zoomed or expanded. Take a look at the following snippet of code:

for (int i = 0; i < slide.length; i++) {
    BufferedImage img = new BufferedImage(pgsize.width, pgsize.height,   BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = img.createGraphics();
    graphics.setPaint(Color.white);
    graphics.fill(new Rectangle(0, 0, pgsize.width, pgsize.height));
    slide[i].draw(graphics);
    fileName="C:/DATASTORE/slide-"+(i+1)+".png";
    FileOutputStream out = new FileOutputStream(fileName);
    javax.imageio.ImageIO.write(img, "png", out);
out.flush();
out.close();
com.lowagie.text.Image image =com.lowagie.text.Image.getInstance(fileName);
            image.setWidthPercentage(40.0f);
    doc.add((image));
    }

doc.close();
} catch(DocumentException de) {
          System.err.println(de.getMessage());
    }

如果有人知道解决方案,请帮我纠正.谢谢你.

If anybody knows the solution please help me to rectify. Thank you.

这是完成我希望的任务的代码.现在,我在遵循 Bruno Lowagie 的建议后得到了想要的结果.

Here is the code it accomplishes the task i wished. Now i'm getting the desired results after following Bruno Lowagie recommendations.

但是,正如 Bruno Lowagie 之前指出的,它们是生成的 png 图像中的一个问题.生成的 png 图像不正确,因为幻灯片中的形状或图像与幻灯片的文本重叠.你能帮我找出并纠正错误吗?

But, as Bruno Lowagie pointed out earlier, their is a problem in generated png image. The generated png image is not correct because shape or image in the slide overlaps with the texts of the slide. Can you help me to identify and rectify the error?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import com.itextpdf.text.Image;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
public class ConvertSlidesIntoImages {
    public static void main(String[] args){
    try {
        FileInputStream is = new FileInputStream("C:/DATASTORE/testPPT.ppt");
        SlideShow ppt = new SlideShow(is);
        is.close();
        String fileName;
        Dimension pgsize = ppt.getPageSize();
        Slide[] slide = ppt.getSlides();
        Document doc=new Document();
        PdfWriter.getInstance(doc, new  FileOutputStream("c:/DATASTORE/convertPPTSlidesIntoPDFImages.pdf"));
        doc.open();

        for (int i = 0; i < slide.length; i++) {
            BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = img.createGraphics();
            graphics.setPaint(Color.white);
            graphics.fill(new Rectangle(0, 0, pgsize.width, pgsize.height));
            slide[i].draw(graphics);
            fileName="C:/DATASTORE/slide-"+(i+1)+".png";
            FileOutputStream out = new FileOutputStream(fileName);
            javax.imageio.ImageIO.write(img, "png", out);
            out.flush();
            out.close();
            com.itextpdf.text.Image image =com.itextpdf.text.Image.getInstance(fileName);
            doc.setPageSize(new com.itextpdf.text.Rectangle(image.getScaledWidth(), image.getScaledHeight()));
            doc.newPage();
            image.setAbsolutePosition(0, 0);
            doc.add(image);
            }
    doc.close();
}catch(DocumentException de) {
    System.err.println(de.getMessage());
    }
    catch(Exception ex) {
    ex.printStackTrace();
    }
}

谢谢

推荐答案

首先: 如果 png 存储为 "C:/DATASTORE/slide-"+(i+1)+".png" 不正确,PDF 中的幻灯片也不正确.

First this: If the png stored as "C:/DATASTORE/slide-"+(i+1)+".png" isn't correct, the slide in the PDF won't be correct either.

还有这个:您的代码片段没有向我们展示您如何创建 Document 对象.默认情况下,纵向页面大小为 A4.不用说,大于 595 x 842 的图像不适合该页面.

And this: Your code snippet doesn't show us how you create the Document object. By default, the page size is A4 in portrait. It goes without saying that images that are bigger than 595 x 842 don't fit that page.

现在答案是:有两种方法可以解决这个问题.

Now the answer: There are two ways to solve this.

要么更改图像的大小(使用 setWidthPercentage() 除非您已经计算了实际百分比),然后添加它是位置 (0, 0) 以便它不考虑边距.例如:

Either you change the size of the image (not with setWidthPercentage() unless you've calculated the actual percentage) and you add it a the position (0, 0) so that it doesn't take into account the margins. For instance:

image.scaleToFit(595, 842);
image.setAbsolutePosition(0, 0);
doc.add(image);
doc.newPage();

更好的解决方案是根据图像大小调整页面大小.

A better solution would be to adapt the size of the page to the size of the image.

Document doc = new Document(new Rectangle(image.getScaledWidth(), image.getScaledHeight()));
// create a writer, open the document
image.setAbsolutePosition(0, 0);
doc.add(image);
doc.newPage();

如果图片大小不同,您可以在添加图片时更改页面大小,如下所示:

If the size of the images varies, you can change the page size while adding images like this:

doc.setPageSize(new Rectangle(image.getScaledWidth(), image.getScaledHeight()));
doc.newPage();
image.setAbsolutePosition(0, 0);
doc.add(image);

重要的是要了解新的页面大小只会在 doc.newPage();

It is important to understand that the new page size will only come into effect after doc.newPage();

注意事项 1:如果您的 PDF 仅包含最后一张幻灯片,则您可能将所有幻灯片放在同一页面上,而最后一张幻灯片将它们全部覆盖.每次添加图像时都需要调用 newPage() 方法(如我的回答中的代码片段所示).

CAVEAT 1: If your PDF only holds the last slide, you're probably putting all the slides on the same page, and the last slide covers them all. You need to invoke the newPage() method each time you add an image (as done in a code snippet in my answer).

注意事项 2:你的指控是错误的.根据 API 文档,有一种方法 setPageSize(Rectangle rect),也许你使用了错误的 Rectangle 类.如果您没有遵循我的建议(恕我直言不明智),您可能正在寻找 com.lowagie.text.Rectangle 而不是 java.awt.Rectangle代码>.

CAVEAT 2: Your allegation is wrong. According to the API docs, there is a method setPageSize(Rectangle rect), maybe you're using the wrong Rectangle class. If you didn't follow my advice (which IMHO wouldn't be wise), you're probably looking for com.lowagie.text.Rectangle instead of java.awt.Rectangle.

注意事项 3:这类似于 CAVEAT 2,java.awt.Image 类中确实没有这样的方法,但如 API 文档中所述,com.itextpdf.text.Image 有一个 getScaleWidth() 方法和 getScaledHeight() 方法.

CAVEAT 3: This is similar to CAVEAT 2, there are indeed no such methods in the class java.awt.Image, but as documented in the API docs, the class com.itextpdf.text.Image has a getScaleWidth() method and a getScaledHeight() method.

这篇关于在java中将图像写入pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆