Android根据pdf模板⽣成pdf⽂件
我们需要⽣成⼀些固定格式的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) {
SimpleDateFormat simpleDateFormat =
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("HHmmss");// HH:mm:ss
//设置默认时区
simpleDateFormat.tTimeZone(android.icu.TimeZone("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 ExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), folderName_WaterDB);
File sdCardDir_WaterPdf = new ExternalStorageDirectory(),
folderName_WaterPdf);
//模板路径
String templatePath = sdCardDir_PdfTemplate + "/" + "WaterTemplate.pdf";
//⽣成的新⽂件路径
String newPDFPath = sdCardDir_WaterPdf + "/" +
mWaterInfo.SamplingDate + "_" + mWaterInfo.WellNumber + "_" + sim2 + ".pdf";
/**
* 使⽤中⽂字体
* 如果是利⽤ AcroFields填充值的不需要在程序中设置字体,在模板⽂件中设置字体为中⽂字体就⾏了
*/
BaFont bf = null;
try {从零开始多少字
bf = ateFont("STSong-Light", "UniGB-UCS2-H", BaFont.NOT_EMBEDDED);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Font FontChine = 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 = AcroFields();
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.tFieldProperty(it[i], "textfont", bf, null);
form.tField(it[i], str[i]);
form.tField(it[i], str[i]);
}
stamper.tFormFlattening(true);//如果为fal那么⽣成的PDF⽂件还能编辑,⼀定要设为true stamper.clo();
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage = ImportedPage(
new ByteArray()), 1);
copy.addPage(importPage);
File sdCardDir_WaterImage = new ExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), folderName_WaterImage);
String imagePath1 = sdCardDir_WaterImage + "/" + "Image" + id + "_1";
String imagePath2 = sdCardDir_WaterImage + "/" + "Image" + id + "_2";
//插⼊现场图⽚
Image image1 = Instance(imagePath1);
doc.add(image1);
Image image2 = Instance(imagePath2);
doc.add(image2);
doc.clo();
诛仙青云志第二季
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();
}
}