Java에서 프레젠테이션 슬라이드 마스터 관리

개요

슬라이드 마스터는 슬라이드 그룹에 대한 공유 디자인 설정을 정의합니다. 일반적인 도형, 로고, 배경, 텍스트 스타일, 테마 설정 및 바닥글 설정을 포함할 수 있습니다. PowerPoint에서 슬라이드 마스터를 편집하는 것이 모든 슬라이드에 동일한 서식을 반복 적용하지 않고 프레젠테이션을 일관되게 유지하는 일반적인 방법입니다.

Aspose.Slides for Java는 동일한 모델을 지원합니다. 프레젠테이션에는 하나 이상의 마스터 슬라이드가 포함될 수 있으며, 각 마스터 슬라이드에는 여러 레이아웃 슬라이드가 포함될 수 있습니다. 일반 슬라이드는 일반적으로 마스터 슬라이드를 직접 참조하지 않습니다. 대신 일반 슬라이드는 레이아웃 슬라이드를 사용하고, 그 레이아웃 슬라이드는 마스터 슬라이드에 속합니다.

계층 구조는 다음과 같습니다:

  1. 슬라이드 마스터 – 공유 디자인 및 테마를 정의합니다.
  2. 레이아웃 슬라이드 – 자리 표시자와 레이아웃 수준 서식의 특정 배치를 정의합니다.
  3. 일반 슬라이드 – 실제 프레젠테이션 내용을 포함하고 하나의 레이아웃 슬라이드를 사용합니다.

마스터 슬라이드, 레이아웃 슬라이드 및 일반 슬라이드의 계층 구조

Aspose.Slides에서 슬라이드 마스터는 IMasterSlide 인터페이스로 표시됩니다. 프레젠테이션의 모든 마스터 슬라이드는 Presentation.getMasters 컬렉션을 통해 사용할 수 있으며, 이 컬렉션은 IMasterSlideCollection을 구현합니다.

슬라이드 마스터 액세스

PowerPoint에서는 보기 > 슬라이드 마스터에서 슬라이드 마스터 보기를 열 수 있습니다.

PowerPoint 보기 탭의 슬라이드 마스터 명령

Aspose.Slides에서는 getMasters() 컬렉션을 사용하여 마스터 슬라이드에 액세스합니다:

import com.aspose.slides.*;

Presentation presentation = new Presentation("presentation.pptx");
try {
    IMasterSlide firstMasterSlide = presentation.getMasters().get_Item(0);
    int masterSlideCount = presentation.getMasters().size();
    int firstMasterLayoutSlideCount = firstMasterSlide.getLayoutSlides().size();

    System.out.println("Master slides: " + masterSlideCount);
    System.out.println("Layouts in the first master: " + firstMasterLayoutSlideCount);
} finally {
    presentation.dispose();
}

일반 슬라이드의 레이아웃을 통해 사용 중인 마스터 슬라이드를 가져올 수도 있습니다:

import com.aspose.slides.*;

Presentation presentation = new Presentation("presentation.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    ILayoutSlide layoutSlide = slide.getLayoutSlide();
    IMasterSlide masterSlide = layoutSlide.getMasterSlide();
    String masterSlideName = masterSlide.getName();

    System.out.println(masterSlideName);
} finally {
    presentation.dispose();
}

슬라이드 마스터에 포함되는 내용

마스터 슬라이드는 슬라이드와 유사한 객체입니다. IBaseSlide을 구현하므로 일반 및 레이아웃 슬라이드와 동일한 많은 슬라이드 속성을 노출합니다. 마스터 전용 멤버는 IMasterSlide API 페이지에 나열되어 있습니다.

일반적으로 사용되는 마스터 슬라이드 멤버는 다음과 같습니다:

멤버 목적
getBackground() 마스터 수준 슬라이드 배경을 설정합니다.
getShapes() 로고, 사진 프레임 및 공유 텍스트와 같이 마스터에 배치된 도형을 저장합니다.
getLayoutSlides() 마스터에 속한 레이아웃 슬라이드를 저장합니다.
getThemeManager() 마스터 테마 API에 대한 액세스를 제공합니다.
getHeaderFooterManager() 마스터 및 해당 하위 레이아웃의 머리글, 바닥글, 날짜 및 슬라이드 번호를 제어합니다.
getDependingSlides() 레이아웃을 통해 마스터에 종속된 일반 슬라이드를 반환합니다.

