java根据模板生成pdf

java

新手入门 

1.maven pom.xml添加依赖:

<dependency>

    <groupId>com.itextpdf</groupId>

    <artifactId>itextpdf</artifactId>

    <version>5.4.3</version>

</dependency>

2.利用adobe acrobat编辑好模板,当然福昕编辑器,迅捷pdf编辑器也是可以的。 

 先用word编辑好你要生成好的pdf的样式,另存为pdf,再用pdf编辑器设置文本域,保存。

3.Java代码:

 把模板拷到resouces下,

从:c/system/fonts/DENG.TTF拷到resources/fonts/目录下。先找到等线,点击进去,复制 《 等线 常规  》   这个字体 。   等线常规  就是DENG.TTF

 源码链接:https://files-cdn.cnblogs.com/files/la-tiao-jun-blog/test.rar

public static void main(String[] args) {

Map<String,String> map = new HashMap<String,String>();

        map.put("applyNo","NEW201906172519");

        map.put("loanTerm","3");

        map.put("loanDate","2020-05-20");

        map.put("finishDate","2020-08-20");

        map.put("loanamt","25833.25");

        map.put("amountRepaid","25833.25");

        map.put("loanStatus", "歮靐敩龘我");

        map.put("cardNo", "赣A DN983");

        map.put("vinNo", "BY23424324");

        map.put("modelName", "2020款 运动版 1.5T 自动两驱精英型");

        map.put("mortgagePerson","中国人寿财产保险股份有限公司");

        map.put("carEquityNo", "New4234324324432432");

        Map<String,Object> o=new HashMap<String,Object>();

        o.put("dataMap",map);

pdfout(o);

}

public static void pdfout(Map<String,Object> o){

        // 模板路径  

        String templatePath = getTemplatePath();

        System.out.println("模板路径:"+templatePath);

        // 生成的新文件路径  

        String newPDFPath = "d:/test3.pdf";

        PdfReader reader;

        FileOutputStream out;

        ByteArrayOutputStream bos;

        PdfStamper stamper;

        try {

        String fontPath = getFontPath();

        System.out.println("fontPath:"+fontPath);

            BaseFont bf = BaseFont.createFont(fontPath , BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

            //Font FontChinese = new Font(bf, 5, Font.NORMAL);

            

            out = new FileOutputStream(newPDFPath);// 输出流

            reader = new PdfReader(templatePath);// 读取pdf模板  

            bos = new ByteArrayOutputStream();

            stamper = new PdfStamper(reader, bos);

            AcroFields form = stamper.getAcroFields();

            //文字类的内容处理

            Map<String,String> datemap = (Map<String,String>)o.get("dataMap");

            form.addSubstitutionFont(bf);

            for(String key : datemap.keySet()){

                String value = datemap.get(key);

                form.setField(key,value);

            }

            stamper.setFormFlattening(true);// 如果为false,生成的PDF文件可以编辑,如果为true,生成的PDF文件不可以编辑

            stamper.close();

            Document doc = new Document();

           //Font font = new Font(bf, 32);

            PdfCopy copy = new PdfCopy(doc, out);

            

            doc.open();

            PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);

            copy.addPage(importPage);

            doc.close();

        } catch (IOException e) {

            System.out.println(e);

        } catch (DocumentException e) {

            System.out.println(e);

        }

}

 

    private static String getFontPath(){

        String path="";

        // 1. 生产环境路径

        ClassLoader classLoader= Thread.currentThread().getContextClassLoader();

        java.net.URL url = (classLoader==null)?null:classLoader.getResource("/");

        String threadCurrentPath = (url==null)?"":url.getPath();

        // 2. 如果线程获取为null,则使用当前PdfUtils.class加载路径

        if(threadCurrentPath == null || "".equals(threadCurrentPath.trim())){

             path = PdfUtils2.class.getClass().getResource("/").getPath();

        }

        // 3.拼接字体路径

        StringBuffer stringBuffer = new StringBuffer(path);

        stringBuffer.append("/fonts/DENG.TTF");

        path = stringBuffer.toString();

        //logger.info("getFontPath threadCurrentPath: {}  path: {}",threadCurrentPath,path);

        return path;

    }

    

    private static String getTemplatePath(){

        String path="";

        // 1. 生产环境路径

        ClassLoader classLoader= Thread.currentThread().getContextClassLoader();

        java.net.URL url = (classLoader==null)?null:classLoader.getResource("/");

        String threadCurrentPath = (url==null)?"":url.getPath();

        // 2. 如果线程获取为null,则使用当前PdfUtils.class加载路径

        if(threadCurrentPath == null || "".equals(threadCurrentPath.trim())){

             path = PdfUtils2.class.getClass().getResource("/").getPath();

        }

        // 3.拼接字体路径

        StringBuffer stringBuffer = new StringBuffer(path);

        stringBuffer.append("/dai_adobe.pdf");

        path = stringBuffer.toString();

        //logger.info("getFontPath threadCurrentPath: {}  path: {}",threadCurrentPath,path);

        return path;

    }

以上是 java根据模板生成pdf 的全部内容, 来源链接: utcz.com/z/394112.html

回到顶部