A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 朱婵 中级黑马   /  2014-2-20 13:01  /  600 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文



  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. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马