我们需要生成一些固定格式的pdf文件或者一些报表数据,那么我们可以用 iText包去做。
需要包含的jar包:iText-5.0.6.jar iTextAsian.jar ,怎样jar包导入工程,在这里就不再赘述了,可自行网上搜索。
1、在word里建立一个表单,也就是你的pdf模板格式
2、把word保存为pdf格式
3、用迅捷pdf编辑器打开保存的pdf
保存pdf为模板,取名名字“PdfTemplate”(随便取名)。
以下是代码部分:
public void FillPdfTemplate(String id) {
android.icu.text.SimpleDateFormat simpleDateFormat =
new android.icu.text.SimpleDateFormat("HHmmss");// HH:mm:ss
//设置默认时区
simpleDateFormat.setTimeZone(android.icu.util.TimeZone.getTimeZone("GMT+8:00"));
//获取当前时间
Date date2 = new Date(System.currentTimeMillis());
String sim2 = simpleDateFormat.format(date2);
String folderName_WaterImage = "WaterImage";
String folderName_WaterDB = "WaterDB";
String folderName_WaterPdf = "WaterPdf";
File sdCardDir_PdfTemplate = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), folderName_WaterDB);
File sdCardDir_WaterPdf = new File(Environment.getExternalStorageDirectory(),
folderName_WaterPdf);
//模板路径
String templatePath = sdCardDir_PdfTemplate + "/" + "WaterTemplate.pdf";
//生成的新文件路径
String newPDFPath = sdCardDir_WaterPdf + "/" +
mWaterInfo.SamplingDate + "_" + mWaterInfo.WellNumber + "_" + sim2 + ".pdf";
/**
* 使用中文字体
* 如果是利用 AcroFields填充值的不需要在程序中设置字体,在模板文件中设置字体为中文字体就行了
*/
BaseFont bf = null;
try {
bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Font FontChinese = new Font(bf, 12, Font.NORMAL);
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
try {
out = new FileOutputStream(newPDFPath);//输出流
reader = new PdfReader(templatePath);//读取pdf模板
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
String[] strDate = mWaterInfo.SamplingDate.split("-");
String[] str = {
mWaterInfo.WellNumber, mWaterInfo.Longitude + "," + mWaterInfo.Latitude,
strDate[0], strDate[1], strDate[2], mWaterInfo.SamplingTime,
mWaterInfo.SampleMethods, mWaterInfo.SampleDepth, mWaterInfo.Temperature,
mWaterInfo.Weather, mWaterInfo.WaterLevel, mWaterInfo.WaterTemp1,
mWaterInfo.ORP1, mWaterInfo.DO1, mWaterInfo.pH1, mWaterInfo.CT1, mWaterInfo.NTU1,
mWaterInfo.Smell, mWaterInfo.Thing, mWaterInfo.Color, mWaterInfo.SamplingName,
mWaterInfo.RecordingName};
String[] it = new String[]{
"Text1", "Text2", "Text3", "Text4", "Text5", "Text6", "Text7",
"Text8", "Text9", "Text10", "Text11", "Text12", "Text13", "Text14", "Text15",
"Text16", "Text17", "Text18", "Text19", "Text20", "Text21", "Text22",};
for (int i = 0; i < 22; i++) {
form.setFieldProperty(it[i], "textfont", bf, null);
form.setField(it[i], str[i]);
}
stamper.setFormFlattening(true);//如果为false那么生成的PDF文件还能编辑,一定要设为true
stamper.close();
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage = copy.getImportedPage(
new PdfReader(bos.toByteArray()), 1);
copy.addPage(importPage);
File sdCardDir_WaterImage = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), folderName_WaterImage);
String imagePath1 = sdCardDir_WaterImage + "/" + "Image" + id + "_1";
String imagePath2 = sdCardDir_WaterImage + "/" + "Image" + id + "_2";
//插入现场图片
Image image1 = Image.getInstance(imagePath1);
doc.add(image1);
Image image2 = Image.getInstance(imagePath2);
doc.add(image2);
doc.close();
Toast.makeText(this, "导出pdf完成", Toast.LENGTH_LONG).show();
} catch (IOException e) {
System.out.println(1);
} catch (BadPdfFormatException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}