Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Apache poi:无法在docx标头中添加图像_Java_Apache Poi - Fatal编程技术网

Java Apache poi:无法在docx标头中添加图像

Java Apache poi:无法在docx标头中添加图像,java,apache-poi,Java,Apache Poi,我想用ApachePOI在现有的docx文档中创建一个标题(我已经尝试了3.14和4.0.1版)。 但是当我打开docx时,在标题中我得到了这个(“我们无法显示此图像”): 我正在这样做: document = new XWPFDocument(OPCPackage.open("C:\\users\\thomas\\withoutHeader.docx")); CTSectPr sectPr1 = document.getDocument().getBody().addNewSectPr();

我想用ApachePOI在现有的docx文档中创建一个标题(我已经尝试了3.14和4.0.1版)。 但是当我打开docx时,在标题中我得到了这个(“我们无法显示此图像”):

我正在这样做:

document = new XWPFDocument(OPCPackage.open("C:\\users\\thomas\\withoutHeader.docx"));
CTSectPr sectPr1 = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr1);

//Header content
CTP ctpHeader = CTP.Factory.newInstance();
CTR ctrHeader = ctpHeader.addNewR();
CTText ctHeader = ctrHeader.addNewT();
String headerText = "This is header";
ctHeader.setStringValue(headerText);
XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);
XWPFParagraph[] parsHeader = new XWPFParagraph[1];
parsHeader[0] = headerParagraph;
policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);

//Header image
policy = new XWPFHeaderFooterPolicy(document);
XWPFHeader header = policy.getDefaultHeader();
System.out.println(header.getText());
XWPFParagraph paragraph = header.getParagraphArray(0);
if (paragraph == null) paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun r = paragraph.createRun();
FileInputStream in = new FileInputStream("C:\\Users\\thomas\\dev\\logo.png");
r.addPicture(in, Document.PICTURE_TYPE_JPEG, "C:\\Users\\thomas\\dev\\logo.png", Units.toEMU(100), Units.toEMU(50));
in.close();
FileOutputStream out = new FileOutputStream("C:\\users\\thomas\\withHeader.docx");
document.write(out);
document.close();
out.close();
我错过了什么


以下完整示例适用于我使用当前的
apache poi 4.1.1

该示例打开一个
*.docx
模板,该模板不应该已经有标题。然后,它添加一个默认的标题,其中包含文本和
logo.png

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(new FileInputStream("./Template.docx"));

  XWPFParagraph paragraph;
  XWPFRun run;

  // 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();
  run.setText("This is header "); 

  FileInputStream in = new FileInputStream("./logo.png");
  run.addPicture(in, Document.PICTURE_TYPE_PNG, "logo.png", Units.toEMU(100), Units.toEMU(50));
  in.close();  

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

 }
}

同样的代码也可以在使用ApachePOI3.17时工作。

ApachePOI
版本
3.17
4.x
应该可以工作。请看和。这两者都不起作用。但也许还是图书馆?我用我所有的poi库更新了我的帖子。我告诉过你ApachePOI版本3.17和4.x应该可以工作。但您使用的是3.14版。另外,我的两个答案都显示了完整的代码示例,其中没有一个需要使用底层的
CT*
类。