将图片和文字写到pdf文件中
为什么80%的码农都做不了架构师?>>>
这里主要用到了iText包,jar包在附件里面
由于iText目前不支持bmp格式的图片,所以在往pdf里面插入的时候要进行转换。
转换代码
package com.taiji.lbs.register.util; import java.awt.Image; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.awt.image.MemoryImageSource; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class BmpToJpg { /** * 图片格式转换 BMP -> JPG * @param file * @param dstFile */ public static void bmpTojpg(String file, String dstFile) { try { FileInputStream in = new FileInputStream(file); Image TheImage = read(in); int wideth = TheImage.getWidth(null); int height = TheImage.getHeight(null); BufferedImage tag = new BufferedImage(wideth, height,BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(TheImage, 0, 0, wideth, height, null); FileOutputStream out = new FileOutputStream(dstFile); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); } catch (Exception e) { System.out.println(e); } } public static int constructInt(byte[] in, int offset) { int ret = ((int) in[offset + 3] & 0xff); ret = (ret << 8) | ((int) in[offset + 2] & 0xff); ret = (ret << 8) | ((int) in[offset + 1] & 0xff); ret = (ret << 8) | ((int) in[offset + 0] & 0xff); return (ret); } public static int constructInt3(byte[] in, int offset) { int ret = 0xff; ret = (ret << 8) | ((int) in[offset + 2] & 0xff); ret = (ret << 8) | ((int) in[offset + 1] & 0xff); ret = (ret << 8) | ((int) in[offset + 0] & 0xff); return (ret); } public static long constructLong(byte[] in, int offset) { long ret = ((long) in[offset + 7] & 0xff); ret |= (ret << 8) | ((long) in[offset + 6] & 0xff); ret |= (ret << 8) | ((long) in[offset + 5] & 0xff); ret |= (ret << 8) | ((long) in[offset + 4] & 0xff); ret |= (ret << 8) | ((long) in[offset + 3] & 0xff); ret |= (ret << 8) | ((long) in[offset + 2] & 0xff); ret |= (ret << 8) | ((long) in[offset + 1] & 0xff); ret |= (ret << 8) | ((long) in[offset + 0] & 0xff); return (ret); } public static double constructDouble(byte[] in, int offset) { long ret = constructLong(in, offset); return (Double.longBitsToDouble(ret)); } public static short constructShort(byte[] in, int offset) { short ret = (short) ((short) in[offset + 1] & 0xff); ret = (short) ((ret << 8) | (short) ((short) in[offset + 0] & 0xff)); return (ret); } static class BitmapHeader { public int iSize, ibiSize, iWidth, iHeight, iPlanes, iBitcount, iCompression, iSizeimage, iXpm, iYpm, iClrused, iClrimp; // 读取bmp文件头信息 public void read(FileInputStream fs) throws IOException { final int bflen = 14; byte bf[] = new byte[bflen]; fs.read(bf, 0, bflen); final int bilen = 40; byte bi[] = new byte[bilen]; fs.read(bi, 0, bilen); iSize = constructInt(bf, 2); ibiSize = constructInt(bi, 2); iWidth = constructInt(bi, 4); iHeight = constructInt(bi, 8); iPlanes = constructShort(bi, 12); iBitcount = constructShort(bi, 14); iCompression = constructInt(bi, 16); iSizeimage = constructInt(bi, 20); iXpm = constructInt(bi, 24); iYpm = constructInt(bi, 28); iClrused = constructInt(bi, 32); iClrimp = constructInt(bi, 36); } } public static Image read(FileInputStream fs) { try { BitmapHeader bh = new BitmapHeader(); bh.read(fs); if (bh.iBitcount == 24) { return (readImage24(fs, bh)); } if (bh.iBitcount == 32) { return (readImage32(fs, bh)); } fs.close(); } catch (IOException e) { System.out.println(e); } return (null); } // 24位 protected static Image readImage24(FileInputStream fs, BitmapHeader bh) throws IOException { Image image; if (bh.iSizeimage == 0) { bh.iSizeimage = ((((bh.iWidth * bh.iBitcount) + 31) & ~31) >> 3); bh.iSizeimage *= bh.iHeight; } int npad = (bh.iSizeimage / bh.iHeight) - bh.iWidth * 3; int ndata[] = new int[bh.iHeight * bh.iWidth]; byte brgb[] = new byte[(bh.iWidth + npad) * 3 * bh.iHeight]; fs.read(brgb, 0, (bh.iWidth + npad) * 3 * bh.iHeight); int nindex = 0; for (int j = 0; j < bh.iHeight; j++) { for (int i = 0; i < bh.iWidth; i++) { ndata[bh.iWidth * (bh.iHeight - j - 1) + i] = constructInt3( brgb, nindex); nindex += 3; } nindex += npad; } image = Toolkit.getDefaultToolkit().createImage( new MemoryImageSource(bh.iWidth, bh.iHeight, ndata, 0, bh.iWidth)); fs.close(); return (image); } // 32位 protected static Image readImage32(FileInputStream fs, BitmapHeader bh) throws IOException { Image image; int ndata[] = new int[bh.iHeight * bh.iWidth]; byte brgb[] = new byte[bh.iWidth * 4 * bh.iHeight]; fs.read(brgb, 0, bh.iWidth * 4 * bh.iHeight); int nindex = 0; for (int j = 0; j < bh.iHeight; j++) { for (int i = 0; i < bh.iWidth; i++) { ndata[bh.iWidth * (bh.iHeight - j - 1) + i] = constructInt3( brgb, nindex); nindex += 4; } } image = Toolkit.getDefaultToolkit().createImage( new MemoryImageSource(bh.iWidth, bh.iHeight, ndata, 0, bh.iWidth)); fs.close(); return (image); } public static void main(String[] args) { String srcfile = "c:\\726.bmp"; String dstFile = "c:\\726.jpg"; bmpTojpg(srcfile, dstFile); } }
来看看插入代码
package com.taiji.lbs.register.util; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.List; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Image; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfWriter; import com.taiji.core.util.ApplicationPath; import com.taiji.core.util.PaginationSupport; import com.taiji.lbs.register.hibernate.model.Picture; import com.taiji.lbs.register.hibernate.model.RegisterInfo; public class CreatePDF { /** * 创建pdf将用户信息放入其中 * 乔磊 * @param list * @throws DocumentException * @throws Exception */ public static void createPDF(PaginationSupport list) throws Exception, DocumentException{ //导出成pdf List pdfList = list.getItems(); String picName = ""; String picNameDst = "";//将bmp图片转换成jpg格式 String str = ""; String rootPath = ApplicationPath.getRootPath().replaceAll("\\\\","\\\\\\\\");// 获得绝对地址 服务器的 //建立一个文档对象 Document doc = new Document(); PdfWriter.getInstance(doc, new FileOutputStream("c:/hello.pdf")); // 打开文档对象 doc.open(); //根据经验,建议使用windos自带字体 BaseFont basefont; com.lowagie.text.Font FontChinese ; basefont = BaseFont.createFont("c:\\windows\\fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); FontChinese = new com.lowagie.text.Font(basefont); for (Object object : pdfList) { Picture pic = ((RegisterInfo)object).getPicture(); if (pic != null) { picName = String.valueOf(pic.getId() + ".bmp"); picNameDst = String.valueOf(pic.getId() + ".jpg"); File filePic = new File(rootPath + "\\photo\\" + picName); FileOutputStream output; output = new FileOutputStream(filePic); byte buffer[] = null; if (pic.getPhoto() != null) { buffer = pic.getPhoto(); InputStream in = new ByteArrayInputStream(buffer); int len; while ((len = in.read(buffer)) > 0) { output.write(buffer, 0, len); } output.close(); in.close(); } else { picName = null; } } BmpToJpg.bmpTojpg(rootPath + "\\photo\\" + picName, rootPath + "\\photo\\" + picNameDst); //加用户头像 Image jpg = Image.getInstance(rootPath + "\\photo\\" + picNameDst); jpg.setAlignment(Image.ALIGN_LEFT); doc.add(jpg); //加用户信息 str = ((RegisterInfo)object).getId()+":"+((RegisterInfo)object).getIdNum(); Paragraph tt = new Paragraph(str, FontChinese); tt.setAlignment(Paragraph.ALIGN_CENTER); doc.add(tt); } //释放文档对象 doc.close(); } }
这样就完成了图片格式转换和插入了。
===============================================================
先前的那个例子图片和文本容易分离,为了更美观一些将图片和文本放到一个表格里面,修改部分代码如下
BmpToJpg.bmpTojpg(rootPath + "\\photo\\" + picName, rootPath + "\\photo\\" + picNameDst); //加用户头像 Image jpg = Image.getInstance(rootPath + "\\photo\\" + picNameDst); jpg.setAlignment(Image.ALIGN_LEFT); //doc.add(jpg); //加用户信息 str = ((RegisterInfo)object).getChineseName()+" "+((RegisterInfo)object).getEnglishName()+" "+((RegisterInfo)object).getNationality()+" "+((RegisterInfo)object).getIdNum()+" "+((RegisterInfo)object).getDiplomaticTitle(); Paragraph tt = new Paragraph(str, FontChinese); tt.setAlignment(Paragraph.ALIGN_RIGHT); //doc.add(tt); //创建一个有1列的表格 PdfPTable table = new PdfPTable(1); //定义一个表格单元 PdfPCell cell = new PdfPCell(jpg); //把单元加到表格中 table.addCell(cell); //重新定义单元格 PdfPCell cellText = new PdfPCell(tt); //增加到表格上 table.addCell(cellText); //增加到文档中 doc.add(table);
本文出自 “乔磊的博客 学习 进步” 博客,请务必保留此出处http://sucre.blog.51cto.com/1084905/554921
转载于:https://my.oschina.net/sucre/blog/296131
将图片和文字写到pdf文件中相关推荐
- python 删除pdf页面_使用python从新生成的pdf文件中删除空白页
使用下面的代码,我试图将图片从目录粘贴到PDF文件中.代码已经在工作,生成我的PDF几乎如我所愿.在 唯一的问题是它总是在图片之间添加空白页,我不知道为什么. 如果我执行代码,PDF将以空白页开始,然 ...
- 向pdf文件中插入图片及文字 java实现
向pdf文件中插入图片及文字 引入itextpdf相关依赖 <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --> ...
- 使用itext7在PDF文件中的指定文字位置添加电子签名图片技术记录
使用itext7在PDF文件中的指定文字位置添加电子签名图片 文章目录 使用itext7在PDF文件中的指定文字位置添加电子签名图片 一.技术使用背景 二.使用步骤 1.引入依赖 2.具体代码 2.控 ...
- 如何快速在PDF文件中插入图片
在 PDF文件中插入图片我优先想到了 Adobe Acrobat DC,胜任此项工作完全 OK.但是有个问题,Acrobat 会自动识别 PDF 中的文字.如果有手写字迹经过 Acrobat 识别再保 ...
- pdf exe如何提取pdf文件_python应用:如何用python提取pdf文件中的文字
从pdf中提取文字,相信很多人都干过这事,怎么在python中实现呢,今天带大家看看. 第一步导入库 import PyPDF2 第二步导入pdf文件 pdf_file =open('dataset/ ...
- 如何将图片转换、合并为PDF文件?
文章来源:https://www.reneelab.com.cn/combine-images-into-pdf.html 目录 一.简述:关于PDF格式和常见的图像格式 二.在Windows上将图片 ...
- PDF文件中的图片如何删除?分享两种删除方法
我们怎么把PDF文件中的图片给删除掉呢?大家在日常使用PDF文件的过程中,难免会对文件有编辑需求,有时候需要编辑文字,有时候需要对文件中的图片进行删除处理.遇到这种只需要删除PDF文件里的图片的时候, ...
- 易语言 图片插入超级列表框_新手教程!如何在PDF文件中插入图像
每当在工作中收到一份PDF文件时,只要没有图片,阅读起来就会非常的枯燥无味,长时间的阅读还会带来睡意,而图片的使用能够更好的提高文件的阅读性.所以图片与文字的相配才是完整的一份PDF文件.但是PDF文 ...
- PDF如何编辑,怎么编辑PDF文件中的文字
越来越多的小伙伴会私信小编询问小编关于PDF文件的修改技巧,在使用PDF文件的时候,往往是需要用到PDF编辑器的,编辑文件时,想要修改文件的内容,应该怎么去编辑呢,其实,还是很简单的,不会的小伙伴可以 ...
最新文章
- 【经典课程】李宏毅机器学习2020版正式上线!!!
- CSS3学习笔记1:结构性伪类选择器
- 北美公司面试经验笔记
- 1.10 instanceof关键字
- 201205阶段二FFmpeg编码
- leetcode 239. 滑动窗口最大值(单调队列)
- python画图显示不了中文_完美解决Python matplotlib绘图时汉字显示不正常的问题
- 隐藏文件真实下载地址(支持超大文件)源码
- radiobuttonlist 后面追加操作按钮_【进口知识】通关无纸化签约及代理报关委托收发操作指南...
- android 音乐播放器----获取专辑封面图片
- 无悔入华夏怎么一直显示服务器,无悔入华夏祭祀玩法怎么玩 无悔入华夏祭祀怎么触发?...
- 计算机网络实验5以太网链路帧实验,计算机网络实验-使用Wireshark分析以太网帧与ARP协议.docx...
- samba服务器_win10 更新导致无法连接samba服务器
- cocos2dx中的动作
- lodop简单入门教程
- Android自动打包、签名、优化、上传ANT脚本
- 华为eNSP静态路由原理与配置实例详解
- 用参数方程绘制椭球体
- 小程序逆向——某书小程序反编译(一)
- ios 获取是否静音模式_iOS 判断设备是否静音