Itext7 generate pdf with Exception “Pdf indirect object belongs to other PDF document. Copy object to current pdf document.”

后端 未结 3 1244
南旧
南旧 2020-12-18 21:28

i want to generate a pdf with itext 7,but some wrong happens to us:

com.itextpdf.kernel.PdfException: Pdf indirect object belongs to other PDF document. Copy         


        
3条回答
  •  南笙
    南笙 (楼主)
    2020-12-18 22:30

    To improve preformance you should reuse FontProgram:

    private FontProgram bfChinese = null;
    
    public GeneratePDFService() {
        String PdfFontPath = EnvironmentUtils.getClasspathFilePath("font/MSYH.TTF");
        try {
            bfChinese =  FontProgramFactory.createFont(PdfFontPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    And then:

    public void createPdf() {
        ...
        PdfFont font = PdfFontFactory.createFont(bfChinese, "Identity-H", true);
        Paragraph p = new Paragraph("test").setFont(font);
        document.add(p);
        ...
    }
    

提交回复
热议问题