iText生成pdf(图片转PDF)

jopen 11年前

      前言

               在学习的过程中笔者遇到过这种需求,就是将某个截取到的图片转化为PDF格式的。图片如何

          转化为PDF格式的呢?首先想到的是PDF格式的文档是如何生成的,知道了这个就知道如何将图

          片“插入”到PDF文档中了。那么java有没有生成PDF文档的第三方jar呢?答案是肯定的。

       iText类库    

               iText是著名的开放项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或

          rtf等文档,甚至可以将XML、Html文件转化为PDF格式。

               官方网址和开发文档api:

                http://itextpdf.com

               官方下载地址:

                http://sourceforge.net/projects/itext/files/

               了解了iText类库的功能,我们简要的看看如何来使用他把。这里笔者只是演示一下如何将一张

          图片转换为一个pdf文件,简单的生成一个pdf并插入图片和文字。

        图片生成PDF

               图片是如何生成pdf的呢?我们直接看源码吧:

    package com.kiritor;                import java.io.File;        import java.io.FileNotFoundException;        import java.io.FileOutputStream;        import java.io.IOException;        import java.util.ArrayList;                        import com.itextpdf.text.Document;        import com.itextpdf.text.DocumentException;        import com.itextpdf.text.Image;        import com.itextpdf.text.PageSize;        import com.itextpdf.text.Paragraph;        import com.itextpdf.text.pdf.PdfWriter;                public class PdfManager {            public static File Pdf(ArrayList<String> imageUrllist,String mOutputPdfFileName) {                String TAG = "PdfManager";                Document doc = new Document(PageSize.A4, 20, 20, 20, 20);                try {                    PdfWriter                            .getInstance(doc, new FileOutputStream(mOutputPdfFileName));                    doc.open();                    for (int i = 0; i < imageUrllist.size(); i++) {                        doc.newPage();                        doc.add(new Paragraph("简单使用iText"));                        Image png1 = Image.getInstance(imageUrllist.get(i));                        float heigth = png1.getHeight();                        float width = png1.getWidth();                        int percent = getPercent2(heigth, width);                        png1.setAlignment(Image.MIDDLE);                        png1.scalePercent(percent+3);// 表示是原来图像的比例;                        doc.add(png1);                    }                    doc.close();                } catch (FileNotFoundException e) {                    e.printStackTrace();                } catch (DocumentException e) {                    e.printStackTrace();                } catch (IOException e) {                    e.printStackTrace();                }                        File mOutputPdfFile = new File(mOutputPdfFileName);                if (!mOutputPdfFile.exists()) {                    mOutputPdfFile.deleteOnExit();                    return null;                }                return mOutputPdfFile;            }                    /**            * 第一种解决方案 在不改变图片形状的同时,判断,如果h>w,则按h压缩,否则在w>h或w=h的情况下,按宽度压缩            *            * @param h            * @param w            * @return            */                    public static int getPercent(float h, float w) {                int p = 0;                float p2 = 0.0f;                if (h > w) {                    p2 = 297 / h * 100;                } else {                    p2 = 210 / w * 100;                }                p = Math.round(p2);                return p;            }                    /**            * 第二种解决方案,统一按照宽度压缩 这样来的效果是,所有图片的宽度是相等的,自我认为给客户的效果是最好的            *            * @param args            */            public static int getPercent2(float h, float w) {                int p = 0;                float p2 = 0.0f;                p2 = 530 / w * 100;                p = Math.round(p2);                return p;            }        }  
         具体是如何调用的呢?
    package com.kiritor;                import java.io.File;        import java.io.IOException;        import java.util.ArrayList;                public class ToPDF {          public static void main(String[] args) {            // TODO Auto-generated method stub                ArrayList<String> imageUrllist = new ArrayList<String>();                imageUrllist.add("C:\\Users\\Kiritor\\Desktop\\matlab\\" + "1" + ".jpg");                String pdfUrl = "C:\\Users\\Kiritor\\Desktop\\matlab\\Foreverlove.pdf";                File file = PdfManager.Pdf(imageUrllist, pdfUrl);                try {                    file.createNewFile();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }        }        }  
         我们预期的效果是通过一张图片生成一个pdf文档,并且图片前面添加了iText文字。

          我们在指定的目录下看看是否生成了pdf以及其内容吧。

              该目录下确实生成了pdf文档,且里面的内容确实符合要求。

                20130516184639866.png

            由于笔者只是需要将pic转成pdf其他的详细功能笔者也就没有深究,读者可以查阅API自己

        去实现相关操作(实现一个pdf编辑器?)。