Java에서 프레젠테이션 플레이스홀더 관리
개요
플레이스홀더는 프레젠테이션 템플릿에서 특정 종류의 콘텐츠 위치를 예약하는 도형입니다. 일반적인 예로는 제목, 본문, 그림, 차트 및 일반 용도 콘텐츠 플레이스홀더가 있습니다. 일반 도형과 달리 플레이스홀더는 레이아웃 슬라이드 또는 마스터 슬라이드로부터 위치, 크기, 서식 및 기타 설정을 상속받을 수 있습니다.
Aspose.Slides는 플레이스홀더 정보를 IShape.getPlaceholder 메서드를 통해 제공합니다. 이 메서드는 일반 도형에 대해 null을 반환하거나 IPlaceholder 객체를 반환합니다. 플레이스홀더가 어떤 콘텐츠를 포함하도록 의도되었는지 확인하려면 IPlaceholder.getType을 사용하십시오.
플레이스홀더 유형을 알게 된 후에도 도형 인터페이스는 여전히 중요합니다:
- 비어 있는 텍스트, 그림, 차트 또는 콘텐츠 플레이스홀더는 일반적으로 IAutoShape로 표현됩니다.
- 채워진 그림 플레이스홀더는 IPictureFrame으로 표현될 수 있습니다.
- 채워진 차트 플레이스홀더는 IChart으로 표현될 수 있습니다.
- 콘텐츠 플레이스홀더는 여러 종류의 콘텐츠를 포함할 수 있습니다. 모든 플레이스홀더가 IAutoShape이라고 가정하는 대신 IPlaceholder.getType 및 런타임 도형 인터페이스를 모두 확인하십시오.
Warning
IPlaceholder.getType은 플레이스홀더의 역할을 설명하지만 도형의 런타임 유형을 보장하지 않습니다. 텍스트, 그림, 차트, 표 또는 미디어 관련 멤버에 접근하기 전에 항상 유형 검사를 수행하십시오.플레이스홀더 상속 이해
플레이스홀더는 계층 구조를 형성합니다:
- 마스터 슬라이드는 재사용 가능한 스타일을 정의하며, 경우에 따라 마스터 수준의 플레이스홀더를 정의합니다.
- 레이아웃 슬라이드는 하나 이상의 일반 슬라이드에서 사용되는 배치를 정의하고 마스터로부터 상속받을 수 있습니다.
- 일반 슬라이드는 해당 슬라이드의 플레이스홀더를 포함하며 레이아웃으로부터 상속받을 수 있습니다.
이 계층 구조에서 한 단계 위로 이동하려면 IShape.getBasePlaceholder를 호출하십시오. 슬라이드 플레이스홀더는 일반적으로 해당 레이아웃 플레이스홀더를 반환하며, 레이아웃 플레이스홀더는 마스터 플레이스홀더를 반환할 수 있습니다. 도형에 기반 플레이스홀더가 없을 경우 메서드는 null을 반환합니다.
다음 예제는 첫 번째 슬라이드의 플레이스홀더를 나열하고 해당 기반 플레이스홀더를 보고합니다:
import com.aspose.slides.*;
Presentation presentation = new Presentation("template.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
for (IShape shape : slide.getShapes()) {
IPlaceholder placeholder = shape.getPlaceholder();
if (placeholder == null) {
continue;
}
byte placeholderType = placeholder.getType();
String typeName = shape.getClass().getSimpleName();
String slidePlaceholderMessage = "Slide placeholder: " + placeholderType + "; shape interface: " + typeName;
System.out.println(slidePlaceholderMessage);
IShape layoutPlaceholder = shape.getBasePlaceholder();
if (layoutPlaceholder != null) {
IPlaceholder layoutPlaceholderInfo = layoutPlaceholder.getPlaceholder();
Byte layoutPlaceholderType = layoutPlaceholderInfo == null ? null : layoutPlaceholderInfo.getType();
String layoutPlaceholderMessage = " Layout placeholder: " + layoutPlaceholderType;
System.out.println(layoutPlaceholderMessage);
IShape masterPlaceholder = layoutPlaceholder.getBasePlaceholder();
if (masterPlaceholder != null) {
IPlaceholder masterPlaceholderInfo = masterPlaceholder.getPlaceholder();
Byte masterPlaceholderType = masterPlaceholderInfo == null ? null : masterPlaceholderInfo.getType();
String masterPlaceholderMessage = " Master placeholder: " + masterPlaceholderType;
System.out.println(masterPlaceholderMessage);
}
}
}
} finally {
presentation.dispose();
}
일반 슬라이드에서 플레이스홀더를 편집하면 해당 슬라이드에 대한 로컬 오버라이드가 생성되거나 변경됩니다. 관련 레이아웃이나 마스터를 편집하면 해당 설정을 여전히 상속하는 모든 슬라이드에 영향을 줄 수 있습니다. 로컬 일반 도형은 기반 플레이스홀더가 없으며 동일한 좌표에 있다고 해서 상속을 시작하지도 않습니다.
플레이스홀더의 텍스트 변경
제목, 중앙 제목, 부제목, 본문 및 텍스트 플레이스홀더는 일반적으로 텍스트를 지원합니다. 해당 IAutoShape인지 확인한 후 getTextFrame 메서드를 사용하십시오.
다음 예제는 첫 번째 슬라이드의 첫 번째 제목 플레이스홀더를 업데이트하고 결과를 저장합니다:
import com.aspose.slides.*;
Presentation presentation = new Presentation("template.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape titleShape = null;
for (IShape shape : slide.getShapes()) {
if (!(shape instanceof IAutoShape)) {
continue;
}
IAutoShape autoShape = (IAutoShape) shape;
IPlaceholder placeholder = autoShape.getPlaceholder();
if (placeholder == null) {
continue;
}
byte placeholderType = placeholder.getType();
if (placeholderType == PlaceholderType.Title || placeholderType == PlaceholderType.CenteredTitle) {
titleShape = autoShape;
break;
}
}
if (titleShape == null) {
throw new IllegalStateException("The first slide does not contain a title placeholder.");
}
titleShape.getTextFrame().setText("Quarterly Business Review");
presentation.save("title-placeholder-updated.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
이 패턴은 그림, 차트, 표 또는 미디어 플레이스홀더를 IAutoShape으로 캐스팅하는 것을 피합니다. 또한 취약한 도형 인덱스에 의존하는 대신 목적에 따라 플레이스홀더를 식별합니다.
레이아웃에 프롬프트 텍스트 설정
프롬프트 텍스트는 빈 플레이스홀더에 표시되는 디자인‑타임 지시문으로, 예를 들어 Click to add title와 같습니다. 일반 슬라이드의 도형 컬렉션을 통해 접근하려고 시도하기보다 레이아웃 플레이스홀더에 사용자 정의 프롬프트 텍스트를 설정하십시오. 레이아웃은 ISlide.getLayoutSlide를 통해 접근하고, ILayoutSlide.getShapes이 반환하는 컬렉션을 순회하십시오.
다음 예제는 첫 번째 슬라이드에 사용된 레이아웃의 제목 및 부제목 프롬프트를 변경합니다:
import com.aspose.slides.*;
Presentation presentation = new Presentation("template.pptx");
try {
ILayoutSlide layoutSlide = presentation.getSlides().get_Item(0).getLayoutSlide();
for (IShape shape : layoutSlide.getShapes()) {
if (!(shape instanceof IAutoShape)) {
continue;
}
IAutoShape autoShape = (IAutoShape) shape;
IPlaceholder placeholder = autoShape.getPlaceholder();
if (placeholder == null) {
continue;
}
byte placeholderType = placeholder.getType();
if (placeholderType == PlaceholderType.Title || placeholderType == PlaceholderType.CenteredTitle) {
autoShape.getTextFrame().setText("Enter a concise slide title");
} else if (placeholderType == PlaceholderType.Subtitle) {
autoShape.getTextFrame().setText("Enter a subtitle or reporting period");
}
}
presentation.save("custom-placeholder-prompts.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
프롬프트 텍스트는 일반 슬라이드 콘텐츠가 아닙니다. PowerPoint와 같은 편집 애플리케이션에서 빈 플레이스홀더에 표시되는 용도이며, 사용자가 실제 콘텐츠를 제공하면 더 이상 표시되지 않습니다. 프롬프트를 변경해도 해당 레이아웃을 사용하는 슬라이드의 기존 텍스트가 교체되지는 않습니다.
그림 플레이스홀더 업데이트
처리해야 할 경우가 두 가지 있습니다:
- 그림 플레이스홀더가 이미 채워져 있고 IPictureFrame로 표현되는 경우, IPictureFillFormat.getPicture와 ISlidesPicture.setImage를 사용해 이미지를 교체하십시오.
- 아직 빈 플레이스홀더인 경우, IShapeCollection.addPictureFrame를 이용해 플레이스홀더 좌표에 그림 프레임을 추가하고 빈 플레이스홀더를 제거하십시오.
다음 예제는 두 경우를 모두 지원하고 프레젠테이션을 저장합니다:
import com.aspose.slides.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Presentation presentation = new Presentation("picture-template.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IShape picturePlaceholder = null;
for (IShape shape : slide.getShapes()) {
IPlaceholder placeholder = shape.getPlaceholder();
if (placeholder != null && placeholder.getType() == PlaceholderType.Picture) {
picturePlaceholder = shape;
break;
}
}
if (picturePlaceholder == null) {
throw new IllegalStateException("The first slide does not contain a picture placeholder.");
}
Path imagePath = Paths.get("replacement.png");
byte[] imageBytes = Files.readAllBytes(imagePath);
IPPImage image = presentation.getImages().addImage(imageBytes);
if (picturePlaceholder instanceof IPictureFrame) {
IPictureFrame pictureFrame = (IPictureFrame) picturePlaceholder;
pictureFrame.getPictureFormat().getPicture().setImage(image);
} else {
slide.getShapes().addPictureFrame(ShapeType.Rectangle, picturePlaceholder.getX(), picturePlaceholder.getY(), picturePlaceholder.getWidth(), picturePlaceholder.getHeight(), image);
slide.getShapes().remove(picturePlaceholder);
}
presentation.save("picture-placeholder-updated.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
빈 플레이스홀더에 대해 생성된 교체물은 새로운 플레이스홀더가 아니라 로컬 그림 프레임입니다. 이는 IShape.getPlaceholder에 설정자가 없기 때문이며, 예약된 위치는 유지하지만 더 이상 플레이스홀더‑특정 동작을 상속하지 않습니다. 플레이스홀더 관계를 유지해야 하는 경우, 먼저 PowerPoint에서 플레이스홀더를 준비·채우고 이후 Aspose.Slides를 사용해 결과 IPictureFrame를 업데이트하십시오.
이미지 투명도, 자르기 및 기타 그림 전용 효과에 대해서는 Manage Picture Frames를 참조하십시오. 이러한 작업은 그림 프레임 또는 그림 채우기에 해당하며 플레이스홀더 메타데이터와는 무관합니다.
차트 및 콘텐츠 플레이스홀더 작업
채워진 차트 플레이스홀더는 IChart로 표현될 수 있습니다. 다음 예제는 플레이스홀더 유형과 런타임 인터페이스 모두를 사용해 차트를 찾아 제목을 변경하고 파일을 저장합니다:
import com.aspose.slides.*;
Presentation presentation = new Presentation("chart-template.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IChart placeholderChart = null;
for (IShape shape : slide.getShapes()) {
if (!(shape instanceof IChart)) {
continue;
}
IChart chart = (IChart) shape;
IPlaceholder placeholder = chart.getPlaceholder();
if (placeholder != null && placeholder.getType() == PlaceholderType.Chart) {
placeholderChart = chart;
break;
}
}
if (placeholderChart == null) {
throw new IllegalStateException("The first slide does not contain a populated chart placeholder.");
}
placeholderChart.setTitle(true);
placeholderChart.getChartTitle().addTextFrameForOverriding("Quarterly Revenue");
presentation.save("chart-placeholder-updated.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
일반 콘텐츠 플레이스홀더는 보통 PlaceholderType.Object를 가집니다. PowerPoint에서는 차트, 표, 다이어그램, 그림 및 미디어 등 여러 콘텐츠 유형을 시작할 수 있는 런처 역할을 합니다. 채워진 뒤에는 실제 도형 인터페이스를 검사하여 포함된 내용을 파악하십시오. 특수 레이아웃은 또한 PlaceholderType.Chart, PlaceholderType.Table, PlaceholderType.Picture, PlaceholderType.Media, 또는 PlaceholderType.Diagram을 노출할 수 있습니다.
Aspose.Slides는 IPlaceholder.getType을 변경한다고 해서 빈 IAutoShape 플레이스홀더를 IChart으로 변환하지 않습니다. 인터페이스를 통해 유형을 변경할 수 없습니다. 빈 차트 또는 콘텐츠 영역을 프로그래밍 방식으로 채우려면 해당 좌표에 필요한 객체를 추가한 뒤 빈 플레이스홀더를 제거하십시오. 다음 예제는 차트에 대해 이를 수행합니다:
import com.aspose.slides.*;
Presentation presentation = new Presentation("content-template.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IShape targetPlaceholder = null;
for (IShape shape : slide.getShapes()) {
IPlaceholder placeholder = shape.getPlaceholder();
if (placeholder == null) {
continue;
}
byte placeholderType = placeholder.getType();
if (placeholderType == PlaceholderType.Chart || placeholderType == PlaceholderType.Object) {
targetPlaceholder = shape;
break;
}
}
if (targetPlaceholder == null) {
throw new IllegalStateException("The first slide does not contain a chart or content placeholder.");
}
IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, targetPlaceholder.getX(), targetPlaceholder.getY(), targetPlaceholder.getWidth(), targetPlaceholder.getHeight());
chart.setTitle(true);
chart.getChartTitle().addTextFrameForOverriding("Quarterly Revenue");
slide.getShapes().remove(targetPlaceholder);
presentation.save("content-placeholder-replaced-with-chart.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
추가된 차트는 일반 로컬 차트이며, 플레이스홀더 영역을 차지하지만 레이아웃 플레이스홀더를 상속하지 않습니다. 범주, 시리즈 또는 워크북 데이터를 교체해야 할 때는 전용 chart management articles를 참고하십시오.
전체 예제: 텍스트 또는 이미지 콘텐츠 업데이트
다음 엔드‑투‑엔드 예제는 템플릿을 열고, 첫 번째 슬라이드에서 제목 또는 그림 플레이스홀더를 검색한 뒤, 플레이스홀더와 도형 유형을 확인하고, 적절한 콘텐츠를 업데이트한 다음 결과를 저장합니다. 예제는 도형 인덱스를 가정하거나 모든 플레이스홀더를 동일한 인터페이스로 캐스팅하는 것을 의도적으로 피합니다.
import com.aspose.slides.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Presentation presentation = new Presentation("template.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
boolean updated = false;
for (IShape shape : slide.getShapes()) {
IPlaceholder placeholder = shape.getPlaceholder();
if (placeholder == null) {
continue;
}
byte placeholderType = placeholder.getType();
if ((placeholderType == PlaceholderType.Title || placeholderType == PlaceholderType.CenteredTitle) && shape instanceof IAutoShape) {
IAutoShape titleShape = (IAutoShape) shape;
titleShape.getTextFrame().setText("Quarterly Business Review");
updated = true;
break;
}
if (placeholderType == PlaceholderType.Picture) {
Path imagePath = Paths.get("replacement.png");
byte[] imageBytes = Files.readAllBytes(imagePath);
IPPImage image = presentation.getImages().addImage(imageBytes);
if (shape instanceof IPictureFrame) {
IPictureFrame pictureFrame = (IPictureFrame) shape;
pictureFrame.getPictureFormat().getPicture().setImage(image);
} else {
slide.getShapes().addPictureFrame(ShapeType.Rectangle, shape.getX(), shape.getY(), shape.getWidth(), shape.getHeight(), image);
slide.getShapes().remove(shape);
}
updated = true;
break;
}
}
if (!updated) {
throw new IllegalStateException("No supported title or picture placeholder was found on the first slide.");
}
presentation.save("placeholder-content-updated.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
FAQ
기본 플레이스홀더란 무엇인가요?
기본 플레이스홀더는 레이아웃 또는 마스터에 존재하는 해당 도형으로, 다른 플레이스홀더가 이를 상속받습니다. 해당 플레이스홀더를 가져오려면 IShape.getBasePlaceholder를 사용하십시오. 일반 로컬 도형은 플에이스홀더 계층에 포함되지 않으므로 null을 반환합니다.
레이아웃 플레이스홀더를 편집하여 모든 슬라이드 제목을 변경할 수 있나요?
레이아웃을 통해 상속된 서식이나 프롬프트 텍스트는 변경할 수 있지만, 실제 제목 내용은 일반 슬라이드에 저장됩니다. 프레젠테이션 전체의 제목 텍스트를 교체하려면 슬라이드를 순회하여 각 제목 플레이스홀더를 업데이트해야 합니다.
날짜, 슬라이드 번호, 헤더 및 풋터 플레이스홀더를 어떻게 관리하나요?
해당 슬라이드, 레이아웃, 마스터, 노트 또는 핸드아웃 범위에서 헤더 및 풋터 관리자를 사용하십시오. 자세한 예제는 Manage Presentation Header and Footer를 참조하십시오.