XSLFGroupShape 不包含其子形状 [英] XSLFGroupShape does not encompass its child shapes

查看:22
本文介绍了XSLFGroupShape 不包含其子形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Apache POI 3.16(撰写本文时的最新版本).在下面的代码片段中,我创建了一个 XSLFGroupShape,然后我用它来创建一堆子形状:

I'm using Apache POI 3.16 (the latest version at the time of writing). In the following snippet, I create a XSLFGroupShape which I then use to create a bunch of child shapes:

XSLFGroupShape group = slide.createGroup();

XSLFAutoShape cardRect = group.createAutoShape();
cardRect.setShapeType(ShapeType.RECT);
cardRect.setAnchor(rect);

XSLFPictureShape avatarShape = group.createPicture(avatar);

// More shapes added to the group here...

问题如下:在生成的 PowerPoint 文件中,组位置和尺寸似乎未初始化(我选择了内容像素化的矩形;整个矩形及其内容是单个 XSLFGroupShape;注意组的幻灯片左上角的操纵器):

The problem is the following: in the generated PowerPoint file, the group position and dimensions appear to be uninitialized (I've selected the rectangle whose content is pixelated; the whole rectangle and its content is a single XSLFGroupShape; notice the group's manipulator at the top-left corner of the slide):

我的代码中是否遗漏了什么?有没有办法规避或解决这个问题?

Am I missing anything in my code? Is there a way to circumvent or fix this problem?

推荐答案

GroupShape 需要一个 Anchor 和一个 InteriorAnchor.并且分组的形状必须适合 GroupShape.PowerPoint GUI 会在用户与组一起工作时自动进行管理.但是 apache poi 需要为此进行正确的设置,因为它只是将程序所说的内容写入文件.

The GroupShape needs an Anchorand an InteriorAnchor. And the grouped shapes must fit into the GroupShape. The PowerPoint GUI does managing that automatically while the user is working with groups. But apache poi needs correct settings for this since it simply writes into the file what the program says.

示例:宽度为 350、高度为 300、左侧为 100、顶部为 50 的组形状以及每个角的简单形状.

Example: A group shape in width 350, height 300, left 100, top 50 and a simple shape in each of it's corners.

import java.io.FileOutputStream;

import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.sl.usermodel.*;

import java.awt.Rectangle;
import java.awt.Color;

public class CreatePPTXGroupShape {

 public static void main(String[] args) throws Exception {

  SlideShow slideShow = new XMLSlideShow();

  Slide slide = slideShow.createSlide();

  int groupLeft = 100;
  int groupTop = 50;
  int groupWidth = 350;
  int groupHeight = 300;
  int groupPadding= 10;

  GroupShape group = slide.createGroup();
  group.setInteriorAnchor(new Rectangle(groupLeft, groupTop, groupWidth, groupHeight));
  group.setAnchor(new Rectangle(groupLeft+groupPadding, groupTop+groupPadding, groupWidth-groupPadding, groupHeight-groupPadding));

  AutoShape shape = group.createAutoShape();
  shape.setShapeType(ShapeType.RECT);
  shape.setFillColor(Color.GREEN);
  shape.setAnchor(new Rectangle(groupLeft, groupTop, 150, 100));

  shape = group.createAutoShape();
  shape.setShapeType(ShapeType.TRIANGLE);
  shape.setFillColor(Color.RED);
  shape.setAnchor(new Rectangle(groupLeft+groupWidth-120, groupTop, 120, 100));

  shape = group.createAutoShape();
  shape.setShapeType(ShapeType.DONUT);
  shape.setFillColor(Color.YELLOW);
  shape.setAnchor(new Rectangle(groupLeft, groupTop+groupHeight-90, 90, 90));

  shape = group.createAutoShape();
  shape.setShapeType(ShapeType.ELLIPSE);
  shape.setFillColor(Color.BLUE);
  shape.setAnchor(new Rectangle(groupLeft+groupWidth-100, groupTop+groupHeight-100, 100, 100));

  FileOutputStream out = new FileOutputStream("CreatePPTXGroupShape.pptx");
  slideShow.write(out);
  out.close();
 }
}

这篇关于XSLFGroupShape 不包含其子形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