如何使用 poi word 在段落运行中添加图像?

问题描述 投票:0回答:3

大家!我正在 poi word 中导出单词,我想在标题中添加图像,但失败了。堆栈溢出中有很多关于这个问题的问题,但我找不到任何答案。我使用 poi-3.15.jar,下面是我的代码。有人可以给我一些建议吗?添加文字成功,但图片失败。

        XWPFDocument document = new XWPFDocument();
        XWPFParagraph paragraph = document.createParagraph();
        paragraph.createRun().setText("hhhhh");
        InputStream a = new FileInputStream("2.png");
        paragraph.createRun().addPicture(a, Document.PICTURE_TYPE_PNG, "2.png", 20, 20);
        saveDocument(document2, "D:\\4.docx");
apache-poi
3个回答
1
投票

XWPF
页眉/页脚中输入图片存在问题,直到
apache poi
版本3.15。请参阅使用 POI XWPF 将图像添加到 word .docx 文档标题中

但是在

apache poi
版本 3.16 Beta 2 中,它似乎已修复,因为以下代码使用
apache poi
版本 3.16 Beta 2 工作:

import java.io.*;

import org.apache.poi.xwpf.usermodel.*;

import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;

import org.apache.poi.util.Units;

public class CreateWordHeaderFooter {

 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-footer
  XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
  if (headerFooterPolicy == null) headerFooterPolicy = doc.createHeaderFooterPolicy();

  // create header start
  XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);

  paragraph = header.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.LEFT);

  run = paragraph.createRun();  
  run.setText("The Header:");

  InputStream a = new FileInputStream("file_icon.png");
  paragraph.createRun().addPicture(a, Document.PICTURE_TYPE_PNG, "file_icon.png", Units.toEMU(20), Units.toEMU(20));
  a.close();

  // create footer start
  XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);

  paragraph = footer.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.CENTER);

  run = paragraph.createRun();  
  run.setText("The Footer: ");

  doc.write(new FileOutputStream("CreateWordHeaderFooter.docx"));
  doc.close();

 }
}

0
投票

这里是使用XWPF run方法添加图像的代码。

import org.apache.poi.xwpf.usermodel.XWPFRun;                               
                                                                               
String imgFile = "C:\\poi-3.9\\pictures\\Picture1.jpeg";                       
XWPFParagraph p = document.createParagraph();                                  
XWPFRun r = p.createRun();                                                     
int format = XWPFDocument.PICTURE_TYPE_JPEG;                                   
try {                                                                          
    r.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(400),
            Units.toEMU(100)); // 200x200 pixels                               
} catch (Exception e){                                                         
    System.out.println (e.getMessage());                                       
}

0
投票

以上代码都不适合我

© www.soinside.com 2019 - 2024. All rights reserved.