亲宝软件园·资讯

展开

Android根据pdf模板生成pdf文件

别具匠心 人气:0

我们需要生成一些固定格式的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     }

 

加载全部内容

相关教程
猜你喜欢
用户评论