黑马程序员技术交流社区

标题: 文件转换工具 ImageToPDF [打印本页]

作者: 朱婵    时间: 2014-2-20 13:01
标题: 文件转换工具 ImageToPDF


  1. import java.io.IOException;

  2. import com.itextpdf.text.Rectangle;
  3. import com.itextpdf.text.DocumentException;
  4. import java.io.FileOutputStream;

  5. import com.itextpdf.text.Document;

  6. import com.itextpdf.text.Image;

  7. import com.itextpdf.text.pdf.PdfWriter;
  8. /**
  9. *
  10. * @Title ImageToPDF.java
  11. * @Package com.pde.pdp.ffc.util
  12. * @Description 文件转换工具  ImageToPDF
  13. * @date 2012-4-10下午02:57:54
  14. * @author guods
  15. * @version V1.0
  16. */
  17. public class ImageToPdf extends FileConvertUtilAbstract {
  18.         public boolean convert(String imagePath,String filePath) {
  19.                 boolean result = false;
  20.                 Document document = null;
  21.                 Image image = null;
  22.                 try {
  23.                         image = Image.getInstance(imagePath);
  24.                         Rectangle ret = new Rectangle(image.getWidth() + 60,
  25.                                         image.getHeight() + 60);
  26.                         document = new Document(ret, 30, 30, 30, 30);
  27.                         PdfWriter.getInstance(document, new FileOutputStream(filePath));
  28.                         document.setPageSize(ret);
  29.                         document.open();
  30.                         document.add(image);
  31.                         result = true;
  32.                 } catch (DocumentException de) {
  33.                         result = false;
  34.                 } catch (IOException ioe) {
  35.                         result = false;
  36.                 }finally{
  37.                         if(document!=null)
  38.                                 document.close();
  39.                 }
  40.                 return result;
  41.         }
  42. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2