
Java의 PowerPoint에서 텍스트 또는 도형 자동 맞춤
2022-10-19 last update
7 minutes reading java autofit powerpoint apiPowerPoint에서 미리 정의된 도형에 텍스트를 입력할 때 텍스트가 너무 길면 도형이 오버플로되어 문서 전체가 어수선해 보일 수 있습니다. 이 기사에서는 Free Spire.Presentation for Java를 사용하여 도형에 맞게 텍스트를 자동으로 축소하는 방법 또는 텍스트에 맞게 도형의 크기를 자동으로 조정하는 방법을 배웁니다.
● free library을 다운로드하여 압축을 푼 다음 Spire.Presentation.jar 파일을 프로젝트에 종속성으로 추가합니다.
● pom.xml에 다음 구성을 추가하여 maven 프로젝트에 jar 종속성을 직접 추가합니다.
Java용 무료 Spire.Presentation은 IAutoShape.getTextFrame().setAutofitType(TextAutofitType 값)을 제공하여 텍스트의 자동 맞춤 모드를 설정합니다. 값을 NORMAL로 설정하면 텍스트가 도형에 맞게 자동으로 축소되고, SHAPE로 설정하면 도형 크기가 텍스트에 맞게 자동으로 축소되거나 확장됩니다.
JAR 종속성 가져오기(2가지 방법)
● free library을 다운로드하여 압축을 푼 다음 Spire.Presentation.jar 파일을 프로젝트에 종속성으로 추가합니다.
● pom.xml에 다음 구성을 추가하여 maven 프로젝트에 jar 종속성을 직접 추가합니다.
<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>5.1.0</version>
</dependency>
</dependencies>
샘플 코드
Java용 무료 Spire.Presentation은 IAutoShape.getTextFrame().setAutofitType(TextAutofitType 값)을 제공하여 텍스트의 자동 맞춤 모드를 설정합니다. 값을 NORMAL로 설정하면 텍스트가 도형에 맞게 자동으로 축소되고, SHAPE로 설정하면 도형 크기가 텍스트에 맞게 자동으로 축소되거나 확장됩니다.
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("In 1928, Auden published his first book of verse, and his collection Poems, published in 1930, which established him as the leading voice of a new generation.");
//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("Resize shape to fit text.");
//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);
}
}
