Among free third-party libraries I used, Free Spire.Presentation for Java supports automatically shrinking text to fit a shape or resizing a shape to fit text by setting AutofitType as NORMAL or SHAPE. Today I’ll introduce how to do so using Java codes.
DEPENDENCY
First of all, we need to add a
Jar file called Spire.Presentation.jar to IDEA. There are two methods to do it.
Method
One: Download the package of the free
library from the link, find Spire.Presentation.jar in the “lib” folder and then
manually add it to IDEA.
Method
Two: Create a Maven project in IDEA, type
the following codes in the pom.xml file and finally click the button “import
changes”.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
USING THE CODE
import com.spire.presentation.*;
import java.awt.geom.Rectangle2D;
public class AutoFitTextOrShape {
public static void main(String[] args) throws Exception {
//create a Presentation instance
Presentation presentation = new Presentation();
//get the first slide
ISlide slide = presentation.getSlides().get(0);
//add a shape to slide
IAutoShape textShape1 = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(50,50,200,80));
//add text to shape
textShape1.getTextFrame().setText("Your nose,through thick and thin,remains between your eyes and chin,not pasted on some other place,be glad your nose is on your face!");
//set the auto-fit type to normal, which means the text automatically shrinks to fit the shape when text overflows the shape
textShape1.getTextFrame().setAutofitType(TextAutofitType.NORMAL);
//add another shape to slide
IAutoShape textShape2 = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(350, 50, 200, 80));
textShape2.getTextFrame().setText("Be glad your nose in on your face");
//set the auto-fit type to shape, which means the shape size automatically decreases or increases to fit text
textShape2.getTextFrame().setAutofitType(TextAutofitType.SHAPE);
//save to file
presentation.saveToFile("output/AutoFit.pptx", FileFormat.PPTX_2013);
}
}Output
No comments:
Post a Comment