How do I edit a PPTX file via Java while it is open in Microsoft PowerPoint?

穿精又带淫゛_ 提交于 2019-12-11 04:57:37

问题


With reference to POI - Cannot write to file while it is opened in Excel?

The required functionality for my Java application under development is the PowerPoint version of the above: Using Java, create a slide and add content generated from captured application data, then add the slide to an existing PowerPoint file that is currently opened in Microsoft PowerPoint.

If no PowerPoint file is opened, the Java application will first open Microsoft PowerPoint if necessary, then create a new, empty PowerPoint file and then open it in Microsoft PowerPoint.

For example, my application will create Test.pptx. The application will then open it in the Microsoft PowerPoint application. Next, I add the slide. The operation succeeds and no exceptions are thrown. However, the changes are not reflected in the Microsoft PowerPoint view.

  • If I close the file in Microsoft PowerPoint and open it again, the updated changes can be seen. However, this is not what I want. The user must be able to continuously add new slides and see these slides update into the Microsoft PowerPoint view on-the-fly without needing to restart the Microsoft PowerPoint application after each slide addition.
  • I have noticed that when the PowerPoint file is opened in Microsoft PowerPoint, a ~$Test.pptx temporary file is created on my Desktop. I tried to get my Java application to add the generated slides directly into ~Test.pptx but FileNotFoundException: the process cannot access the file because it is being used by another process. is subsequently thrown.

The application is implemented using Java 1.8, runs on Windows 10, and uses Microsoft Office 2013. Development on this particular feature has just begun from scratch so solutions are still being sought and explored.

At present, we are using Apache POI but we can switch away from using it if it means solving the problem. It was mooted to try using Microsoft Office macros. This idea is still being explored as we do not understand how to use macros enough to determine whether it adequately meets our requirements.

How can I implement the required functionality? What technologies or libraries would I need to get the job done?

Sample code taken from TutorialsPoint's Apache POI PPT tutorial. The existing PowerPoint file should already be opened in Microsoft PowerPoint, and when the below code is run, I need to see the added slides in the Microsoft PowerPoint view immediately. Reminder: Using Apache POI is not mandatory.

public class EditPresentation {
  public static void main(String ar[]) throws IOException{
    //opening an existing slide show
    File file = new File("example1.pptx");
    FileInputStream inputstream=new FileInputStream(file);
    XMLSlideShow ppt = new XMLSlideShow(inputstream);

    //adding slides to the slodeshow
    XSLFSlide slide1 = ppt.createSlide();
    XSLFSlide slide2 = ppt.createSlide();

    //saving the changes 
    FileOutputStream out = new FileOutputStream(file);
    ppt.write(out);

    System.out.println("Presentation edited successfully");
    out.close();    
  }
} 

来源:https://stackoverflow.com/questions/40524341/how-do-i-edit-a-pptx-file-via-java-while-it-is-open-in-microsoft-powerpoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!