繁体   English   中英

Apache POI XSLF 从幻灯片上的文本中删除阴影

[英]Apache POI XSLF remove shadow from text on the slide

我得到了带有简单演示文稿的 pptx 文件。 它有背景图像,上面有白色文字,这个文字有阴影。 我需要简化演示并删除所有这些东西(将背景设置为白色,字体颜色设置为黑色并删除阴影)

更改背景和字体颜色非常简单,就像这样

        SlideShow ppt = SlideShowFactory.create(inputStream);
        List<Slide> slides= ppt.getSlides();
        for (int i = 0; i< slides.size(); i++) {
            Slide slide = slides.get(i);
            ((XSLFSlide)slide).getBackground().setFillColor(Color.white);
            XSLFTextShape[] shapes = ((XSLFSlide) slide).getPlaceholders();
            for (XSLFTextShape textShape : shapes) {
                List<XSLFTextParagraph> textparagraphs = textShape.getTextParagraphs();
                for (XSLFTextParagraph para : textparagraphs) {
                    List<XSLFTextRun> textruns = para.getTextRuns();
                    for (XSLFTextRun incomingTextRun : textruns) {
                        incomingTextRun.setFontColor(Color.black);
                }
            }

但我不知道如何去除阴影。 这是之前和之后的例子前 后

我试图在 TextShape 上调用getShadow()方法,但它为空,在XSLFTextRun中没有管理文本阴影的方法。 对于HSLF我看到有setShadowed()TextRun 但是如何处理 XSLF 中的阴影呢?

谢谢!

更新:

感谢 Axel Richter 提供非常有价值的答案。 在我的文档中,我发现了两个带有阴影文本的案例。

  1. 第一个是 Axel 描述的,解决方案是清除 CTRegularTextRun 中的阴影。 我还发现XSLFTextParagraph.getTextRuns()可能包含 LineBreak 对象,所以在转换XSLFTextRun.getXMLObject()之前 - 最好检查它是CTRegularTextRun的实例而不是CTTextLineBreak

代码:

private void clearShadowFromTextRun(XSLFTextRun run) {
    if (run.getXmlObject() instanceof CTRegularTextRun) {
        CTRegularTextRun cTRun = (CTRegularTextRun) run.getXmlObject();
        if (cTRun.getRPr() != null) {
            if (cTRun.getRPr().getEffectLst() != null) {
                if (cTRun.getRPr().getEffectLst().getOuterShdw() != null) {
                    cTRun.getRPr().getEffectLst().unsetOuterShdw();
                }
            }
        }
    }
}
  1. 第二种情况 - SlideMaster 包含一些正文和标题的样式定义。 因此,如果我们想完全消除所有阴影 - 我们也应该清除它们。

代码:

private void clearSlideMastersShadowStyles(XMLSlideShow ppt) {
    List<XSLFSlideMaster> slideMasters = ppt.getSlideMasters();
    for (XSLFSlideMaster slideMaster : slideMasters) {
        CTSlideMaster ctSlideMaster = slideMaster.getXmlObject();
        if (ctSlideMaster.getTxStyles() != null) {
            if (ctSlideMaster.getTxStyles().getTitleStyle() != null) {
                clearShadowsFromStyle(ctSlideMaster.getTxStyles().getTitleStyle());
            }
            if (ctSlideMaster.getTxStyles().getBodyStyle() != null) {
                clearShadowsFromStyle(ctSlideMaster.getTxStyles().getBodyStyle());
            }
            if (ctSlideMaster.getTxStyles().getOtherStyle() != null) {
                clearShadowsFromStyle(ctSlideMaster.getTxStyles().getOtherStyle());
            }
        }
    }
}

private void clearShadowsFromStyle(CTTextListStyle ctTextListStyle) {
        if (ctTextListStyle.getLvl1PPr() != null) {
            if (ctTextListStyle.getLvl1PPr().getDefRPr() != null)
                if (ctTextListStyle.getLvl1PPr().getDefRPr().getEffectLst() != null)
                    if (ctTextListStyle.getLvl1PPr().getDefRPr().getEffectLst().getOuterShdw() != null)
                        ctTextListStyle.getLvl1PPr().getDefRPr().getEffectLst().unsetOuterShdw();
        }
//same stuff for other 8 levels. Ugly uhh...
    }

XSLFTextRun尚未实现文本阴影的设置。 但是当然它们是在XML中设置的。

带有阴影文本的运行如下所示:

<a:r>
 <a:rPr lang="de-DE" smtClean="0" dirty="0" b="1">
  <a:effectLst>
   <a:outerShdw dir="2700000" algn="tl" dist="38100" blurRad="38100">
    <a:srgbClr val="000000">
     <a:alpha val="43137"/>
    </a:srgbClr>
   </a:outerShdw>
  </a:effectLst>
 </a:rPr>
 <a:t>The text...</a:t>
</a:r>

如您所见,有一个rPr (运行属性),其effectLst具有一个outerShdw元素。 我们可以使用ooxml-schemas类和方法来设置和取消设置。

...
      incomingTextRun.setFontColor(Color.black);

      org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun cTRun = (org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun)incomingTextRun.getXmlObject();
      if (cTRun.getRPr() != null) {
       if (cTRun.getRPr().getEffectLst() != null) {
        if (cTRun.getRPr().getEffectLst().getOuterShdw() != null) {
         cTRun.getRPr().getEffectLst().unsetOuterShdw();
        }
       }
      }
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM