How do I edit the presProps.xml file with ApachePoi

我的梦境 提交于 2019-12-24 10:35:43

问题


I need to have a powerpoint loop until the user hits the escape key. Through saving and unzipping the file after checking the "Loop continuously until 'ESC'" option in the powerpoint's Slideshow Setup options I have a diff of the file that changes(ppt/presProps.xml)

Not fixed:

<p:presentationPr xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
   <p:showPr showNarration="1">
      <p:present />
      <p:sldAll />
      <p:penClr>
         <a:prstClr val="red" />
      </p:penClr>
      <p:extLst>
         <p:ext uri="{EC167BDD-8182-4AB7-AECC-EB403E3ABB37}">
            <p14:laserClr xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main">
               <a:srgbClr val="FF0000" />
            </p14:laserClr>
         </p:ext>
         <p:ext uri="{2FDB2607-1784-4EEB-B798-7EB5836EED8A}">
            <p14:showMediaCtrls xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main" val="1" />
         </p:ext>
      </p:extLst>
   </p:showPr>
   <p:extLst>
      <p:ext uri="{E76CE94A-603C-4142-B9EB-6D1370010A27}">
         <p14:discardImageEditData xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main" val="0" />
      </p:ext>
      <p:ext uri="{D31A062A-798A-4329-ABDD-BBA856620510}">
         <p14:defaultImageDpi xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main" val="220" />
      </p:ext>
      <p:ext uri="{FD5EFAAD-0ECE-453E-9831-46B23BE46B34}">
         <p15:chartTrackingRefBased xmlns:p15="http://schemas.microsoft.com/office/powerpoint/2012/main" val="0" />
      </p:ext>
   </p:extLst>
</p:presentationPr>

Fixed:

<p:presentationPr xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
   <p:showPr loop="1" showNarration="1">
      <p:present />
      <p:sldAll />
      <p:penClr>
         <a:prstClr val="red" />
      </p:penClr>
      <p:extLst>
         <p:ext uri="{EC167BDD-8182-4AB7-AECC-EB403E3ABB37}">
            <p14:laserClr xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main">
               <a:srgbClr val="FF0000" />
            </p14:laserClr>
         </p:ext>
         <p:ext uri="{2FDB2607-1784-4EEB-B798-7EB5836EED8A}">
            <p14:showMediaCtrls xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main" val="1" />
         </p:ext>
      </p:extLst>
   </p:showPr>
   <p:extLst>
      <p:ext uri="{E76CE94A-603C-4142-B9EB-6D1370010A27}">
         <p14:discardImageEditData xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main" val="0" />
      </p:ext>
      <p:ext uri="{D31A062A-798A-4329-ABDD-BBA856620510}">
         <p14:defaultImageDpi xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main" val="220" />
      </p:ext>
      <p:ext uri="{FD5EFAAD-0ECE-453E-9831-46B23BE46B34}">
         <p15:chartTrackingRefBased xmlns:p15="http://schemas.microsoft.com/office/powerpoint/2012/main" val="0" />
      </p:ext>
   </p:extLst>
</p:presentationPr>

Element diff:

3c3
<    <p:showPr showNarration="1">
---
>    <p:showPr loop="1" showNarration="1">

It looks like it's on a p element within the presentation element but I can't figure out how to set that attribute within POI.


回答1:


This is just a quick hack to show the modifications of package parts, which are not accessible via the API. As XmlBeans sets the loop attributes as "true" instead of "1", it might be necessary to set it via the XmlCursor API.

public static void main(String[] args) throws Exception {
    OPCPackage opc = OPCPackage.open("headers.pptx", PackageAccess.READ_WRITE);
    try (XMLSlideShow ppt = new XMLSlideShow(opc)) {
        PackagePart presProps = ppt.getPackage().getPart(PackagingURIHelper.createPartName("/ppt/presProps.xml"));
        PresentationPrDocument doc = PresentationPrDocument.Factory.parse(presProps.getInputStream());
        CTPresentationProperties pr = doc.getPresentationPr();
        CTShowProperties showPr = pr.isSetShowPr() ? pr.getShowPr() : pr.addNewShowPr();
        showPr.setLoop(true);
        presProps.clear();
        try (OutputStream os = presProps.getOutputStream()) {
            doc.save(os);
        }
    }
}


来源:https://stackoverflow.com/questions/49240059/how-do-i-edit-the-presprops-xml-file-with-apachepoi

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