Java 在word文档的标题中添加图像时出现问题

Java 在word文档的标题中添加图像时出现问题,java,spring-boot,ms-word,apache-poi,Java,Spring Boot,Ms Word,Apache Poi,我正在word文档的标题中添加图片。它显示图像的边框,并表示“当前无法显示图像”。如果我将文本添加到标题,它将显示文本,如果我在文档正文中添加图像,它也将显示图像。获取图像时,它会在标题上显示文本,但不会显示图像 我的支票快用完了,有人能告诉我吗 谢谢大家! public static void createHeaderAndFotter(XWPFDocument document) throws IOException, BadElementException, InvalidFormatEx

我正在word文档的标题中添加图片。它显示图像的边框,并表示“当前无法显示图像”。如果我将文本添加到标题,它将显示文本,如果我在文档正文中添加图像,它也将显示图像。获取图像时,它会在标题上显示文本,但不会显示图像

我的支票快用完了,有人能告诉我吗

谢谢大家!

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"); 
}
如果我删除这一行上的逗号并对前面的一行进行注释,那么它会添加图像

        XWPFParagraph paragraph = document.createParagraph();

我相信这是否有效在很大程度上取决于所使用的
apachepoi
版本。在以前的
apachepoi
版本中,页眉/页脚中的图片存在多个问题

下面是使用ApachePOI4.0.1的最简单的工作代码。建议始终使用最新的稳定版本:

代码:

结果:


请参阅,以了解以前apache poi版本中页眉/页脚中图片的多个问题。谢谢,我也尝试了此方法,但也不起作用。我已将poi更新到不同的版本,包括最新版本,但仍然不起作用。我相信你是对的,但不知何故,这并不能解决问题。我所做的是在项目中包含一个带有页眉和页脚的模板。现在一切正常。无论如何,谢谢你的帮助!我上面的代码示例已经完成,可以使用ApachePOI4.0.1工作。因此,我不清楚它如何以及为什么对你不起作用。我用不同的方式解决了我的问题(对我的项目来说更有效),但我将标记你的答案,因为代码是直截了当的,我猜我的依赖性有问题。非常感谢。
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();

 }
}