写入图像到Java中的PDF文件 [英] Writing image into pdf file in java

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

问题描述

我正在写一个code,将微软电源点(PPT)滑入图像和对生成的图像写入PDF文件。继code生成,图像写入到PDF文件中,但我面对的是,当我写的图像转换成PDF文件,它的大小超过了PDF页面的大小,我可以看到只有75%的形象其余的是看不见的问题。还有一件事这里需要注意的是,在PDF文件书写的笔迹看起来像缩小或扩大。看看code以下片段:

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.

下面是code它实现了我希望的任务。现在,我下面布鲁诺Lowagie建议后得到了预期的效果。

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

不过,正如布鲁诺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();
    }
}

感谢您

推荐答案

第一本:如果巴新存储为C:/ DATASTORE /幻灯片 - +(我+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.

这:您的code段不向我们展示了如何创建文件对象。缺省情况下,页大小为A4纵向。它不用说,这是图像大于595点¯x842不适合该网页。

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();

CAVEAT 1:
如果您的PDF仅持有最后一张幻灯片,你可能把同一页上的所有幻灯片和最后一张幻灯片覆盖它​​们。你需要每次添加一个图像(如在我的回答一个code段完成)来调用 NEWPAGE()方法。

CAVEAT 2:
你的指控是错误的。根据API文档,还有一种方法<一href=\"http://api.itextpdf.com/itext/com/itextpdf/text/Document.html#setPageSize%28com.itextpdf.text.Rectangle%29\"相对=nofollow> setPageSize(矩形RECT) ,也许你用错了长方形类。如果你没有按照我的建议(这恕我直言,不会是明智的),你可能找 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.

CAVEAT 3:
这是类似的警告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天全站免登陆