首页 技术 正文
技术 2022年11月21日
0 收藏 798 点赞 3,421 浏览 3491 个字

我们需要生成一些固定格式的pdf文件或者一些报表数据,那么我们可以用 iText包去做。

需要包含的jar包:iText-5.0.6.jar    iTextAsian.jar ,怎样jar包导入工程,在这里就不再赘述了,可自行网上搜索。

1、再word里建立一个表单,也就是你的pdf模板格式

2、把word保存为pdf格式

3、用迅捷pdf编辑器打开保存的pdf

保存pdf为模板,取名名字“PdfTemplate”(随便取名)。

以下是代码部分:

  1 public void FillPdfTemplate(String id) {
2 android.icu.text.SimpleDateFormat simpleDateFormat =
3 new android.icu.text.SimpleDateFormat("HHmmss");// HH:mm:ss
4 //设置默认时区
5 simpleDateFormat.setTimeZone(android.icu.util.TimeZone.getTimeZone("GMT+8:00"));
6 //获取当前时间
7 Date date2 = new Date(System.currentTimeMillis());
8 String sim2 = simpleDateFormat.format(date2);
9
10 String folderName_WaterImage = "WaterImage";
11 String folderName_WaterDB = "WaterDB";
12 String folderName_WaterPdf = "WaterPdf";
13
14 File sdCardDir_PdfTemplate = new File(Environment.getExternalStoragePublicDirectory(
15 Environment.DIRECTORY_DOWNLOADS), folderName_WaterDB);
16 File sdCardDir_WaterPdf = new File(Environment.getExternalStorageDirectory(),
17 folderName_WaterPdf);
18
19 //模板路径
20 String templatePath = sdCardDir_PdfTemplate + "/" + "WaterTemplate.pdf";
21 //生成的新文件路径
22 String newPDFPath = sdCardDir_WaterPdf + "/" +
23 mWaterInfo.SamplingDate + "_" + mWaterInfo.WellNumber + "_" + sim2 + ".pdf";
24
25 /**
26 * 使用中文字体
27 * 如果是利用 AcroFields填充值的不需要在程序中设置字体,在模板文件中设置字体为中文字体就行了
28 */
29 BaseFont bf = null;
30 try {
31 bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
32 } catch (DocumentException e) {
33 e.printStackTrace();
34 } catch (IOException e) {
35 e.printStackTrace();
36 }
37 Font FontChinese = new Font(bf, 12, Font.NORMAL);
38
39 PdfReader reader;
40 FileOutputStream out;
41 ByteArrayOutputStream bos;
42 PdfStamper stamper;
43 try {
44 out = new FileOutputStream(newPDFPath);//输出流
45 reader = new PdfReader(templatePath);//读取pdf模板
46 bos = new ByteArrayOutputStream();
47 stamper = new PdfStamper(reader, bos);
48 AcroFields form = stamper.getAcroFields();
49
50 String[] strDate = mWaterInfo.SamplingDate.split("-");
51 String[] str = {
52 mWaterInfo.WellNumber, mWaterInfo.Longitude + "," + mWaterInfo.Latitude,
53 strDate[0], strDate[1], strDate[2], mWaterInfo.SamplingTime,
54 mWaterInfo.SampleMethods, mWaterInfo.SampleDepth, mWaterInfo.Temperature,
55 mWaterInfo.Weather, mWaterInfo.WaterLevel, mWaterInfo.WaterTemp1,
56 mWaterInfo.ORP1, mWaterInfo.DO1, mWaterInfo.pH1, mWaterInfo.CT1, mWaterInfo.NTU1,
57 mWaterInfo.Smell, mWaterInfo.Thing, mWaterInfo.Color, mWaterInfo.SamplingName,
58 mWaterInfo.RecordingName};
59
60 String[] it = new String[]{
61 "Text1", "Text2", "Text3", "Text4", "Text5", "Text6", "Text7",
62 "Text8", "Text9", "Text10", "Text11", "Text12", "Text13", "Text14", "Text15",
63 "Text16", "Text17", "Text18", "Text19", "Text20", "Text21", "Text22",};
64
65 for (int i = 0; i < 22; i++) {
66 form.setFieldProperty(it[i], "textfont", bf, null);
67 form.setField(it[i], str[i]);
68 }
69
70
71 stamper.setFormFlattening(true);//如果为false那么生成的PDF文件还能编辑,一定要设为true
72 stamper.close();
73
74 Document doc = new Document();
75
76 PdfCopy copy = new PdfCopy(doc, out);
77 doc.open();
78 PdfImportedPage importPage = copy.getImportedPage(
79 new PdfReader(bos.toByteArray()), 1);
80 copy.addPage(importPage);
81
82
83 File sdCardDir_WaterImage = new File(Environment.getExternalStoragePublicDirectory(
84 Environment.DIRECTORY_DOWNLOADS), folderName_WaterImage);
85
86 String imagePath1 = sdCardDir_WaterImage + "/" + "Image" + id + "_1";
87 String imagePath2 = sdCardDir_WaterImage + "/" + "Image" + id + "_2";
88 //插入现场图片
89 Image image1 = Image.getInstance(imagePath1);
90 doc.add(image1);
91 Image image2 = Image.getInstance(imagePath2);
92 doc.add(image2);
93
94 doc.close();
95
96 Toast.makeText(this, "导出pdf完成", Toast.LENGTH_LONG).show();
97 } catch (IOException e) {
98 System.out.println(1);
99 } catch (BadPdfFormatException e) {
100 e.printStackTrace();
101 } catch (DocumentException e) {
102 e.printStackTrace();
103 }
104 }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,944
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,470
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,284
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,100
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,730
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,766