我们需要生成一些固定格式的pdf文件或者一些报表数据,那么我们可以用 iText包去做。
需要包含的jar包:iText-5.0.6.jar iTextAsian.jar ,怎样jar包导入工程,在这里就不再赘述了,可自行网上搜索。
1、再word里建立一个表单,也就是你的pdf模板格式
2、把word保存为pdf格式
3、用迅捷pdf编辑器打开保存的pdf
@H_301_54@
保存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 @R_419_3462@ stamper; 43 try { 44 out = new fileOutputStream(newpdfPath);//输出流 45 reader = new pdfReader(templatePath);//读取pdf模板 46 bos = new ByteArrayOutputStream(); 47 stamper = new @R_419_3462@(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 }
总结
以上是内存溢出为你收集整理的Android根据pdf模板生成pdf文件全部内容,希望文章能够帮你解决Android根据pdf模板生成pdf文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)