1

如何将图像作为页眉添加到 XWPF 文档中的每一页?我已经尝试了我能想到的所有可能的事情,下面是我的代码:

        XWPFDocument docx = new XWPFDocument();
        CTSectPr sectPr = docx.getDocument().getBody().addNewSectPr();
        XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(docx, sectPr);
        XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.FIRST);
        String imgFile="sample.png";
        header.addPictureData(new FileInputStream(imgFile), XWPFDocument.PICTURE_TYPE_PNG);
        String nameoffile ="customer"+".docx";       
        FileOutputStream out = new FileOutputStream(nameoffile);
        docx.write(out);
        out.close();

但是,这在 java.lang.IndexOutOfBoundsException 行给了我一个 java.lang.IndexOutOfBoundsException

4

1 回答 1

0

您应该为图像添加正确的目录。可能“new FileInputStream(imgFile)”为空。将您的图像放在目录中,然后将路径设置为:

String imgFile="C:\Users\{user}\Desktop\Project\sample.png";
于 2021-02-17T11:30:06.273 回答