Bug 55714

Summary: Background image ignored on slide copy
Product: POI Reporter: Benjamin Gamard <benjamin.gam>
Component: XSLFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 3.10-dev   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: Input file with a background image

Description Benjamin Gamard 2013-10-28 13:57:04 UTC
Created attachment 30982 [details]
Input file with a background image

Hi and thank you for this great library. I'm using 3.10-beta2

I try to copy some slides from a presentation to a new blank presentation, like this :

XMLSlideShow srcPptx = null;
try (InputStream is = Resources.getResource("test.pptx").openStream()) {
    srcPptx = new XMLSlideShow(is);
}

int slideIndex = 0;
for (XSLFSlide srcSlide : srcPptx.getSlides()) {
    XMLSlideShow newPptx = new XMLSlideShow();
    
    // Add slide
    XSLFSlide newSlide = newPptx.createSlide();
    XSLFSlideLayout srcSlideLayout = srcSlide.getSlideLayout();
    XSLFSlideMaster srcSlideMaster = srcSlide.getSlideMaster();
    XSLFSlideLayout newSlideLayout = newSlide.getSlideLayout();
    XSLFSlideMaster newSlideMaster = newSlide.getSlideMaster();
    newSlideLayout.importContent(srcSlideLayout);
    newSlideMaster.importContent(srcSlideMaster); 
    newSlide.importContent(srcSlide);
    
    // Write the new presentation
    try (OutputStream os = new FileOutputStream("slide-" + (slideIndex++) + ".pptx")) {
        newPptx.write(os);
    }
}

It seems to work great, except that background images are not copied.
I join you an input .pptx to reproduce.

Tell me if you need more details.
Comment 1 Nick Burch 2013-10-28 14:21:30 UTC
PPTX files are a zip of XML files.

Are you able to unzip the resulting file, and diff the slide and layout xml to see what's different between the slides that include the background and those who lost it?
Comment 2 Benjamin Gamard 2013-10-28 14:34:29 UTC
(In reply to Nick Burch from comment #1)
> PPTX files are a zip of XML files.
> 
> Are you able to unzip the resulting file, and diff the slide and layout xml
> to see what's different between the slides that include the background and
> those who lost it?

Yes, here it is.

The source pptx slide contains at the beginning :

...
<p:cSld>
<p:bg>
  <p:bgPr>
    <a:blipFill dpi="0" rotWithShape="1">
      <a:blip r:embed="rId2" cstate="print">
        <a:lum />
      </a:blip>
      <a:srcRect />
      <a:stretch>
        <a:fillRect />
      </a:stretch>
    </a:blipFill>
    <a:effectLst />
  </p:bgPr>
</p:bg>
<p:spTree>
...

And the resulting pptx does not contains the <p:bg> tag :

...
<p:cSld>
<p:spTree>
...

I tried to debug into the copy process, and it seems that it never pass in the XSLFBackground.copy method (but it does for all the other shapes).
Comment 3 Benjamin Gamard 2013-10-28 16:25:30 UTC
After further debugging, I think I have found where the problem is.

in XSLFSlide.importContent (line 228), we have :
XSLFBackground bgShape = getBackground();

instead of :
XSLFBackground bgShape = src.getBackground();

It seems to import correctly the background in the new pptx, but it's not enough. The <p:bgPr> is not copied (as I said in my previous comment). If I copy it manually like that :
newSlide.getXmlObject().getCSld().setBg(srcSlide.getXmlObject().getCSld().getBg());

The background works.
Comment 4 Nick Burch 2013-10-28 16:35:08 UTC
Looks promising!

Any chance you could write a short junit unit test that shows the problem? We can use that to test your proposed fix, as well as to ensure it stays fixed into the future!
Comment 5 Benjamin Gamard 2013-10-28 17:07:30 UTC
I was on the right path, however it does not seem to work if the background is carried by the slide master. Do you have any idea about it? Thanks.
Comment 6 Benjamin Gamard 2013-10-30 14:36:56 UTC
It seems that there is no way to add a new slide master or slide layout in POI for the moment, and the copy process keeps overlapping old ones.
So I am stuck here now.
Comment 7 Nick Burch 2013-11-07 22:06:56 UTC
(In reply to Benjamin Gamard from comment #6)
> It seems that there is no way to add a new slide master or slide layout in
> POI for the moment, and the copy process keeps overlapping old ones.
> So I am stuck here now.

It might not be that much extra work to implement new slide master and/or layout support for XSLF, so that might be the way to go!

(There may be some IDs that need fixing up, and you'll need to identify the smallest valid new master/layout for when creating the new one)
Comment 8 Andreas Beeker 2016-10-19 23:47:16 UTC
fixed via r1765733