将图片和文字写到pdf文件中

简介:

这里主要用到了iText包,jar包在附件里面

由于iText目前不支持bmp格式的图片,所以在往pdf里面插入的时候要进行转换。

转换代码

 

 
  1. package com.taiji.lbs.register.util;  
  2.  
  3. import java.awt.Image;  
  4. import java.awt.Toolkit;  
  5. import java.awt.image.BufferedImage;  
  6. import java.awt.image.MemoryImageSource;  
  7. import java.io.FileInputStream;  
  8. import java.io.FileOutputStream;  
  9. import java.io.IOException;  
  10. import com.sun.image.codec.jpeg.JPEGCodec;  
  11. import com.sun.image.codec.jpeg.JPEGImageEncoder;  
  12.  
  13. public class BmpToJpg {  
  14.  
  15.     /**  
  16.      * 图片格式转换 BMP -> JPG  
  17.      * @param file  
  18.      * @param dstFile  
  19.      */ 
  20.     public static void bmpTojpg(String file, String dstFile) {  
  21.         try {  
  22.             FileInputStream in = new FileInputStream(file);  
  23.             Image TheImage = read(in);  
  24.             int wideth = TheImage.getWidth(null);  
  25.             int height = TheImage.getHeight(null);  
  26.             BufferedImage tag = new BufferedImage(wideth, height,BufferedImage.TYPE_INT_RGB);  
  27.             tag.getGraphics().drawImage(TheImage, 00, wideth, height, null);  
  28.             FileOutputStream out = new FileOutputStream(dstFile);  
  29.             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
  30.             encoder.encode(tag);  
  31.             out.close();  
  32.         } catch (Exception e) {  
  33.             System.out.println(e);  
  34.         }  
  35.     }  
  36.  
  37.     public static int constructInt(byte[] in, int offset) {  
  38.         int ret = ((int) in[offset + 3] & 0xff);  
  39.         ret = (ret << 8) | ((int) in[offset + 2] & 0xff);  
  40.         ret = (ret << 8) | ((int) in[offset + 1] & 0xff);  
  41.         ret = (ret << 8) | ((int) in[offset + 0] & 0xff);  
  42.         return (ret);  
  43.     }  
  44.  
  45.     public static int constructInt3(byte[] in, int offset) {  
  46.         int ret = 0xff;  
  47.         ret = (ret << 8) | ((int) in[offset + 2] & 0xff);  
  48.         ret = (ret << 8) | ((int) in[offset + 1] & 0xff);  
  49.         ret = (ret << 8) | ((int) in[offset + 0] & 0xff);  
  50.         return (ret);  
  51.     }  
  52.  
  53.     public static long constructLong(byte[] in, int offset) {  
  54.         long ret = ((long) in[offset + 7] & 0xff);  
  55.         ret |= (ret << 8) | ((long) in[offset + 6] & 0xff);  
  56.         ret |= (ret << 8) | ((long) in[offset + 5] & 0xff);  
  57.         ret |= (ret << 8) | ((long) in[offset + 4] & 0xff);  
  58.         ret |= (ret << 8) | ((long) in[offset + 3] & 0xff);  
  59.         ret |= (ret << 8) | ((long) in[offset + 2] & 0xff);  
  60.         ret |= (ret << 8) | ((long) in[offset + 1] & 0xff);  
  61.         ret |= (ret << 8) | ((long) in[offset + 0] & 0xff);  
  62.         return (ret);  
  63.     }  
  64.  
  65.     public static double constructDouble(byte[] in, int offset) {  
  66.         long ret = constructLong(in, offset);  
  67.         return (Double.longBitsToDouble(ret));  
  68.     }  
  69.  
  70.     public static short constructShort(byte[] in, int offset) {  
  71.         short ret = (short) ((short) in[offset + 1] & 0xff);  
  72.         ret = (short) ((ret << 8) | (short) ((short) in[offset + 0] & 0xff));  
  73.         return (ret);  
  74.     }  
  75.  
  76.     static class BitmapHeader {  
  77.         public int iSize, ibiSize, iWidth, iHeight, iPlanes, iBitcount,  
  78.                 iCompression, iSizeimage, iXpm, iYpm, iClrused, iClrimp;  
  79.  
  80.         // 读取bmp文件头信息  
  81.         public void read(FileInputStream fs) throws IOException {  
  82.             final int bflen = 14;  
  83.             byte bf[] = new byte[bflen];  
  84.             fs.read(bf, 0, bflen);  
  85.             final int bilen = 40;  
  86.             byte bi[] = new byte[bilen];  
  87.             fs.read(bi, 0, bilen);  
  88.             iSize = constructInt(bf, 2);  
  89.             ibiSize = constructInt(bi, 2);  
  90.             iWidth = constructInt(bi, 4);  
  91.             iHeight = constructInt(bi, 8);  
  92.             iPlanes = constructShort(bi, 12);  
  93.             iBitcount = constructShort(bi, 14);  
  94.             iCompression = constructInt(bi, 16);  
  95.             iSizeimage = constructInt(bi, 20);  
  96.             iXpm = constructInt(bi, 24);  
  97.             iYpm = constructInt(bi, 28);  
  98.             iClrused = constructInt(bi, 32);  
  99.             iClrimp = constructInt(bi, 36);  
  100.         }  
  101.     }  
  102.  
  103.     public static Image read(FileInputStream fs) {  
  104.         try {  
  105.             BitmapHeader bh = new BitmapHeader();  
  106.             bh.read(fs);  
  107.             if (bh.iBitcount == 24) {  
  108.                 return (readImage24(fs, bh));  
  109.             }  
  110.             if (bh.iBitcount == 32) {  
  111.                 return (readImage32(fs, bh));  
  112.             }  
  113.             fs.close();  
  114.         } catch (IOException e) {  
  115.             System.out.println(e);  
  116.         }  
  117.         return (null);  
  118.     }  
  119.  
  120.     // 24位  
  121.     protected static Image readImage24(FileInputStream fs, BitmapHeader bh)  
  122.             throws IOException {  
  123.         Image image;  
  124.         if (bh.iSizeimage == 0) {  
  125.             bh.iSizeimage = ((((bh.iWidth * bh.iBitcount) + 31) & ~31) >> 3);  
  126.             bh.iSizeimage *= bh.iHeight;  
  127.         }  
  128.         int npad = (bh.iSizeimage / bh.iHeight) - bh.iWidth * 3;  
  129.         int ndata[] = new int[bh.iHeight * bh.iWidth];  
  130.         byte brgb[] = new byte[(bh.iWidth + npad) * 3 * bh.iHeight];  
  131.         fs.read(brgb, 0, (bh.iWidth + npad) * 3 * bh.iHeight);  
  132.         int nindex = 0;  
  133.         for (int j = 0; j < bh.iHeight; j++) {  
  134.             for (int i = 0; i < bh.iWidth; i++) {  
  135.                 ndata[bh.iWidth * (bh.iHeight - j - 1) + i] = constructInt3(  
  136.                         brgb, nindex);  
  137.                 nindex += 3;  
  138.             }  
  139.             nindex += npad;  
  140.         }  
  141.         image = Toolkit.getDefaultToolkit().createImage(  
  142.                 new MemoryImageSource(bh.iWidth, bh.iHeight, ndata, 0,  
  143.                         bh.iWidth));  
  144.         fs.close();  
  145.         return (image);  
  146.     }  
  147.  
  148.     // 32位  
  149.     protected static Image readImage32(FileInputStream fs, BitmapHeader bh)  
  150.             throws IOException {  
  151.         Image image;  
  152.         int ndata[] = new int[bh.iHeight * bh.iWidth];  
  153.         byte brgb[] = new byte[bh.iWidth * 4 * bh.iHeight];  
  154.         fs.read(brgb, 0, bh.iWidth * 4 * bh.iHeight);  
  155.         int nindex = 0;  
  156.         for (int j = 0; j < bh.iHeight; j++) {  
  157.             for (int i = 0; i < bh.iWidth; i++) {  
  158.                 ndata[bh.iWidth * (bh.iHeight - j - 1) + i] = constructInt3(  
  159.                         brgb, nindex);  
  160.                 nindex += 4;  
  161.             }  
  162.         }  
  163.         image = Toolkit.getDefaultToolkit().createImage(  
  164.                 new MemoryImageSource(bh.iWidth, bh.iHeight, ndata, 0,  
  165.                         bh.iWidth));  
  166.         fs.close();  
  167.         return (image);  
  168.     }  
  169.  
  170.     public static void main(String[] args) {  
  171.         String srcfile = "c:\\726.bmp";  
  172.         String dstFile = "c:\\726.jpg";  
  173.         bmpTojpg(srcfile, dstFile);  
  174.     }  
  175.  
  176. }  

