java程序通过 pdf模板 添加pdf表单数据并打印

1:用adobe acrobat设置pdf的表单属性


第二步:写java程序


package document.pdf;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

public class OperatePdf {
    public static void main(String[] args) {
        fromPDFTempletToPdfWithValue();
    }
    
    
    public static void fromPDFTempletToPdfWithValue(){
        String fileName = "f:/test.pdf";
        
        try {
            //获得pdf阅读器
            PdfReader reader = new PdfReader(fileName);
            //字节数组输出流
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            
            PdfStamper ps = new PdfStamper(reader,bos);
            
            BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            //设置中文12号字体
            Font fontChinese = new Font(bf,12,Font.NORMAL);
            //自定义表单
            AcroFields form = ps.getAcroFields();
            
            String str = "黑龙江沙河科技";
            if(str.length()>6)
                str = str.substring(0,6);
            form.setField("company", str);
            
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateStr = format.format(date);
            form.setField("date", dateStr);
            
            List<List<String>> lists = new ArrayList<List<String>>();
            List<String> list = new ArrayList<String>();
            for (int j = 0; j <5; j++) {
                list.add("张三"+(j+1));
                list.add("经理"+(j+1));
                list.add("身份证");
                list.add("4111111111111");
                lists.add(list);
            }
            for(int i=0;i<lists.size();i++){
                List<String> list2 = lists.get(i);
                form.setField("seq"+(i+1),String.valueOf(i+1));
                form.setField("name"+i,list2.get(0));
                form.setField("position"+i,list2.get(1));
                form.setField("certificate"+i,list2.get(2));
                form.setField("number"+i,list2.get(3));
            }
            // 设置为true/false在点击生成的pdf文档的填充域时有区别,
            ps.setFormFlattening(true);
            ps.close();
            FileOutputStream fos = new FileOutputStream("f:/1.pdf");
            fos.write(bos.toByteArray());
            
        } catch (IOException | DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

}

public static void printFile(String path) throws Exception {
        File file = new File(path);
        File[] fies=file.listFiles();
            for(File f:fies){
            System.out.println("file "+f.getName());
                 String fileExt=f.getName().substring(f.getName().indexOf(".")+1,f.getName().length());
                  if("pdf".equalsIgnoreCase(fileExt)){
                    String filepath=path+File.separator+f.getName();
                    File pdfFile=new File(filepath);
                    //构建打印请求属性集
                    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
                    //设置打印格式,因为未确定文件类型,这里选择AUTOSENSE
                    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
                    //查找所有的可用打印服务
                    PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
                    //定位默认的打印服务
                    PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
                     //显示打印对话框  
                    //PrintService service = ServiceUI.printDialog(null, 200, 200, printService,   defaultService, flavor, pras);
                    if(defaultService!=null){
                        DocPrintJob job = defaultService.createPrintJob(); //创建打印作业
                        FileInputStream fis = new FileInputStream(pdfFile); //构造待打印的文件流
                        DocAttributeSet das = new HashDocAttributeSet();
                        Doc doc = new SimpleDoc(fis, flavor, das); //建立打印文件格式
                        job.print(doc, pras); //进行文件的打印
                    }
                    f.delete();
            }
        }
      }

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值