Reputation: 1546
I have an application which creates a power point presentation with Apache POI. I have some text boxes, and I need to set the left and right margin of this text boxes to 0. Hypothetically it would be just these two lines:
xslfTextParagraph.setLeftMargin(0.0d);
xslfTextParagraph.setRightMargin(0.0d);
However this does not seem to work for me.
Relevant code snippet:
TextBox<XSLFShape, XSLFTextParagraph> textShape = shapeGroup.createTextBox();
Rectangle rect = new Rectangle(x, y, width, height);
textShape.setAnchor(rect.getBounds2D());
XSLFTextParagraph xslfTextParagraph = textShape.getTextParagraphs().get(0);
xslfTextParagraph.setLeftMargin(0.0); // <- This does not change the margin of the textbox
xslfTextParagraph.setRightMargin(0.0); // <- This does not change the margin of the textbox
XSLFTextRun r = xslfTextParagraph.addNewTextRun();
r.setText("Some text");
r.setFontColor(new Color(0,0,0));
r.setFontSize(14.0);
r.setBold(true);
Java version: 1.8, Apache POI version: 5.0.0
Do you have any suggestion how could I set the margins to zero?
Upvotes: 0
Views: 495
Reputation: 61945
I suspect you want setting the body properties of the text body. Those have top-, bottom-, left- and right-inset properties.
But to get those, a XSLFTextShape
is needed instead of the TextBox<XSLFShape, XSLFTextParagraph>
interface.
...
XSLFTextShape textShape = (XSLFTextShape)shapeGroup.createTextBox();
Rectangle rect = new Rectangle(x, y, width, height);
textShape.setAnchor(rect);
XDDFTextBody textBody = textShape.getTextBody();
XDDFBodyProperties bodyProperties = textBody.getBodyProperties();
bodyProperties.setTopInset(0d);
bodyProperties.setBottomInset(0d);
bodyProperties.setLeftInset(0d);
bodyProperties.setRightInset(0d);
...
Complete example:
import java.io.FileOutputStream;
import org.apache.poi.sl.usermodel.*;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.xddf.usermodel.text.*;
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 = 200;
int groupHeight = 100;
int groupPadding= 10;
GroupShape shapeGroup = slide.createGroup();
shapeGroup.setInteriorAnchor(new Rectangle(groupLeft, groupTop, groupWidth, groupHeight));
shapeGroup.setAnchor(new Rectangle(groupLeft+groupPadding, groupTop+groupPadding, groupWidth-groupPadding, groupHeight-groupPadding));
int x = groupLeft+20;
int y = groupTop+20;
int width = 100;
int height = 20;
XSLFTextShape textShape = (XSLFTextShape)shapeGroup.createTextBox();
Rectangle rect = new Rectangle(x, y, width, height);
textShape.setAnchor(rect);
XDDFTextBody textBody = textShape.getTextBody();
XDDFBodyProperties bodyProperties = textBody.getBodyProperties();
bodyProperties.setTopInset(0d);
bodyProperties.setBottomInset(0d);
bodyProperties.setLeftInset(0d);
bodyProperties.setRightInset(0d);
XSLFTextParagraph xslfTextParagraph = textShape.getTextParagraphs().get(0);
XSLFTextRun r = xslfTextParagraph.addNewTextRun();
r.setText("Some text");
r.setFontColor(new Color(0,0,0));
r.setFontSize(14.0);
r.setBold(true);
FileOutputStream out = new FileOutputStream("CreatePPTXGroupShape.pptx");
slideShow.write(out);
out.close();
}
}
Upvotes: 1