You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by bu...@apache.org on 2021/07/29 08:37:05 UTC

[Bug 65473] New: when do copy slide operation, text shape's copy is not working, new slide use the old text shape object's reference

https://bz.apache.org/bugzilla/show_bug.cgi?id=65473

            Bug ID: 65473
           Summary: when do copy slide operation, text shape's copy is not
                    working, new slide use the old text shape object's
                    reference
           Product: POI
           Version: 5.0.0-FINAL
          Hardware: PC
            Status: NEW
          Severity: normal
          Priority: P2
         Component: XSLF
          Assignee: dev@poi.apache.org
          Reporter: xdrodger@outlook.com
  Target Milestone: ---

Created attachment 37971
  --> https://bz.apache.org/bugzilla/attachment.cgi?id=37971&action=edit
input pptx file

When do copy slide operation, text shape's copy is not working, new slide use
the old text shape object's reference. Below is the source code, attachment is
input file. Only one attachment is allowed, so no result file is uploaded.


import org.apache.poi.xslf.usermodel.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class CopySlideTest {

    private XMLSlideShow ppt;
    private String dirPath = "D:\\workspace\\practice-demo\\file\\input\\";
    private String fileName = "copy-slide-demo.pptx";
    private String outFileName = "copy-slide-demo-out.pptx";

    @Before
    public void setUp() throws Exception {
        String filePath = dirPath + fileName;
        ppt = new XMLSlideShow(new FileInputStream(filePath));
    }

    @Test
    public void testCopySlide() {
        String shapeName = "title";
        XSLFSlide defaultSlide = getSlideByShapeName(shapeName);
        int slideIndex = defaultSlide.getSlideNumber() - 1;
        List<Integer> slideIndexList = new ArrayList<>();
        for (int i =0; i < 3; i ++) {
            if (i == 0) {
                // pass
            } else {
                XSLFSlide newSlide = copySlide(slideIndex);
                slideIndex = newSlide.getSlideNumber() - 1;
            }
            slideIndexList.add(slideIndex);
        }
        for (Integer index : slideIndexList) {
            XSLFSlide slide = ppt.getSlides().get(index);
            replaceText(slide, shapeName, "this is " + slide.getSlideNumber() +
" slide.");
        }
    }

    @After
    public void savePpt() throws Exception {
        String filePath = dirPath + outFileName;
        FileOutputStream out = new FileOutputStream(filePath);
        ppt.write(out);
        out.close();
        ppt.close();
    }

    private void replaceText(XSLFSlide slide, String shapeName, String value) {
        XSLFShape shape = getShape(slide, shapeName);
        if (shape == null) {
            return;
        }
        XSLFTextShape textShape = (XSLFTextShape) shape;
        List<XSLFTextParagraph> textParagraphs = textShape.getTextParagraphs();
        for (XSLFTextParagraph textParagraph : textParagraphs) {
            List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
            for (XSLFTextRun textRun : textRuns) {
                textRun.setText(value);
            }
        }
    }

    private XSLFSlide copySlide(int index) {
        XSLFSlideLayout defaultSlideLayout = null;
        List<XSLFSlideMaster> slideMasters = ppt.getSlideMasters();
        for (XSLFSlideMaster slideMaster : slideMasters) {
            for (XSLFSlideLayout slideLayout : slideMaster.getSlideLayouts()) {
                if (Objects.equals(SlideLayout.TITLE_AND_CONTENT,
slideLayout.getType())) {
                    defaultSlideLayout = slideLayout;
                    break;
                }
            }
        }
        XSLFSlide slide = ppt.getSlides().get(index);
        XSLFSlide newSlide =
ppt.createSlide(defaultSlideLayout).importContent(slide);
        ppt.setSlideOrder(newSlide, slide.getSlideNumber());
        return newSlide;
    }

    private XSLFSlide getSlideByShapeName(String shapeName) {
        List<XSLFSlide> slides = ppt.getSlides();
        for (XSLFSlide slide : slides) {
            List<XSLFShape> shapes = slide.getShapes();
            for (XSLFShape shape : shapes) {
                if (shape.getShapeName().equals(shapeName)) {
                    return slide;
                }
            }
        }
        throw new InvalidParameterException("shape not exist");
    }

    public XSLFShape getShape(XSLFSlide slide, String shapeName) {
        List<XSLFShape> shapes = slide.getShapes();
        for (XSLFShape shape : shapes) {
            if (shape.getShapeName().equals(shapeName)) {
                return shape;
            }
        }
        throw new InvalidParameterException("shape not exist in slide");
    }
}

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 65473] when do copy slide operation, text shape's copy is not working, new slide use the old text shape object's reference

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=65473

--- Comment #3 from PJ Fanning <fa...@yahoo.com> ---
See https://github.com/apache/poi/pull/371

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 65473] when do copy slide operation, text shape's copy is not working, new slide use the old text shape object's reference

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=65473

xdrodger@outlook.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 OS|                            |All
           Severity|normal                      |critical
           Priority|P2                          |P1

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 65473] when do copy slide operation, text shape's copy is not working, new slide use the old text shape object's reference

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=65473

--- Comment #1 from xdrodger@outlook.com ---
Created attachment 37972
  --> https://bz.apache.org/bugzilla/attachment.cgi?id=37972&action=edit
output pptx file

output pptx file

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 65473] when do copy slide operation, text shape's copy is not working, new slide use the old text shape object's reference

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=65473

--- Comment #2 from xdrodger@outlook.com ---
Created attachment 37973
  --> https://bz.apache.org/bugzilla/attachment.cgi?id=37973&action=edit
source code

source code

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 65473] when do copy slide operation, text shape's copy is not working, new slide use the old text shape object's reference

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=65473

PJ Fanning <fa...@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|critical                    |major

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 65473] when do copy slide operation, text shape's copy is not working, new slide use the old text shape object's reference

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=65473

--- Comment #4 from PJ Fanning <fa...@yahoo.com> ---
Github PR 371 is merged if anyone wants to build POI for themselves to try it
out.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 65473] when do copy slide operation, text shape's copy is not working, new slide use the old text shape object's reference

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=65473

PJ Fanning <fa...@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|NEW                         |RESOLVED

--- Comment #5 from PJ Fanning <fa...@yahoo.com> ---
added r1903574

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org