Search code examples
javaapachems-wordapache-poi

Apache POI header.xml Docx output issue


I am new to Apache poi. I am using apache poi version 3.10 and java 1.6.

When I download docx file, I should get file with watermark. After downloading, when I try to open this docx, it gives me the error: “The Office Open XML file cannot be opened because there are problems with the content” and in the description: text/xml declaration can only appear at the very beginning of the input. Location word/header4.xml

However, when I open docx viewer from browser, it perfectly shows me file with content, header and watermark.

Couldn't understand what gives error

XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
headerFooterPolicy.createWatermark("Watermark");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
document.write(baos);
PortletResponseUtil.sendFile(request, res, outputFile,
baos.toByteArray(), contentType);

I tried this links Add watermark in word document using Apache POI Add a watermark with Apache POI to a docx that already have a header Is it possible for Apache POI to add / remove watermark to Microsoft Word

What I expect that I could open docx file with watermark in word document.


Solution

  • When I download docx file, I should get file with watermark. After downloading, when I try to open this docx, it gives me the error: “The Office Open XML file cannot be opened because there are problems with the content” and in the description: text/xml declaration can only appear at the very beginning of the input. Location word/header4.xml

    Used custom library