Get Paragraph Bounds from Presentations in Java
Get Paragraph and Portion Coordinates in a TextFrame
Using Aspose.Slides for Java, developers can now get the rectangular coordinates for Paragraph inside paragraphs collection of TextFrame. It also allows you to get the coordinates of portion inside portion collection of a paragraph. In this topic, we are going to demonstrate with the help of an example that how to get the rectangular coordinates for paragraph along with position of portion inside a paragraph.
AutoShape shape = (AutoShape)pres.getSlides().get_Item(0).getShapes().get_Item(0);
TextFrame textFrame = (TextFrame)shape.getTextFrame();
for (IParagraph paragraph : textFrame.getParagraphs()){
for (IPortion portion : paragraph.getPortions()){
Point2D.Float point = portion.getCoordinates();
}
}
Get Rectangular Coordinates of a Paragraph
Using getRect() method developers can get paragraph bounds rectangle.
Presentation pres = new Presentation("HelloWorld.pptx");
try {
IAutoShape shape = (IAutoShape) pres.getSlides().get_Item(0).getShapes().get_Item(0);
ITextFrame textFrame = shape.getTextFrame();
Rectangle2D.Float rect = textFrame.getParagraphs().get_Item(0).getRect();
System.out.println("X: " + rect.x + " Y: " + rect.y + " Width: " + rect.width + " Height: " + rect.height);
} finally {
if (pres != null) pres.dispose();
}
Get the Size of a Paragraph and Portion Inside a Table Cell TextFrame
To get the Portion or Paragraph size and coordinates in a table cell text frame, you can use the IPortion.getRect and IParagraph.getRect methods.
This sample code demonstrates the described operation:
Presentation pres = new Presentation("source.pptx");
try {
Table tbl = (Table)pres.getSlides().get_Item(0).getShapes().get_Item(0);
ICell cell = tbl.getRows().get_Item(1).get_Item(1);
double x = tbl.getX() + tbl.getRows().get_Item(1).get_Item(1).getOffsetX();
double y = tbl.getY() + tbl.getRows().get_Item(1).get_Item(1).getOffsetY();
for (IParagraph para : cell.getTextFrame().getParagraphs())
{
if (para.getText().equals(""))
continue;
Rectangle2D.Float rect = para.getRect();
IAutoShape shape =
pres.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle,
(float)rect.getX() + (float)x, (float)rect.getY() + (float)y, (float)rect.getWidth(), (float)rect.getHeight());
shape.getFillFormat().setFillType(FillType.NoFill);
shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.YELLOW);
shape.getLineFormat().getFillFormat().setFillType(FillType.Solid);
for (IPortion portion : para.getPortions())
{
if (portion.getText().contains("0"))
{
rect = portion.getRect();
shape =
pres.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle,
(float)rect.getX() + (float)x, (float)rect.getY() + (float)y, (float)rect.getWidth(), (float)rect.getHeight());
shape.getFillFormat().setFillType(FillType.NoFill);
}
}
}
} finally {
if (pres != null) pres.dispose();
}
FAQ
In what units are the coordinates returned for a paragraph and text portions measured?
In points, where 1 inch = 72 points. This applies to all coordinates and dimensions on the slide.
Does word wrapping affect a paragraph’s bounds?
Yes. If wrapping is enabled in the TextFrame, the text breaks to fit the area width, which changes the paragraph’s actual bounds.
Can paragraph coordinates be reliably mapped to pixels in the exported image?
Yes. Convert points to pixels using: pixels = points × (DPI / 72). The result depends on the DPI chosen for rendering/export.
How do I get the “effective” paragraph formatting parameters, taking style inheritance into account?
Use the effective paragraph formatting data structure; it returns the final consolidated values for indents, spacing, wrapping, RTL, and more.