슬라이드 마스터에 이미지 추가

마스터 슬라이드에 이미지를 추가하면 해당 마스터의 레이아웃을 사용하는 슬라이드에 표시됩니다. 로고, 워터마크, 장식 밴드 및 기타 반복 시각 요소에 유용합니다.

다음 예제는 첫 번째 마스터 슬라이드에 로고를 추가합니다:

import com.aspose.slides.*;

Presentation presentation = new Presentation("presentation.pptx");
try {
    IMasterSlide masterSlide = presentation.getMasters().get_Item(0);
    IImage logo = Images.fromFile("logo.png");

    try {
        IPPImage logoImage = presentation.getImages().addImage(logo);

        masterSlide.getShapes().addPictureFrame(
                ShapeType.Rectangle,
                20,
                20,
                80,
                80,
                logoImage);
    } finally {
        logo.dispose();
    }

    presentation.save("presentation-with-logo.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

그림 프레임에 대한 자세한 내용은 Picture Frame를 참조하십시오.

마스터 그래픽 가시성 제어

IBaseSlide.setShowMasterShapes를 사용하면 마스터에서 상속된 그래픽(예: 로고나 장식 도형)을 삭제하지 않고 숨길 수 있습니다. 해당 그래픽을 제외해야 하는 슬라이드에서는 Slide.setShowMasterShapes에 false를 전달하고, 표시해야 하는 슬라이드에서는 true를 유지합니다.

다음 독립 실행형 예제는 마스터에 파란색 장식 밴드를 만들고 동일한 빈 레이아웃을 사용하는 두 슬라이드에 적용합니다. 첫 번째 슬라이드에서는 밴드가 보이고 두 번째 슬라이드에서는 숨깁니다. 입력 프레젠테이션이나 이미지가 필요하지 않습니다.

import com.aspose.slides.*;
import java.awt.Color;

Presentation presentation = new Presentation();
try {
    IMasterSlide masterSlide = presentation.getMasters().get_Item(0);
    ILayoutSlide layoutSlide = masterSlide.getLayoutSlides().getByType(SlideLayoutType.Blank);
    layoutSlide.setShowMasterShapes(true);

    float slideHeight = (float) presentation.getSlideSize().getSize().getHeight();
    IAutoShape band = masterSlide.getShapes().addAutoShape(ShapeType.Rectangle, 0, 0, 60, slideHeight);
    Color bandColor = new Color(70, 130, 180);
    band.getFillFormat().setFillType(FillType.Solid);
    band.getFillFormat().getSolidFillColor().setColor(bandColor);
    band.getLineFormat().getFillFormat().setFillType(FillType.NoFill);

    ISlide visibleSlide = presentation.getSlides().get_Item(0);
    visibleSlide.setLayoutSlide(layoutSlide);
    visibleSlide.getShapes().clear();

    ISlide hiddenSlide = presentation.getSlides().addEmptySlide(layoutSlide);

    visibleSlide.setShowMasterShapes(true);
    hiddenSlide.setShowMasterShapes(false);

    presentation.save("master-graphics.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

예제는 새 프레젠테이션에 제공되는 Blank 레이아웃을 사용하고 초기 슬라이드의 자체 자리 표시자를 제거합니다.

설정 범위 선택

일반 슬라이드는 ISlide.getLayoutSlide와 ILayoutSlide.getMasterSlide를 통해 마스터에 연결됩니다. 개별 슬라이드에 속성을 설정하면 해당 슬라이드에만 영향을 줍니다. [LayoutSlide.setShowMasterShapes](https://reference.aspose.com/slides/ko/java/com.aspose.slides/layoutslide/#setShowMasterShapes-boolean-)에 false를 전달하면 해당 공유 레이아웃을 사용하는 모든 슬라이드에서 마스터 그래픽이 숨겨지며, 개별 슬라이드 설정이 true이더라도 마찬가지입니다. 하나의 슬라이드만 숨기려면 해당 슬라이드 속성을 변경하고 공유 레이아웃은 그대로 둡니다.

마스터 슬라이드 자체에서는 가시성 제어가 지원되지 않습니다. 마스터에서는 getShowMasterShapes가 항상 false를 반환하고, setShowMasterShapes에 true를 전달하면 예외가 발생합니다. 일반 슬라이드나 레이아웃에 적용하십시오.

그래픽과 배경 구분

작업 효과
마스터 그래픽 숨기기 마스터 도형을 삭제하거나 슬라이드 자체 도형을 변경하지 않고 가시성을 제어합니다.
슬라이드 배경 채우기 변경 배경 색상, 그라디언트 또는 이미지를 변경합니다. 마스터 그래픽은 별도 도형이므로 해당 배경 위에 계속 표시될 수 있습니다. 자세한 내용은 Presentation Background를 참조하십시오.
마스터에서 도형 삭제 공유 소스 도형을 제거하여 해당 마스터를 사용하는 모든 슬라이드에서 더 이상 사용할 수 없게 합니다.

자리 표시자 작업

자리 표시자는 일반적으로 레이아웃 슬라이드에 정의됩니다. 마스터 슬라이드는 해당 레이아웃이 상속받는 공유 스타일 및 테마를 제공하고, 각 레이아웃은 사용할 자리 표시자와 배치를 결정합니다.

PowerPoint에서는 슬라이드 마스터 보기에서 자리 표시자 명령을 사용할 수 있습니다.

PowerPoint 슬라이드 마스터 보기의 자리 표시자 삽입 명령

Aspose.Slides에서 새 자리 표시자를 추가하려면 마스터에 속한 레이아웃 슬라이드를 작업합니다:

import com.aspose.slides.*;

Presentation presentation = new Presentation("presentation.pptx");
try {
    IMasterSlide masterSlide = presentation.getMasters().get_Item(0);
    ILayoutSlide blankLayoutSlide = masterSlide.getLayoutSlides().getByType(SlideLayoutType.Blank);

    if (blankLayoutSlide == null) {
        blankLayoutSlide = masterSlide.getLayoutSlides().add(SlideLayoutType.Blank, "Blank");
    }

    blankLayoutSlide.getPlaceholderManager().addTextPlaceholder(60, 120, 600, 80);

    presentation.getSlides().addEmptySlide(blankLayoutSlide);
    presentation.save("presentation-with-placeholder.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

또한 마스터 슬라이드에 이미 존재하는 자리 표시자 도형을 포맷할 수 있습니다. 다음 예제는 제목 자리 표시자를 찾아 선형 그라디언트 채우기를 적용합니다:

import com.aspose.slides.*;
import java.awt.Color;

Presentation presentation = new Presentation("presentation.pptx");
try {
    IMasterSlide masterSlide = presentation.getMasters().get_Item(0);
    IAutoShape titlePlaceholder = null;

    for (IShape shape : masterSlide.getShapes()) {
        if (shape instanceof IAutoShape) {
            IAutoShape autoShape = (IAutoShape) shape;

            if (autoShape.getPlaceholder() != null &&
                    autoShape.getPlaceholder().getType() == PlaceholderType.Title) {
                titlePlaceholder = autoShape;
                break;
            }
        }
    }

    if (titlePlaceholder != null) {
        Color redGradientColor = new Color(255, 0, 0);
        Color purpleGradientColor = new Color(128, 0, 128);

        titlePlaceholder.getFillFormat().setFillType(FillType.Gradient);
        titlePlaceholder.getFillFormat().getGradientFormat().setGradientShape(GradientShape.Linear);
        titlePlaceholder.getFillFormat().getGradientFormat().getGradientStops().add(0.0f, redGradientColor);
        titlePlaceholder.getFillFormat().getGradientFormat().getGradientStops().add(1.0f, purpleGradientColor);
    }

    presentation.save("presentation-title-style.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

일반 슬라이드에 상속된 포맷된 제목 자리 표시자

더 많은 자리 표시자 및 텍스트 서식 옵션은 Set Prompt Text in Placeholder와 Text Formatting를 참조하십시오.

슬라이드 마스터 배경 변경

마스터 배경은 레이아웃 및 해당 배경을 재정의하지 않은 슬라이드에 상속됩니다. 다음 예제는 첫 번째 마스터 슬라이드에 단색 배경 색상을 설정합니다:

import com.aspose.slides.*;
import java.awt.Color;

Presentation presentation = new Presentation("presentation.pptx");
try {
    IMasterSlide masterSlide = presentation.getMasters().get_Item(0);
    Color masterBackgroundColor = Color.GREEN;

    masterSlide.getBackground().setType(BackgroundType.OwnBackground);
    masterSlide.getBackground().getFillFormat().setFillType(FillType.Solid);
    masterSlide.getBackground().getFillFormat().getSolidFillColor().setColor(masterBackgroundColor);

    presentation.save("presentation-master-background.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

관련 주제는 Presentation Background와 Presentation Theme를 참조하십시오.

슬라이드 마스터를 다른 프레젠테이션에 복제

IMasterSlideCollection.addClone을 사용하여 마스터 슬라이드를 다른 프레젠테이션으로 복사합니다. 복제된 마스터는 대상 프레젠테이션의 레이아웃 및 슬라이드에서 사용할 수 있습니다.

import com.aspose.slides.*;

Presentation sourcePresentation = new Presentation("source.pptx");
Presentation destinationPresentation = new Presentation("destination.pptx");
try {
    IMasterSlide sourceMasterSlide = sourcePresentation.getMasters().get_Item(0);
    IMasterSlide clonedMasterSlide = destinationPresentation.getMasters().addClone(sourceMasterSlide);

    destinationPresentation.save("destination-with-master.pptx", SaveFormat.Pptx);
} finally {
    sourcePresentation.dispose();
    destinationPresentation.dispose();
}

일반 슬라이드와 마스터를 함께 복제해야 하는 경우는 Clone Slides를 참조하십시오.

여러 슬라이드 마스터 추가

프레젠테이션에는 여러 마스터 슬라이드를 포함할 수 있습니다. 이는 섹션마다 다른 브랜딩, 페이지 구조 또는 테마 설정이 필요할 때 유용합니다.

마스터 슬라이드 삽입 및 관리용 PowerPoint 명령

다음 예제는 기본 마스터를 복제하고 복제본에 다른 배경을 지정한 뒤, 해당 복제 마스터 아래에 레이아웃을 만들고 해당 레이아웃을 기반으로 새 슬라이드를 추가합니다:

import com.aspose.slides.*;
import java.awt.Color;

Presentation presentation = new Presentation("presentation.pptx");
try {
    IMasterSlide defaultMasterSlide = presentation.getMasters().get_Item(0);
    IMasterSlide sectionMasterSlide = presentation.getMasters().addClone(defaultMasterSlide);
    Color sectionMasterBackgroundColor = Color.LIGHT_GRAY;

    sectionMasterSlide.getBackground().setType(BackgroundType.OwnBackground);
    sectionMasterSlide.getBackground().getFillFormat().setFillType(FillType.Solid);
    sectionMasterSlide.getBackground().getFillFormat().getSolidFillColor().setColor(sectionMasterBackgroundColor);

    ILayoutSlide sourceBlankLayout = defaultMasterSlide.getLayoutSlides().getByType(SlideLayoutType.Blank);
    if (sourceBlankLayout == null) {
        sourceBlankLayout = defaultMasterSlide.getLayoutSlides().get_Item(0);
    }

    ILayoutSlide sectionBlankLayout = sectionMasterSlide.getLayoutSlides().addClone(sourceBlankLayout);

    presentation.getSlides().addEmptySlide(sectionBlankLayout);
    presentation.save("presentation-with-multiple-masters.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

슬라이드 마스터 비교

마스터 슬라이드는 IBaseSlide에서 상속된 equals 메서드를 사용하여 비교할 수 있습니다. 비교는 도형, 텍스트, 서식, 애니메이션 및 기타 슬라이드 설정과 같은 정적 콘텐츠와 구조를 확인합니다. 슬라이드 ID와 같은 고유 식별자나 현재 날짜와 같은 동적 자리 표시자 값은 비교하지 않습니다.

import com.aspose.slides.*;

Presentation firstPresentation = new Presentation("first.pptx");
Presentation secondPresentation = new Presentation("second.pptx");
try {
    int firstPresentationMasterCount = firstPresentation.getMasters().size();
    int secondPresentationMasterCount = secondPresentation.getMasters().size();

    for (int firstMasterIndex = 0; firstMasterIndex < firstPresentationMasterCount; firstMasterIndex++) {
        for (int secondMasterIndex = 0; secondMasterIndex < secondPresentationMasterCount; secondMasterIndex++) {
            IMasterSlide firstMasterSlide = firstPresentation.getMasters().get_Item(firstMasterIndex);
            IMasterSlide secondMasterSlide = secondPresentation.getMasters().get_Item(secondMasterIndex);
            boolean areMasterSlidesEqual = firstMasterSlide.equals(secondMasterSlide);

            if (areMasterSlidesEqual) {
                System.out.printf(
                        "first.pptx master #%d equals second.pptx master #%d%n",
                        firstMasterIndex,
                        secondMasterIndex);
            }
        }
    }
} finally {
    firstPresentation.dispose();
    secondPresentation.dispose();
}

자세한 내용은 Compare Presentation Slides를 참조하십시오.

슬라이드 마스터 보기를 기본 보기로 설정

ViewProperties의 setLastView 메서드를 사용하여 PowerPoint가 처음 열 때 표시할 보기를 제어합니다. 다음 예제는 프레젠테이션을 슬라이드 마스터 보기로 엽니다:

import com.aspose.slides.*;

Presentation presentation = new Presentation("presentation.pptx");
try {
    presentation.getViewProperties().setLastView(ViewType.SlideMasterView);
    presentation.save("presentation-master-view.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

다른 보기 설정에 대해서는 Save Presentation를 참조하십시오.

사용되지 않는 마스터 슬라이드 제거

프레젠테이션에 사용되지 않는 마스터 슬라이드가 남아 있는 경우가 있습니다. 사용되지 않는 마스터를 제거하면 파일 크기를 줄이고 템플릿 유지관리를 단순화할 수 있습니다.

removeUnused를 사용하여 getMasters() 컬렉션에서 사용되지 않는 마스터를 제거합니다:

import com.aspose.slides.*;

Presentation presentation = new Presentation("presentation.pptx");
try {
    presentation.getMasters().removeUnused(true);
    presentation.save("presentation-clean.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

또는 저코드 API인 Compress.removeUnusedMasterSlides 메서드를 사용할 수 있습니다:

import com.aspose.slides.*;

Presentation presentation = new Presentation("presentation.pptx");
try {
    Compress.removeUnusedMasterSlides(presentation);
    presentation.save("presentation-clean.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

FAQ

슬라이드 마스터와 레이아웃 슬라이드의 차이점은 무엇인가요?

슬라이드 마스터는 테마, 배경, 공통 도형 및 텍스트 스타일과 같은 공유 디자인 설정을 정의합니다. 레이아웃 슬라이드는 마스터 슬라이드에 속하며 자리 표시자의 특정 배치를 정의합니다. 일반 슬라이드는 레이아웃 슬라이드를 사용하므로 레이아웃과 마스터 모두로부터 상속받습니다.

하나의 프레젠테이션에 여러 슬라이드 마스터를 포함할 수 있나요?

네. 프레젠테이션에 여러 슬라이드 마스터를 포함할 수 있습니다. 섹션마다 시각 체계나 브랜딩이 다를 경우 여러 마스터를 사용하십시오.

자리 표시자를 마스터 슬라이드에 추가해야 하나요, 레이아웃 슬라이드에 추가해야 하나요?

대부분의 경우 레이아웃 슬라이드에 자리 표시자를 추가합니다. 공유 시각 요소와 공유 서식은 마스터 슬라이드에 두고, 내용 자리 표시자는 일반 슬라이드가 사용할 레이아웃에 배치하십시오.

아직 사용 중인 마스터 슬라이드를 삭제할 수 있나요?

아닙니다. 종속된 슬라이드가 있는 마스터 슬라이드는 직접 안전하게 제거할 수 없습니다. 먼저 해당 슬라이드를 다른 마스터의 레이아웃으로 이동하거나, 사용되지 않은 마스터만 제거하는 정리 방법을 사용하십시오.