itextpdf

本文详细介绍了如何在Java项目中使用iTextPDF库来创建、编辑和读取PDF文档,包括添加文本、图像、表格等内容,以及设置样式和安全性的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   public  ByteArrayOutputStream createPdfStream(String templateFilePath, Map<String, String> resultMap) {
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");

//        String path = getClass().getResource("/").getPath();
//        if (path.indexOf("WEB-INF/classes") != -1) {
//            path = path.substring(1, path.indexOf("WEB-INF/classes"));
//            path = "/"+path + "font/simsun.ttf";
//        }
        //字体存放的地址
        String path1 = "D:\\Project\\acpartner\\app\\biz\\shared\\src\\main\\resources\\STFANGSO.ttf";
        ByteArrayOutputStream ba = new ByteArrayOutputStream();
        PdfReader reader = null;
        PdfStamper stamp = null;
        try {
            reader = new PdfReader(templateFilePath);
            stamp = new PdfStamper(reader, ba);
            ArrayList<BaseFont> fontList = new ArrayList<>();
            BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            BaseFont bf = BaseFont.createFont(path1, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            fontList.add(baseFont);
            //获取模版中的字段
            AcroFields form = stamp.getAcroFields();
            //form.setSubstitutionFonts(fontList);
            //填充表单
            if (resultMap != null) {
                //依次获取要填充的数据
                for (Map.Entry<String, String> entry : resultMap.entrySet()) {
                    if (entry.getKey().equals("partyAName")){
                        form.setFieldProperty(entry.getKey(),"textsize",32f,null);
                        form.setFieldProperty(entry.getKey(),"textfont",bf,null);
                    }
                    if (entry.getKey().equals("startTime")){
                        form.setFieldProperty(entry.getKey(),"textfont",bf,null);
                        form.setFieldProperty(entry.getKey(),"textsize",16f,null);
                    }
                    form.setField(entry.getKey(), entry.getValue());
                }
            }
            stamp.setFormFlattening(true);//设置为不能编辑
        } catch (IOException ioException) {
            ioException.printStackTrace();
        } catch (DocumentException documentException) {
            documentException.printStackTrace();
        }finally {

            if (null != stamp) {
                try {
                    stamp.close();
                } catch (DocumentException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != reader) {
                reader.close();
            }
        }
        return ba;
    }
    @Test
    public void pdfTest() {
        //定义要填充的数据封装成Map对象
        Map<String, String> resultMap= new HashMap<>(0);
        resultMap.put( "partyAName", "湖人总冠军公司");
        resultMap.put( "chainInfo", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        resultMap.put("startTime","2021年1月12日");
        resultMap.put("endTime","2022年1月12日");
        resultMap.put("chainTime","2022年1月12日");
        //根据模板填充数据源
        PDFUtil pdfUtil = new PDFUtil();
        try{
            //PDF地址
            String templatePath = "C:\\Users\\wb-pxh\\Downloads\\Distribution_gold.pdf";
            ByteArrayOutputStream pdf = pdfUtil.createPdfStream(templatePath,  resultMap);
            //输出Pdf的位置
            String newPadPath="C:\\Users\\wb-pxh\\Downloads\\pdf2.pdf";
            FileOutputStream out = new FileOutputStream(newPadPath);
            out.write(pdf.toByteArray());
            out.flush();
            out.close();
            pdf.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值