• java利用itext填写pdf模板并导出


    1.先用word做出界面

    2.再转换成pdf格式

    3.用Adobe Acrobat 打开你刚刚用word转换成的pdf

    会出现如下界面

    下一步

    点击浏览,选择刚才你转换好的pdf

    下一步

    4.打开后它会自动侦测并命名表单域,右键表单域,点击属性,出现文本域属性对话框,有的人说要改成中文字体,可是我没有改一样成功啦

    5.一般情况下不需要修改什么东西,至少我没有修改哦

    6.直接另存为就行了

    ************************上程序********************************

    准备:itext的jar包包:官网:http://sourceforge.net/projects/itext/files/

    因为是利用模板,所以不需要建立字体来解决中文不显示的问题

    public void fillTemplate(){//利用模板生成pdf
                    //模板路径
            String templatePath = "pdfFolder/template_demo.pdf";
                    //生成的新文件路径
            String newPDFPath = "pdfFolder/newPdf.pdf";
            PdfReader reader;
            FileOutputStream out;
            ByteArrayOutputStream bos;
            PdfStamper stamper;
            try {
                out = new FileOutputStream(newPDFPath);//输出流
                reader = new PdfReader(templatePath);//读取pdf模板
                bos = new ByteArrayOutputStream();
                stamper = new PdfStamper(reader, bos);
                AcroFields form = stamper.getAcroFields();
                 
                String[] str = {"123456789","小鲁","男","1994-00-00",
                        "130222111133338888"
                        ,"河北省唐山市"};
                int i = 0;
                java.util.Iterator<String> it = form.getFields().keySet().iterator();
                while(it.hasNext()){
                    String name = it.next().toString();
                    System.out.println(name);
                    form.setField(name, str[i++]);
                }
                stamper.setFormFlattening(true);//如果为false那么生成的PDF文件还能编辑,一定要设为true
                stamper.close();
                 
                Document doc = new Document();
                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(1);
            } catch (DocumentException e) {
                System.out.println(2);
            }
     
             
        } 

    模板如图:

    程序结果如图:

    可以看到,少了一个鲁......于是我把模板的表单域的字体改成了宋体,结果中文一个也不显示了,所以我判断是他那个字体支持的中文比较少吧,先不管这个了

    现在都两点多了(不是下午两点多啊。。。)

    到此时,利用模板生成pdf已经over了,我再说说入门的hello word 级别的程序吧,反正闲着也是闲着

    程序一:

    public void test1(){//生成pdf
            Document document = new Document();
            try {
                PdfWriter.getInstance(document, new FileOutputStream("pdfFolder/1.pdf"));
                document.open();
                document.add(new Paragraph("hello word"));
                document.close();
            } catch (FileNotFoundException | DocumentException e) {
                System.out.println("file create exception");
            }
        }

    结果:

    可是如果要输出中文呢,上面这个就不行了,就要用到语言包了

    最新亚洲语言包:http://sourceforge.net/projects/itext/files/extrajars/

    public void test1_1(){
            BaseFont bf;
            Font font = null;
            try {
                bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H",
                        BaseFont.NOT_EMBEDDED);//创建字体
                font = new Font(bf,12);//使用字体
            } catch (DocumentException | IOException e) {
                e.printStackTrace();
            }
            Document document = new Document();
            try {
                PdfWriter.getInstance(document, new FileOutputStream("pdfFolder/2.pdf"));
                document.open();
                document.add(new Paragraph("hello word 你好 世界",font));//引用字体
                document.close();
            } catch (FileNotFoundException | DocumentException e) {
                System.out.println("file create exception");
            }
        }

    结果如下:

    另外一种方法:我不用第三方语言包:

    我是在工程目录里面新建了一个字体文件夹Font,然后把宋体的字体文件拷贝到这个文件夹里面了

    上程序:

    public void test1_2(){
            BaseFont bf;
            Font font = null;
            try {
                bf = BaseFont.createFont("Font/simsun.ttc,1", //注意这里有一个,1
                        BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                font = new Font(bf,12);
            } catch (DocumentException | IOException e) {
                e.printStackTrace();
            }
            Document document = new Document();
            try {
                PdfWriter.getInstance(document, new FileOutputStream("pdfFolder/3.pdf"));
                document.open();
                document.add(new Paragraph("使用中文另外一种方法",font));
                document.close();
            } catch (FileNotFoundException | DocumentException e) {
                System.out.println("file create exception");
            }
        }

    结果“:

    我如果换成:华康少女文字W5(P).TTC,即

    bf = BaseFont.createFont("Font/华康少女文字W5(P).TTC,1", //simsun.ttc
    BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

    本文转自 https://www.cnblogs.com/LUA123/p/5108007.html

  • 相关阅读:
    firefox禁止更新提示
    laravel性能优化笔记片段
    laravel 7 在线教育项目实操笔记(2)后台基础功能开发
    一、笔记片段 迁移文件相关(创建表 字段 添加假数据等)
    路由、控制器笔记
    laravel rbac笔记片段
    二 笔记片段
    三 文件上传片段
    分享5个有趣的 JavaScript 代码片段
    异步上传文件
  • 原文地址:https://www.cnblogs.com/pypua/p/13353145.html
Copyright © 2020-2023  润新知