来看看插入代码

 

 
  1. package com.taiji.lbs.register.util;  
  2.  
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.File;  
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.util.List;  
  8.  
  9. import com.lowagie.text.Document;  
  10. import com.lowagie.text.DocumentException;  
  11. import com.lowagie.text.Image;  
  12. import com.lowagie.text.Paragraph;  
  13. import com.lowagie.text.pdf.BaseFont;  
  14. import com.lowagie.text.pdf.PdfWriter;  
  15. import com.taiji.core.util.ApplicationPath;  
  16. import com.taiji.core.util.PaginationSupport;  
  17. import com.taiji.lbs.register.hibernate.model.Picture;  
  18. import com.taiji.lbs.register.hibernate.model.RegisterInfo;  
  19.  
  20. public class CreatePDF {  
  21.     /**  
  22.      * 创建pdf将用户信息放入其中  
  23.      * 乔磊  
  24.      * @param list  
  25.      * @throws DocumentException   
  26.      * @throws Exception   
  27.      */ 
  28.     public static void createPDF(PaginationSupport list) throws Exception, DocumentException{  
  29.         //导出成pdf  
  30.         List pdfList = list.getItems();  
  31.         String picName = "";  
  32.         String picNameDst = "";//将bmp图片转换成jpg格式  
  33.         String str = "";  
  34.         String rootPath = ApplicationPath.getRootPath().replaceAll("\\\\","\\\\\\\\");// 获得绝对地址 服务器的  
  35.         //建立一个文档对象  
  36.          Document doc = new Document();  
  37.          PdfWriter.getInstance(doc, new FileOutputStream("c:/hello.pdf"));  
  38.         // 打开文档对象  
  39.         doc.open();  
  40.         //根据经验,建议使用windos自带字体  
  41.         BaseFont basefont;  
  42.         com.lowagie.text.Font   FontChinese ;  
  43.         basefont = BaseFont.createFont("c:\\windows\\fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);  
  44.         FontChinese   =   new   com.lowagie.text.Font(basefont);     
  45.         for (Object object : pdfList) {  
  46.             Picture pic = ((RegisterInfo)object).getPicture();  
  47.             if (pic != null) {  
  48.                 picName = String.valueOf(pic.getId() + ".bmp");  
  49.                 picNameDst = String.valueOf(pic.getId() + ".jpg");  
  50.                 File filePic = new File(rootPath + "\\photo\\" + picName);  
  51.                 FileOutputStream output;  
  52.                 output = new FileOutputStream(filePic);  
  53.                 byte buffer[] = null;  
  54.                 if (pic.getPhoto() != null) {  
  55.                     buffer = pic.getPhoto();  
  56.                     InputStream in = new ByteArrayInputStream(buffer);  
  57.                     int len;  
  58.                     while ((len = in.read(buffer)) > 0) {  
  59.                         output.write(buffer, 0, len);  
  60.                     }  
  61.                     output.close();  
  62.                     in.close();  
  63.                 } else {  
  64.                     picName = null;  
  65.                 }  
  66.             }  
  67.             BmpToJpg.bmpTojpg(rootPath + "\\photo\\" + picName, rootPath + "\\photo\\" + picNameDst);  
  68.             //加用户头像  
  69.             Image jpg = Image.getInstance(rootPath + "\\photo\\" + picNameDst);  
  70.             jpg.setAlignment(Image.ALIGN_LEFT);  
  71.             doc.add(jpg);  
  72.             //加用户信息  
  73.             str = ((RegisterInfo)object).getId()+":"+((RegisterInfo)object).getIdNum();  
  74.             Paragraph tt = new Paragraph(str, FontChinese);  
  75.             tt.setAlignment(Paragraph.ALIGN_CENTER);  
  76.             doc.add(tt);  
  77.         }  
  78.         //释放文档对象     
  79.         doc.close();  
  80.     }  
  81. }  

这样就完成了图片格式转换和插入了。

===============================================================

先前的那个例子图片和文本容易分离,为了更美观一些将图片和文本放到一个表格里面,修改部分代码如下

 
  1. BmpToJpg.bmpTojpg(rootPath + "\\photo\\" + picName, rootPath + "\\photo\\" + picNameDst);  
  2.             //加用户头像  
  3.             Image jpg = Image.getInstance(rootPath + "\\photo\\" + picNameDst);  
  4.             jpg.setAlignment(Image.ALIGN_LEFT);  
  5.             //doc.add(jpg);  
  6.             //加用户信息  
  7.             str = ((RegisterInfo)object).getChineseName()+" "+((RegisterInfo)object).getEnglishName()+" "+((RegisterInfo)object).getNationality()+" "+((RegisterInfo)object).getIdNum()+" "+((RegisterInfo)object).getDiplomaticTitle();  
  8.             Paragraph tt = new Paragraph(str, FontChinese);  
  9.             tt.setAlignment(Paragraph.ALIGN_RIGHT);  
  10.             //doc.add(tt);  
  11.               
  12.               
  13.             //创建一个有1列的表格  
  14.             PdfPTable table = new PdfPTable(1);  
  15.             //定义一个表格单元  
  16.             PdfPCell cell = new PdfPCell(jpg);  
  17.             //把单元加到表格中  
  18.             table.addCell(cell);  
  19.             //重新定义单元格  
  20.             PdfPCell cellText = new PdfPCell(tt);  
  21.             //增加到表格上  
  22.             table.addCell(cellText);  
  23.             //增加到文档中  
  24.             doc.add(table); 

 本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/554921,如需转载请自行联系原作者

相关文章
|
1月前
|
数据挖掘 数据安全/隐私保护 开发者
使用Spire.PDF for Python插件从PDF文件提取文字和图片信息
使用Spire.PDF for Python插件从PDF文件提取文字和图片信息
62 0
|
1月前
|
存储 缓存 Python
如何使用Python抓取PDF文件并自动下载到本地
如何使用Python抓取PDF文件并自动下载到本地
31 0
|
1月前
|
JSON JavaScript 前端开发
vue项目使用Print.js插件实现PDF文件打印
vue项目使用Print.js插件实现PDF文件打印
33 0
|
1月前
|
机器学习/深度学习 文字识别 数据安全/隐私保护
Python实现从PDF和图片提取文字的方法总结
Python实现从PDF和图片提取文字的方法总结
43 0
|
1月前
|
Shell Python
Python生成PDF文件
Python生成PDF文件
22 0
|
3月前
|
Java API Apache
使用 Apache PDFBox 操作PDF文件
Apache PDFBox库是一个开源的Java工具,专门用于处理PDF文档。它允许用户创建全新的PDF文件,编辑现有的PDF文档,以及从PDF文件中提取内容。此外,Apache PDFBox还提供了一些命令行实用工具。
100 6
|
3月前
|
存储
Vue3 实现 PDF 文件在线预览功能
Vue3 实现 PDF 文件在线预览功能
292 0
|
4月前
|
JavaScript 前端开发
nodejs实现解析chm文件列表,无需转换为PDF文件格式,在线预览chm文件以及目录,不依赖任何网页端插件
nodejs实现解析chm文件列表,无需转换为PDF文件格式,在线预览chm文件以及目录,不依赖任何网页端插件
|
2月前
|
编解码 数据可视化 数据挖掘
【办公自动化】用Python将PDF文件转存为图片
【办公自动化】用Python将PDF文件转存为图片
62 1
|
1月前
|
前端开发
前端实现生成pdf文件并下载
前端实现生成pdf文件并下载
35 1