Java 在XWPF文档中添加图像作为标题

Java 在XWPF文档中添加图像作为标题,java,xwpf,Java,Xwpf,如何将图像作为标题添加到XWPF文档中的每个页面? 我已经尝试了我能想到的一切,下面是我的代码: XWPFDocument docx = new XWPFDocument(); CTSectPr sectPr = docx.getDocument().getBody().addNewSectPr(); XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(docx, sectPr);

如何将图像作为标题添加到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,您应该为图像添加一个正确的目录。可能“新文件输入流(imgFile)”为空。 将图像放入目录,然后将路径设置为:

String imgFile="C:\Users\{user}\Desktop\Project\sample.png";

您应该为映像添加正确的目录。可能“新文件输入流(imgFile)”为空。 将图像放入目录,然后将路径设置为:

String imgFile="C:\Users\{user}\Desktop\Project\sample.png";