apache poi: header does not load correctly

130 Views Asked by At

I have here a header header template saved in a Word document (Dok3.docx). I am loading this Word document as an XWPFDocument. I am copying the header from this Word document to a new Word document (CreateWordHeader.docx). But the header does not load correctly incorrect header. As you can see, the image is not loaded, and the format of the header is a mess. And when opening the generated CreatWordHeader Word document, i get the error: Word found unreadable content error.Anyone an idea?

public class Main {
    public static void main(String[] args) throws IOException {
        XWPFDocument doc = new XWPFDocument();
        XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT);
        header.setHeaderFooter(loadHeader().getHeaderArray(0)._getHdrFtr());
        FileOutputStream out = new FileOutputStream("CreateWordHeader.docx");
        doc.write(out);
        out.close();
        doc.close();

    }

    private static XWPFDocument loadHeader() {
        try {
            return new XWPFDocument(Objects.requireNonNull(Main.class.getClassLoader().
                    getResourceAsStream("Dok3.docx")));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
0

There are 0 best solutions below