word文档页眉添加图片问题 [英] Problem adding image in the header of word document

查看:61
本文介绍了word文档页眉添加图片问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在 Word 文档的标题中添加图片.它显示图像的框架并显示当前无法显示图像".如果我在标题中添加文本,它会显示文本,如果我在文档正文中添加图像,它也会显示图像.获取图像也是如此,它在标题上显示文本,但没有图像.

I'm adding a picture in the header of a word document. It shows a frame for the image and says "the image cannot currently be display". If I add text to the header it show the text, and if I add the image in the document body, it also shows the image. So is getting the image and it show text on the header, but no the image.

我的支票快用完了,有人可以给我建议吗?

I'm running out of checkings, can anyone advise with this please?

谢谢!

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 commment on this line and comment the one before, then it adds the image

        XWPFParagraph paragraph = document.createParagraph();

推荐答案

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

I believe whether this works or not highly depends on apache poiversion used. There was multiple issues with pictures in header/footer in former apache poi versions.

以下是使用 apache poi 4.0.1 的最少工作代码.建议始终使用最新的稳定版本.:

The following is the most minimal working code using apache poi 4.0.1. It is recommend always using the latest stable version.:

代码:

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();

 }
}

结果:

这篇关于word文档页眉添加图片问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