简体   繁体   English

对于图像,Java中的Word Doc(.docx)中未发生追加

[英]For Image, appending is not happening in Word Doc(.docx) in Java

I have tried following code: 我尝试了以下代码:

   File f=new File("stackover.docx");
   XWPFDocument doc=new XWPFDocument(OPCPackage.open(new FileInputStream(f)));
  List<XWPFParagraph>  paragraphs = doc.getParagraphs();
  XWPFParagraph para =  paragraphs.get(paragraphs.size() - 1);
  org.apache.poi.xwpf.usermodel.XWPFRun run=para.createRun();
    XWPFTable table=doc.createTable();
    XWPFTableRow row=table.getRow(0);
    FileOutputStream fos=new FileOutputStream(f);
    File fileName=new File("stackOverImage.jpeg");
    FileInputStream fis=new FileInputStream(fileName);
    File fileName1=new File("stackOverImage2.jpeg");
    FileInputStream fis1=new FileInputStream(fileName1);

    run.addBreak();

    run.setText("Append The value please");
    run.addBreak();
    run.addPicture(fis, XWPFDocument.PICTURE_TYPE_JPEG, fileName.toString(),        Units.toEMU(320), Units.toEMU(210));
    fis.close();
    run.addBreak(BreakType.PAGE);
    run.setText("Append The value please 12");
    run.addBreak();
    run.addPicture(fis1, XWPFDocument.PICTURE_TYPE_JPEG, fileName1.toString(),  Units.toEMU(320), Units.toEMU(210));
    fis1.close();
    doc.write(fos);
    fos.flush();
    fos.close();

Note: The document is already saved & has header & footer. 注意:该文档已经保存并且具有页眉和页脚。

If I try to append an image in a document which doesn't have header & footer, the above code works great. 如果我尝试在没有页眉和页脚的文档中附加图像,则上述代码效果很好。 Although, size of file is increased after the execution, but while I try to open the file, it says We're sorry. 虽然,文件的大小在执行后会增加,但是当我尝试打开文件时,它表示对不起。 We can't open the file. 我们无法打开文件。 We found a problem with its content . 我们发现其内容存在问题 Can you please explain the reason why this happening & solution? 您能否解释一下这种情况的原因和解决方案? Thanks in advance. 提前致谢。

I am using following jars: 1. Poi 3.12 我正在使用以下罐子:1. Poi 3.12
2. Poi -ooxml-3.10.1 2. Poi -ooxml-3.10.1
3. Poi-scratchpad-3.15 3. Poi-scratchpad-3.15
4. Ooxml-schemas-1.1 4. Ooxml-schemas-1.1
5. Xmlbeans-2.3.0 5. Xmlbeans-2.3.0
6. Dom4j-1.1 6. Dom4j-1.1

Error is like below: 错误如下: 在此处输入图片说明 在此处输入图片说明

First problem is that your jars are all from different versions of POI. 第一个问题是您的罐子都来自不同版本的POI。

Before you do anything else, replace your jars with a single version of POI. 在执行其他任何操作之前,请先将POI替换为单个POI版本。 I see you mixing at least versions 3.10, 3.12, and 3.15. 我看到您至少混用了3.10、3.12和3.15版本。 The POI jars are not developed independently of one another, and mixing and matching does not work. POI罐并不是彼此独立开发的,并且混合和匹配不起作用。 Next, you do not need the scratchpad jar for XWPF documents. 接下来,您不需要XWPF文档的暂存罐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM