给pdf动态添加数据

依赖


            fr.opensagres.xdocreport
            org.apache.poi.xwpf.converter.pdf
            1.0.4
        
        
            com.aspose
            aspose-pdf
            11.5.0
        
        
            commons-io
            commons-io
            2.5
        
        
             e-iceblue 
            spire.pdf
            3.11.6
        



license.xml  破解pdf


  
    
      Aspose.Total for Java
      Aspose.Pdf for Java
    
    Enterprise
    20991231
    20991231
    8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7
  
  sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=




后端
  先将服务器上的pdf和电子签章(需填写动态字段,用于动态赋值)保存到项目目录下,然后将pdf填充数据之后保存到服务器中
 public int downAgreement(Person person) throws IOException {

		//绝对路径
        String localUrl="F:\\e-modules\\person\\src\\main\\pdf";

        Person person1=personMapper.selectTaxName(person.getTaxNo());
        // 如果文件夹不存在则创建
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String format = simpleDateFormat.format(date);
        File file1 = new File(localUrl+"/"+format);
        if(!file1.exists()){//如果文件夹不存在
            file1.mkdir();//创建文件夹
        }
		//读取   license.xml  需要手动修改pom依赖包才可以使用,见地址文章另一篇   替换本地仓库里面com\aspose\aspose-pdf文件夹下的11.5.0文件夹
               https://download.csdn.net/download/weixin_50391597/42869934
        InputStream license = PersonServiceImpl.class.getClassLoader().getResourceAsStream("license.xml");

//            BufferedReader reader = new BufferedReader(new InputStreamReader(license));
//            StringBuilder sb = new StringBuilder();
//            String line = null;
//            while (true) {
//                try {
//                    if (!((line = reader.readLine()) != null)) break;
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//                sb.append(line + "/n");
//            }
//            System.out.println("input:" + sb.toString());
            try {
                new License().setLicense(license);
            } catch (Exception e) {
                e.printStackTrace();
            }
   
        //查询这个的协议    动态获取服务器上保存的协议
        Tax tax = personMapper.selectPdf(person.getNo());
        //线上图片地址
        UUID uuid = UUID.randomUUID();
		//工具类  将服务器上的pdf放入项目目录下    见另一篇文文章
        HttpUtils.saveImageToDisk(imageUrl+tax.getPdf(),localUrl+"/"+format+"/"+uuid.toString()+".pdf");
        File f = new File(localUrl+"/"+format+"/"+uuid.toString()+".pdf");
        String decode = URLDecoder.decode(f.getAbsolutePath());
        Document pdfDoc = new Document(decode);
        PageCollection pages = pdfDoc.getPages();
        Font font = FontRepository.findFont("Microsoft YaHei");
        System.out.println("文档总页码数:" + pages.size());
        // 主体
        TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("taxName");
        pages.accept(textFragmentAbsorber);
        for (TextFragment textFragment : (Iterable) textFragmentAbsorber.getTextFragments()) {
            textFragment.getTextState().setFont(font);
            textFragment.setText(person1.getTaxName());
            //textFragment.getTextState().setFontStyle(1);// 字体加粗
        }
        //
        textFragmentAbsorber = new TextFragmentAbsorber("taxName");
        pages.accept(textFragmentAbsorber);
        for (TextFragment textFragment : (Iterable) textFragmentAbsorber.getTextFragments()) {
            textFragment.getTextState().setFont(font);
            textFragment.setText(person1.getTaxName());
        }
        // 名称
        textFragmentAbsorber = new TextFragmentAbsorber("name");
        pages.accept(textFragmentAbsorber);
        for (TextFragment textFragment : (Iterable) textFragmentAbsorber.getTextFragments()) {
            textFragment.getTextState().setFont(font);
            textFragment.setText(person.getName());
        }
        // 
        textFragmentAbsorber = new TextFragmentAbsorber("idNo");
        pages.accept(textFragmentAbsorber);
        for (TextFragment textFragment : (Iterable) textFragmentAbsorber.getTextFragments()) {
            textFragment.getTextState().setFont(font);
            textFragment.setText(person.getIdCard());
        }
        // 手机号
        textFragmentAbsorber = new TextFragmentAbsorber("phone");
        pages.accept(textFragmentAbsorber);
        for (TextFragment textFragment : (Iterable) textFragmentAbsorber.getTextFragments()) {
            textFragment.getTextState().setFont(font);
            textFragment.setText(person.getPhone());
        }

        //日期
        textFragmentAbsorber = new TextFragmentAbsorber("date");
        pages.accept(textFragmentAbsorber);
        for (TextFragment textFragment : (Iterable) textFragmentAbsorber.getTextFragments()) {
            textFragment.getTextState().setFont(font);
            textFragment.setText(DateUtils.dateTime(person.getCreateTime()).substring(0,10));
        }
        pdfDoc.save( localUrl+"/"+format + File.separator + person.getIdCard()+person.getTaxNo()+".pdf");
        //读取pdf
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile(localUrl+"/" +format + File.separator + person.getIdCard()+person.getTaxNo()+".pdf");
        doc.getPages().add();  //先添加一张
        //获取文档的第几页
        PdfPageBase page = doc.getPages().get(5);
        //动态获取签章
        UUID uuid1 = UUID.randomUUID();
        HttpUtils.saveImageToDisk(imageUrl+tax.getPhoto(),localUrl+"/"+format+"/"+uuid1.toString()+".png");
        PdfImage image = PdfImage.fromFile(localUrl+"/"+format+"/"+uuid1.toString()+".png");
        float width = image.getWidth() * 0.3f;
        float height = image.getHeight() * 0.3f;
        //放置图片的位置
        page.getCanvas().drawImage(image, 100,130, width, height);
        //移除最后一页页
//        System.out.println("888"+doc.getPages().getCount());
        doc.getPages().remove(doc.getPages().get(doc.getPages().getCount()-1));
        //保存PDF文件导项目目录下
        doc.saveToFile(localUrl+"/" +format + File.separator + person.getIdCard()+person.getTaxNo()+".pdf");
        //将签约好的电子版保存到服务器中
        File file = new File(localUrl + "/" + format + File.separator + person.getIdCard() + person.getTaxNo() + ".pdf");
        FileInputStream input = new FileInputStream(file);
        MultipartFile multipartFile =new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));

        String resultUrl = iFileSrevice.getFile(multipartFile);
        JSONObject respJson = JSONObject.parseObject(resultUrl);
        Object resultUrl1 =  respJson.get("data");
        String url0 = resultUrl1.toString();
        JSONObject respJson1 = JSONObject.parseObject(url0);
        String url = respJson1.get("url").toString();
        //截取
        //获得第一个点的位置
        int index=url.indexOf("/");
        //根据第一个点的位置 获得第二个点的位置
        index=url.indexOf("/", index+1);
        index=url.indexOf("/", index+1);
        //根据第二个点的位置,截取 字符串。得到结果 result
        String result=url.substring(index);
        person.setAgreementPhoto(result);
        personMapper.updatePersonAgreementPhoto(person);
        //删除从服务器拉下来的临时pdf协议
        f.delete();
        //删除完成后的临时签约电子协议
        File file00 = new File(localUrl+"/" +format + File.separator + person.getIdCard()+person.getTaxNo()+".pdf");
        file00.delete();
        //删除临时签章
        File file2 = new File(localUrl + "/" + format + "/" + uuid1.toString() + ".png");
        file2.delete();

        doc.close();
        return 1;
    }



你可能感兴趣的:(Java中给pdf填入数据,perl,测试工具,struts)