Java – Word document header add picture problem

Word document header add picture problem… here is a solution to the problem.

Word document header add picture problem

I added a picture to the header of a word document. It displays the frame of the image and displays “Image cannot be displayed at this time”. If I add text in the header, it shows the text, and if I add an image in the body of the document, it also shows the image. The same goes for getting an image, which displays text on the caption but no image.

I’m running out of checks, can someone give me advice?

Thanks!

public static void createHeaderAndFotter(XWPFDocument document) throws IOException, BadElementException, InvalidFormatException {

XWPFHeaderFooterPolicy headerFooterPolicy = document.getHeaderFooterPolicy();
    if (headerFooterPolicy == null) headerFooterPolicy = document.createHeaderFooterPolicy();

File image = new ClassPathResource("/static/images/NIAB_Header.bmp").getFile();
    BufferedImage bimg1 = ImageIO.read(image);
    int width = bimg1.getWidth();
    int height = bimg1.getHeight();
    String imageName= image.getName();

XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);

XWPFParagraph paragraph = header.createParagraph();
        XWPFParagraph paragraph = document.createParagraph();
    paragraph.setAlignment(ParagraphAlignment.CENTER);

XWPFRun run = paragraph.createRun();

run.addPicture(new FileInputStream(image), XWPFDocument.PICTURE_TYPE_PNG, imageName, Units.toEMU(width), Units.toEMU(height));
    run.setText("HEADER"); 
}

If I remove the comment on this line and comment the previous comment, then it adds the image

        XWPFParagraph paragraph = document.createParagraph();

Solution

I believe that whether this works depends a lot on the version of Apache Poi used. In previous versions of Apache Poi, there were multiple issues with images in headers/footers.

Below is the simplest working code using Apache Poi 4.0.1. It is recommended to always use the latest stable version. :

Code:

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.apache.poi.util.Units;

public class CreateWordHeaderWithImage {

public static void main(String[] args) throws Exception {

XWPFDocument doc = new XWPFDocument();

 the body content
  XWPFParagraph paragraph = doc.createParagraph();
  XWPFRun run = paragraph.createRun();  
  run.setText("The Body...");

 create header
  XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT);

 header's first paragraph
  paragraph = header.getParagraphArray(0);
  if (paragraph == null) paragraph = header.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.CENTER);

run = paragraph.createRun();

FileInputStream in = new FileInputStream("samplePict.jpeg");
  run.addPicture(in, Document.PICTURE_TYPE_JPEG, "samplePict.jpeg", Units.toEMU(100), Units.toEMU(50));
  in.close();  

run.setText("HEADER"); 

FileOutputStream out = new FileOutputStream("CreateWordHeaderWithImage.docx");
  doc.write(out);
  doc.close();
  out.close();

}
}

Result:

enter image description here

Related Problems and Solutions