Android根据pdf模板生成pdf文件
我们需要生成一些固定格式的pdf文件或者一些报表数据,那么我们可以用 iText包去做。
需要包含的jar包:iText-5.0.6.jar iTextAsian.jar ,怎样jar包导入工程,在这里就不再赘述了,可自行网上搜索。
1、在word里建立一个表单,也就是你的pdf模板格式
2、把word保存为pdf格式
3、用迅捷pdf编辑器打开保存的pdf
保存pdf为模板,取名名字“PdfTemplate”(随便取名)。
以下是代码部分:
public void FillPdfTemplate(String id) {android.icu.text.SimpleDateFormat simpleDateFormat =new android.icu.text.SimpleDateFormat("HHmmss");// HH:mm:ss//设置默认时区simpleDateFormat.setTimeZone(android.icu.util.TimeZone.getTimeZone("GMT+8:00"));//获取当前时间Date date2 = new Date(System.currentTimeMillis());String sim2 = simpleDateFormat.format(date2);String folderName_WaterImage = "WaterImage";String folderName_WaterDB = "WaterDB";String folderName_WaterPdf = "WaterPdf";File sdCardDir_PdfTemplate = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), folderName_WaterDB);File sdCardDir_WaterPdf = new File(Environment.getExternalStorageDirectory(),folderName_WaterPdf);//模板路径String templatePath = sdCardDir_PdfTemplate + "/" + "WaterTemplate.pdf";//生成的新文件路径String newPDFPath = sdCardDir_WaterPdf + "/" +mWaterInfo.SamplingDate + "_" + mWaterInfo.WellNumber + "_" + sim2 + ".pdf";/*** 使用中文字体* 如果是利用 AcroFields填充值的不需要在程序中设置字体,在模板文件中设置字体为中文字体就行了*/BaseFont bf = null;try {bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);} catch (DocumentException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}Font FontChinese = new Font(bf, 12, Font.NORMAL);PdfReader reader;FileOutputStream out;ByteArrayOutputStream bos;PdfStamper stamper;try {out = new FileOutputStream(newPDFPath);//输出流reader = new PdfReader(templatePath);//读取pdf模板bos = new ByteArrayOutputStream();stamper = new PdfStamper(reader, bos);AcroFields form = stamper.getAcroFields();String[] strDate = mWaterInfo.SamplingDate.split("-");String[] str = {mWaterInfo.WellNumber, mWaterInfo.Longitude + "," + mWaterInfo.Latitude,strDate[0], strDate[1], strDate[2], mWaterInfo.SamplingTime,mWaterInfo.SampleMethods, mWaterInfo.SampleDepth, mWaterInfo.Temperature,mWaterInfo.Weather, mWaterInfo.WaterLevel, mWaterInfo.WaterTemp1,mWaterInfo.ORP1, mWaterInfo.DO1, mWaterInfo.pH1, mWaterInfo.CT1, mWaterInfo.NTU1,mWaterInfo.Smell, mWaterInfo.Thing, mWaterInfo.Color, mWaterInfo.SamplingName,mWaterInfo.RecordingName};String[] it = new String[]{"Text1", "Text2", "Text3", "Text4", "Text5", "Text6", "Text7","Text8", "Text9", "Text10", "Text11", "Text12", "Text13", "Text14", "Text15","Text16", "Text17", "Text18", "Text19", "Text20", "Text21", "Text22",};for (int i = 0; i < 22; i++) {form.setFieldProperty(it[i], "textfont", bf, null);form.setField(it[i], str[i]);}stamper.setFormFlattening(true);//如果为false那么生成的PDF文件还能编辑,一定要设为truestamper.close();Document doc = new Document();PdfCopy copy = new PdfCopy(doc, out);doc.open();PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);copy.addPage(importPage);File sdCardDir_WaterImage = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), folderName_WaterImage);String imagePath1 = sdCardDir_WaterImage + "/" + "Image" + id + "_1";String imagePath2 = sdCardDir_WaterImage + "/" + "Image" + id + "_2";//插入现场图片Image image1 = Image.getInstance(imagePath1);doc.add(image1);Image image2 = Image.getInstance(imagePath2);doc.add(image2);doc.close();Toast.makeText(this, "导出pdf完成", Toast.LENGTH_LONG).show();} catch (IOException e) {System.out.println(1);} catch (BadPdfFormatException e) {e.printStackTrace();} catch (DocumentException e) {e.printStackTrace();}}
Android根据pdf模板生成pdf文件相关推荐
- java 制作pdf模板,Java-pdf模板制作流程-使用pdf 模板生成pdf文件
Java 使用pdf 模板生成pdf文件 --制作流程 1. 使用工具 adobe acrobat dc.word 2015 2. 使用 word 繪制一個 3*5 的表格並保存, ...
- itextpdf通过pdf模板生成pdf文件
itextpdf通过pdf模板生成pdf文件,设置粗体字体 1.创建pdf模板 2.使用模板生成pdf 3.itext自带的字体列表 4.遇到的坑 1.创建pdf模板 可以使用PDFFescape网站 ...
- Java使用Adobe Acrobat DC根据PDF模板生成PDF文件
制作模板 首先需要安装Adobe Acrobat DC来制作模板 打开dc工具-->准备表单,然后打开你需要制作的pdf源文件 将文本域拖到你需要代码替换的位置 你可以双击文本域修改当前文本域的 ...
- java pdf工具类_Java PDF工具类(一)| 使用 itextpdf 根据PDF模板生成PDF(文字和图片)...
Java PDF工具类(一)| 使用 itextpdf 根据设置好的PDF模板填充PDF(文字和图片) 相关文章: Java PDF工具类(二)| 使用 wkhtmltox 实现 HTML转PDF(文 ...
- Java中操作pdf模板生成pdf
[java] iText使用PDF模板生成输出PDF 项目需求涉及到操作pdf模板,根据生成好的模板向里面填充数据 用到的jar包是iText-5.0.6.jar 和iTextAsian.jar pd ...
- android手机生成pdf格式文件,Android根据pdf模板生成pdf文件
1 public voidFillPdfTemplate(String id) {2 android.icu.text.SimpleDateFormat simpleDateFormat = 3 ne ...
- android程序更改pdf文件格式,Android根据pdf模板生成pdf文件
1 public voidFillPdfTemplate(String id) {2 android.icu.text.SimpleDateFormat simpleDateFormat = 3 ne ...
- (三)、IText通过pdf模板生成pdf文件(可用做回执单等功能)
一.首先制作一个pdf模板 先用word做出个模板界面, 文件另存为pdf格式文件 通过Adobe Acrobat DC软件打开刚刚用word转换成的pdf文件步骤:文件->创建->创建表 ...
- java 根据PDF模板生成PDF文件并导出
1.准备操作的maven包 <dependency><groupId>com.itextpdf</groupId><artifactId>itextpd ...
最新文章
- 在ubuntu 16.04.5 LTS上使用Python 2.7生成带logo的二维码实战
- 自建通用Makefile 分享
- Python 中文注释报错解决方法
- 详解/etc/profile、/etc/bash.bahsrc、~/.profile、~/.bashrc的用途
- 堡垒之夜一年内增1亿玩家、YouTube为支持AV1的8K电视提供8K内容、Elon Musk要过极简生活等...
- oracle 11g segment,11g视图dba_segments中增加了一个有用的segment_subtype字段!
- protobuf 向前兼容向后兼容
- SPH(光滑粒子流体动力学)流体模拟实现二:SPH算法(4)-算法实现1
- android dp sp px_规范|iOS与Android设计规范解析(一)
- 找不到可安装的isam怎么解决_安装系统找不到硬盘怎么办
- HDU2502 月之数(解法二)【废除!!!】
- UnityShader8:编写Shader时需要注意的细节和可能需要的意外/BUG
- 深度学习:径向基网络(RBF)
- 中文字符频率统计python_使用 Python 统计中文字符的数量
- 【图像隐写】基于matlab DWT数字水印多种攻击效果对比【含Matlab源码 1134期】
- 网上摘的数据缓存资料
- 关于字长、内存空间、地址总线、数据总线的理解
- android进入recovery模式,Android关机重启至recovery安卓进入Recovery模式模式
- sphinx安装及简单使用
- 教你文件批量重命名技巧,彻底告别手工改名的繁琐