将图片和文字写到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,如需转载请自行联系原作者

相关文章
|
4月前
|
机器学习/深度学习 文字识别 Java
Python实现PDF图片OCR识别:从原理到实战的全流程解析
本文详解2025年Python实现扫描PDF文本提取的四大OCR方案(Tesseract、EasyOCR、PaddleOCR、OCRmyPDF),涵盖环境配置、图像预处理、核心识别与性能优化,结合财务票据、古籍数字化等实战场景,助力高效构建自动化文档处理系统。
1222 0
|
3月前
|
机器学习/深度学习 文字识别 Shell
高效率办公PDF批量处理:批量OCR识别PDF区域文字内容,用PDF内容批量改名或导出表格的货物运单应用案例
针对铁路货运物流单存档需求,本项目基于WPF与飞桨OCR技术,实现批量图片多区域文字识别与自动重命名。用户可自定义识别区域,系统提取关键信息(如车号、批次号)并生成规范文件名,提升档案管理效率与检索准确性,支持PDF及图像文件处理。
484 0
|
6月前
|
C#
【PDF提取内容改名】批量提取PDF指定区域内容重命名PDF文件,PDF自动提取内容命名的方案和详细步骤
本工具可批量提取PDF中的合同编号、日期、发票号等关键信息,支持PDF自定义区域提取并自动重命名文件,适用于合同管理、发票处理、文档归档和数据录入场景。基于iTextSharp库实现,提供完整代码示例与百度、腾讯网盘下载链接,助力高效处理PDF文档。
835 40
|
6月前
|
编译器 Python
如何利用Python批量重命名PDF文件
本文介绍了如何使用Python提取PDF内容并用于文件重命名。通过安装Python环境、PyCharm编译器及Jupyter Notebook,结合tabula库实现PDF数据读取与处理,并提供代码示例与参考文献。
|
8月前
|
人工智能 搜索推荐 算法
PDF 转 JPG 图片小工具:CodeBuddy 助力解决转换痛点
在 PDF 转 JPG 的实际应用中,用户普遍面临转换质量差、批量处理效率低、格式兼容性不足以及编程实现困难等痛点。而 CodeBuddy 凭借智能代码生成与优化、实时错误诊断修复、助力代码学习拓展,以及支持多场景适配与个性化定制等强大的 AI 编程能力,精准直击这些难题。使用 CodeBuddy 开发 Python PDF 转 JPG 小工具,能够有效提升转换效率与质量,降低开发门槛和成本,为用户带来高效、优质的文件格式转换体验。
304 16
|
8月前
|
人工智能 算法 安全
使用CodeBuddy实现批量转换PPT、Excel、Word为PDF文件工具
通过 CodeBuddy 实现本地批量转换工具,让复杂的文档处理需求转化为 “需求描述→代码生成→一键运行” 的极简流程,真正实现 “技术为效率服务” 的目标。感兴趣的快来体验下把
453 10
|
7月前
|
人工智能 开发工具 开发者
【HarmonyOS 5】鸿蒙应用实现发票扫描、文档扫描输出PDF图片或者表格的功能
HarmonyOS 系统提供的核心场景化视觉服务,旨在帮助开发者快速实现移动端文档数字化功能。
362 0
|
7月前
|
数据采集 存储 API
Python爬虫结合API接口批量获取PDF文件
Python爬虫结合API接口批量获取PDF文件
|
9月前
|
存储 JSON API
如何将 Swagger 文档导出为 PDF 文件
你会发现自己可能需要将 Swagger 文档导出为 PDF 或文件,以便于共享和存档。在这篇博文中,我们将指导你完成将 Swagger 文档导出为 PDF 格式的过程。
|
11月前
|
人工智能 编解码 文字识别
OCRmyPDF:16.5K Star!快速将 PDF 文件转换为可搜索、可复制的文档的命令行工具
OCRmyPDF 是一款开源命令行工具,专为将扫描的 PDF 文件转换为可搜索、可复制的文档。支持多语言、图像优化和多核处理。
1225 17
OCRmyPDF:16.5K Star!快速将 PDF 文件转换为可搜索、可复制的文档的命令行工具

热门文章

最新文章