AndroidiText向pdf模板插入数据和图片

更新时间:2023-07-31 06:16:48 阅读: 评论:0

AndroidiText向pdf模板插⼊数据和图⽚
需求
这些⽇志在写App程序,有这么⼀个需求,就是需要⽣成格式统⼀的⼀个pdf⽂件,并向固定表格中填充数据,并且再在pdf中追加两页图⽚。
⽅案
⼿⼯设计⼀个pdf模板,这个具体步骤就不再赘述,可以⽹上搜索。说⼀下模板制作的简单步骤:
1、⽤word制作你需要的表单模板
2、把word制作好的表单模板,另存为pdf格式。拉拉的幸福生活
3、⽤pdf编辑器(我⽤的迅捷pdf编辑器),打开pdf,然后进⾏表单编辑,在需要填充数据的的表格放⼊⽂本控件。
4、另存为pdf模板。
这个模板就是App读取并进⾏操作的pdf模板,通过这个模板就可以把数据填充到pdf中,并重新⽣成⼀个
新的pdf⽂件。
下⾯是代码部分,⽤到的jar包请看我的其他博⽂介绍,这⾥就不再赘述。
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);
File sdCardDir_WaterImage =new ExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), folderName_WaterImage);
//模板路径
String templatePath = sdCardDir_PdfTemplate +"/"+"WaterTemplate.pdf";
//⽣成的新⽂件路径
String newPDFPath = sdCardDir_WaterPdf +"/"+
mWaterInfo.SamplingDate +"_"+ mWaterInfo.WellNumber +"_"+ sim2 +".pdf";
String imagePath1 = sdCardDir_WaterImage +"/"+"Image"+ id +"_1";
平安无恙String imagePath2 = sdCardDir_WaterImage +"/"+"Image"+ id +"_2";
/**
* 使⽤中⽂字体
* 如果是利⽤ 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);
//下⾯是我需要填充的数据,String格式
//下⾯是我需要填充的数据,String格式
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, mWaterInfo.ReagenInfo, mWaterInfo.bkInfo};
//这个是pdf模板制作时,插⼊的⽂本控件名称
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",
"Text23","Text24"};
PdfReader reader =new PdfReader(templatePath);//对pdf进⾏读写
PdfStamper pdfStamper = null;
try{
pdfStamper =new PdfStamper(reader,new FileOutputStream(new File(newPDFPath)));
}catch(DocumentException e){
e.printStackTrace();
}
AcroFields acroFields = AcroFields();//获取pdf表单
Map<String, String> formData =new HashMap<>();
//formData.put("identify_number", Id_number());
int len = it.length;
//把所有的控件都填⼊相应的数据
运气好怎么形容for(int i =0; i < len; i++){
acroFields.tFieldProperty(it[i],"textfont", bf, null);
try{
acroFields.tField(it[i], str[i]);
}catch(DocumentException e){
e.printStackTrace();
}
}
PdfContentByte overContent2 = UnderContent(2);//追加⼀页
PdfContentByte overContent3 = UnderContent(3);//追加⼀页
Image idFontImg2 = Instance(imagePath1);//获取图⽚封装对象
Image idFontImg3 = Instance(imagePath2);//获取图⽚封装对象
Rectangle rectangleIdFont;
rectangleIdFont =new Rectangle(Width(), Height());
//设置图⽚位置,及缩放
idFontImg2.Width(), Height());
idFontImg3.Width(), Height());
try{
float x = Left();
float y = Bottom();
idFontImg2.tAbsolutePosition(x, y);
idFontImg3.tAbsolutePosition(x, y);
overContent2.addImage(idFontImg2);//将图⽚添加到pdf
overContent3.addImage(idFontImg3);//将图⽚添加到pdf
}catch(DocumentException e){
e.printStackTrace();
}
现代歌曲//addMaterMark(reader, pdfStamper, bfChine, timeWater);//追加⽔印
pdfStamper.tFormFlattening(true);//设置表单之后不可编辑
try{
pdfStamper.clo();
}catch(DocumentException e){
}catch(DocumentException e){
e.printStackTrace();
}
reader.clo();
Toast.makeText(this,"导出pdf成功", Toast.LENGTH_LONG).show();
程序调试了三四天,中间是各种问题,各种bug,各种报错,终于调试成功。欢迎交流!

本文发布于:2023-07-31 06:16:48,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1124456.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:模板   需要   数据   设置   表单   获取
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图