XSLFGroupShape does not encompass its child shapes

故事扮演 提交于 2021-02-17 03:34:33

问题


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...

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?


回答1:


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.

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();
 }
}


来源:https://stackoverflow.com/questions/44109882/xslfgroupshape-does-not-encompass-its-child-shapes

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