Android根據pdf模板生成pdf文件
- 2021 年 1 月 23 日
- 筆記
- Android java, Anroid
我們需要生成一些固定格式的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 }