使用Apache POI添加图像时,Word Doc已损坏

时间:2017-12-22 04:50:07

标签: java xml-parsing apache-poi docx

我正在尝试使用Apache POI在word文档中保存屏幕截图,但是当我尝试打开该word文档时,我收到XML解析错误

XWPFDocument docx = new XWPFDocument();
    XWPFParagraph par = docx.createParagraph();
    XWPFRun run = par.createRun();
   run.setText("Hello, World. This is my first java generated docx-file. Have fun.");        run.setFontSize(13);
    System.setProperty("webdriver.chrome.driver", "F:\\Jar files\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
   driver.get("http://www.google.com");
   driver.manage().window().maximize();

   TakesScreenshot scrShot =((TakesScreenshot)driver);

   //Call getScreenshotAs method to create image file

           File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);

       //Move image file to new destination

           File DestFile=new File("F:\\Jar files\\Prin.png");

           //Copy file at destination

           FileUtils.copyFile(SrcFile, DestFile);
           Thread.sleep(2000);
           driver.close();
    InputStream pic = new FileInputStream("F:\\Jar files\\Prin.png");
    run.addPicture(pic, Document.PICTURE_TYPE_PNG, "3", 0, 0);
    FileOutputStream out = new FileOutputStream("F:\\Jar files\\OG2.docx"); 
    System.out.println("Success");
    docx.write(out); 
    out.close(); 
    pic.close();

Word文档屏幕截图: XML Parsing Error Scrrenshot

XML Parsing Error

1 个答案:

答案 0 :(得分:0)

从3.8工作中将poi-ooxml更新为最新版(3.17)。