android 网页生成pdf,Android根据pdf模板生成pdf文件

1 public voidFillPdfTemplate(String id) {2 android.icu.text.SimpleDateFormat simpleDateFormat =

3 new android.icu.text.SimpleDateFormat("HHmmss");//HH:mm:ss4 //设置默认时区

5 simpleDateFormat.setTimeZone(android.icu.util.TimeZone.getTimeZone("GMT+8:00"));6 //获取当前时间

7 Date date2 = newDate(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 = newFile(Environment.getExternalStoragePublicDirectory(15 Environment.DIRECTORY_DOWNLOADS), folderName_WaterDB);16 File sdCardDir_WaterPdf = newFile(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 = newByteArrayOutputStream();47 stamper = newPdfStamper(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 = newString[]{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 = newDocument();75

76 PdfCopy copy = newPdfCopy(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 = newFile(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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值