Java에서 프레젠테이션 속성 관리

소개

Aspose.Slides for Java은 내장 및 사용자 정의 문서 속성을 지원합니다. 이러한 속성을 사용하여 작성자, 제목 및 사용자 정의 값과 같은 프레젠테이션 메타데이터를 읽고 업데이트할 수 있습니다.

Aspose.Slides는 IDocumentProperties 인터페이스를 통해 프레젠테이션 문서 속성을 작업할 수 있도록 합니다. 이 인터페이스의 인스턴스는 IPresentation.getDocumentProperties에서 반환됩니다. 다음 예제에서는 이러한 속성을 읽고, 수정하고, 관리하는 방법을 보여줍니다.

프레젠테이션 속성 관리

문서 속성은 프레젠테이션 콘텐츠와 함께 설명 정보를 저장합니다. 두 가지 범주로 나뉩니다:

  • 내장 속성에는 제목, 작성자 및 문서 통계와 같은 표준 메타데이터가 포함됩니다.
  • 사용자 정의 속성에는 사용자가 정의한 이름/값 쌍이 저장됩니다.

Aspose.Slides for Java는 두 범주 모두에 대한 액세스를 제공합니다. PowerPoint에서 파일 → 정보 → 속성 → 고급 속성을 선택하면 문서 메타데이터를 확인할 수 있습니다. 대화 상자는 요약, 통계, 내용, 사용자 정의와 같은 탭으로 정보를 그룹화합니다. 사용자 정의 탭을 사용하여 사용자 정의 속성을 관리합니다.

암호화된 프레젠테이션에서 공개 속성 읽기

개방 암호는 일반적으로 프레젠테이션 콘텐츠와 문서 속성을 모두 보호합니다. IProtectionManager.setEncryptDocumentProperties을 false로 호출한 후 프레젠테이션을 암호화하면 문서 속성이 공개됩니다. 그런 다음 애플리케이션은 LoadOptions.setOnlyLoadDocumentProperties을 true로 설정하고 개방 암호 없이 공개 메타데이터를 읽을 수 있습니다.

LoadOptions.setOnlyLoadDocumentProperties은 Aspose.Slides가 로드하는 내용을 제어합니다; 암호를 해독하지는 않습니다. 속성이 암호화에 포함된 경우 암호 없이 로드하면 실패합니다. 프레젠테이션이 암호화되지 않은 경우 이 옵션은 무시되고 전체 프레젠테이션이 로드됩니다.

예제는 문서 속성이 공개된 암호화된 프레젠테이션이 필요합니다. IProtectionManager.isOnlyDocumentPropertiesLoaded를 통해 로드 모드를 확인한 다음 IPresentation.getDocumentProperties를 사용해 내장 속성을 읽습니다:

import com.aspose.slides.IDocumentProperties;
import com.aspose.slides.LoadOptions;
import com.aspose.slides.Presentation;

LoadOptions loadOptions = new LoadOptions();
loadOptions.setOnlyLoadDocumentProperties(true);

Presentation presentation = new Presentation("public-properties-encrypted.pptx", loadOptions);
try {
    if (presentation.getProtectionManager().isOnlyDocumentPropertiesLoaded()) {
        IDocumentProperties documentProperties = presentation.getDocumentProperties();

        System.out.println("Author: " + documentProperties.getAuthor());
        System.out.println("Title: " + documentProperties.getTitle());
        System.out.println("Keywords: " + documentProperties.getKeywords());
    } else {
        System.out.println("The presentation was not loaded in document-properties-only mode.");
    }
} finally {
    presentation.dispose();
}

이 모드에서는 슬라이드 콘텐츠가 로드되지 않습니다. 슬라이드, 마스터, 레이아웃, 도형, 미디어 및 기타 프레젠테이션 객체를 사용할 수 없습니다. 전체 프레젠테이션 객체 모델이 필요한 작업을 수행하기 전에 항상 IProtectionManager.isOnlyDocumentPropertiesLoaded를 확인하십시오.

암호화된 프레젠테이션 속성 업데이트

암호화된 PPTX 파일을 LoadOptions.setOnlyLoadDocumentProperties을 true로 설정한 후 로드하면 공개 메타데이터를 읽기 위한 용도입니다. Aspose.Slides는 해당 메타데이터 전용 객체에서 변경된 속성을 저장할 수 없습니다. 공개 속성은 암호화된 프레젠테이션 내부의 해당 데이터와 일치해야 하기 때문입니다. 따라서 업데이트하려면 올바른 개방 암호와 전체 로드가 필요합니다.

예제는 공개 문서 속성이 있는 암호화된 PPTX 프레젠테이션과 알려진 개방 암호가 필요합니다. LoadOptions.setPassword으로 전체 프레젠테이션을 연 다음 공개 내장 속성을 업데이트하고 결과를 저장합니다. 그런 다음 IPresentationInfo.isEncrypted를 사용해 암호화가 유지되는지 확인하고, 비밀번호 없이 공개 메타데이터를 다시 열어 새로운 값을 검증합니다:

import com.aspose.slides.IDocumentProperties;
import com.aspose.slides.IPresentationInfo;
import com.aspose.slides.LoadOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.PresentationFactory;
import com.aspose.slides.SaveFormat;

final String inputPath = "public-properties-encrypted.pptx";
final String outputPath = "updated-public-properties-encrypted.pptx";

LoadOptions loadOptions = new LoadOptions();
loadOptions.setPassword("open_password");

Presentation presentation = new Presentation(inputPath, loadOptions);
try {
    IDocumentProperties documentProperties = presentation.getDocumentProperties();
    documentProperties.setTitle("Updated Product Roadmap");
    documentProperties.setKeywords("roadmap, planning, indexed");

    presentation.save(outputPath, SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

IPresentationInfo presentationInfo = PresentationFactory.getInstance().getPresentationInfo(outputPath);
System.out.println("The presentation is encrypted: " + presentationInfo.isEncrypted());

LoadOptions metadataLoadOptions = new LoadOptions();
metadataLoadOptions.setOnlyLoadDocumentProperties(true);

Presentation metadataPresentation = new Presentation(outputPath, metadataLoadOptions);
try {
    if (metadataPresentation.getProtectionManager().isOnlyDocumentPropertiesLoaded()) {
        System.out.println("Title: " + metadataPresentation.getDocumentProperties().getTitle());
        System.out.println("Keywords: " + metadataPresentation.getDocumentProperties().getKeywords());
    } else {
        System.out.println("The presentation was not loaded in document-properties-only mode.");
    }
} finally {
    metadataPresentation.dispose();
}

애플리케이션이 프레젠테이션 콘텐츠를 해독하거나 로드할 수 없는 경우, 암호화된 PPTX 파일의 공개 속성은 읽기 전용으로 취급해야 합니다.

내장 속성 액세스

IDocumentProperties 인터페이스는 getAuthor, getTitle, getCreatedTime와 같은 getter 메서드를 통해 내장 메타데이터를 노출합니다. 이 예제는 기존 프레젠테이션을 열어 저장된 메타데이터를 콘솔에 출력합니다. 값은 입력 프레젠테이션에 따라 다르며 일부 필드는 비어 있을 수 있습니다.

import com.aspose.slides.IDocumentProperties;
import com.aspose.slides.Presentation;

Presentation presentation = new Presentation("AccessBuiltInProperties.pptx");
try {
    IDocumentProperties documentProperties = presentation.getDocumentProperties();

    System.out.println("Category: " + documentProperties.getCategory());
    System.out.println("Content status: " + documentProperties.getContentStatus());
    System.out.println("Creation date: " + documentProperties.getCreatedTime());
    System.out.println("Author: " + documentProperties.getAuthor());
    System.out.println("Comments: " + documentProperties.getComments());
    System.out.println("Keywords: " + documentProperties.getKeywords());
    System.out.println("Last modified by: " + documentProperties.getLastSavedBy());
    System.out.println("Manager: " + documentProperties.getManager());
    System.out.println("Modified date: " + documentProperties.getLastSavedTime());
    System.out.println("Presentation format: " + documentProperties.getPresentationFormat());
    System.out.println("Last print date: " + documentProperties.getLastPrinted());
    System.out.println("Is shared between producers: " + documentProperties.getSharedDoc());
    System.out.println("Subject: " + documentProperties.getSubject());
    System.out.println("Title: " + documentProperties.getTitle());
} finally {
    presentation.dispose();
}

내장 속성 수정

내장 메타데이터를 업데이트하려면 IPresentation.getDocumentProperties가 반환한 객체의 setter 메서드를 사용합니다. 이 예제는 기존 프레젠테이션을 연 뒤 setAuthor, setTitle, setSubject, setComments, setManager를 호출하고, Presentation.save으로 업데이트된 프레젠테이션을 저장합니다. 저장된 PPTX에는 새 메타데이터가 포함됩니다.

import com.aspose.slides.IDocumentProperties;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

Presentation presentation = new Presentation("ModifyBuiltInProperties.pptx");
try {
    IDocumentProperties documentProperties = presentation.getDocumentProperties();

    documentProperties.setAuthor("Aspose.Slides for Java");
    documentProperties.setTitle("Manage PowerPoint Presentation Properties");
    documentProperties.setSubject("Modify Built-in Properties");
    documentProperties.setComments("Aspose description");
    documentProperties.setManager("Aspose manager");

    presentation.save("DocumentProperties_output.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

하이퍼링크 기본값 관리

IDocumentProperties.getHyperlinkBase은 상대 하이퍼링크용 문서 수준 기본값을 반환합니다. IDocumentProperties.setHyperlinkBase을 사용해 변경하거나 지울 수 있습니다. 프레젠테이션과 연결된 파일을 다른 위치로 이동할 때 유용합니다. 이는 개별 하이퍼링크의 IHyperlink.getExternalUrl와는 별개이며, 기본값을 변경해도 저장된 링크 주소나 절대 대상은 바뀌지 않습니다.

예제는 target.html과 같은 상대 링크와 https://example.org/absolute.html과 같은 절대 링크를 모두 포함하는 프레젠테이션이 필요합니다. 기존 기본값을 읽고 새 기본 주소를 할당한 뒤 프레젠테이션을 저장·재열어 값을 확인합니다. 그런 다음 빈 문자열로 기본값을 지우고 별도 저장 파일에서 빈 값을 검증합니다. 예제 기본 주소를 실제 연결 파일 위치로 교체하십시오.

import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

Presentation presentation = new Presentation("hyperlinks.pptx");
try {
    System.out.println("Original base: " + presentation.getDocumentProperties().getHyperlinkBase());

    final String newBase = "https://example.com/new/";
    presentation.getDocumentProperties().setHyperlinkBase(newBase);
    presentation.save("hyperlinks-updated.pptx", SaveFormat.Pptx);

    Presentation updatedPresentation = new Presentation("hyperlinks-updated.pptx");
    try {
        System.out.println("Base preserved: " + newBase.equals(updatedPresentation.getDocumentProperties().getHyperlinkBase()));

        updatedPresentation.getDocumentProperties().setHyperlinkBase("");
        updatedPresentation.save("hyperlinks-cleared.pptx", SaveFormat.Pptx);

        Presentation clearedPresentation = new Presentation("hyperlinks-cleared.pptx");
        try {
            System.out.println("Base cleared: " + "".equals(clearedPresentation.getDocumentProperties().getHyperlinkBase()));
        } finally {
            clearedPresentation.dispose();
        }
    } finally {
        updatedPresentation.dispose();
    }
} finally {
    presentation.dispose();
}

두 검증 메시지는 true를 출력해야 합니다. 기본값을 지워도 개별 하이퍼링크 주소는 그대로 유지됩니다.

기본값이 old 폴더를 가리킬 때 상대 링크 target.pptx는 old/target.pptx를 열었습니다. 기본값을 new 폴더로 바꾸고 프레젠테이션을 저장·재열면 동일 링크가 new/target.pptx를 엽니다. 절대 링크는 그대로 동일 파일을 엽니다. 프레젠테이션을 이동하거나 기본값을 변경하기 전후에 PowerPoint에서 상대 링크를 테스트하십시오.

사용자 정의 프레젠테이션 속성 추가

사용자 정의 속성은 내장 필드와 대응되지 않는 메타데이터를 저장합니다. IDocumentProperties.set_Item 메서드를 사용하면 속성 이름으로 값을 지정할 수 있습니다. 이 예제는 프레젠테이션을 생성하고 두 개의 텍스트 값과 정수형 버전 값을 추가한 뒤 Presentation.save으로 저장합니다. 기존 이름에 값을 할당하면 해당 속성의 값이 교체됩니다.

import com.aspose.slides.IDocumentProperties;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

Presentation presentation = new Presentation();
try {
    IDocumentProperties documentProperties = presentation.getDocumentProperties();

    documentProperties.set_Item("Reviewed by", "John Smith");
    documentProperties.set_Item("Confidentiality level", "Internal");
    documentProperties.set_Item("Document version", 2);

    presentation.save("CustomDocumentProperties_output.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

사용자 정의 속성 액세스 및 수정

이 예제는 사용자 정의 속성을 포함한 프레젠테이션을 열고 getCountOfCustomProperties를 사용해 이를 반복합니다. getCustomPropertyName은 0부터 시작하는 인덱스를 받아 IDocumentProperties.set_Item에서 사용된 이름을 반환합니다. 예제는 각 원래 이름과 값을 출력하고, 원래 유형에 관계없이 번호가 매겨진 문자열로 모든 값을 교체한 뒤 변경사항을 저장합니다. 사용자 정의 속성이 없으면 루프는 아무 변화도 만들지 않습니다.

import com.aspose.slides.IDocumentProperties;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

Presentation presentation = new Presentation("AccessAndModifyProperties.pptx");
try {
    IDocumentProperties documentProperties = presentation.getDocumentProperties();

    for (int i = 0; i < documentProperties.getCountOfCustomProperties(); i++) {
        String propertyName = documentProperties.getCustomPropertyName(i);
        Object propertyValue = documentProperties.get_Item(propertyName);

        System.out.println("Custom property name: " + propertyName);
        System.out.println("Custom property value: " + propertyValue);

        documentProperties.set_Item(propertyName, "New Value " + (i + 1));
    }

    presentation.save("CustomProperties_output.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

실시간 예제

문서 속성을 Aspose.Slides API로 다루는 방법을 확인하려면 온라인 앱 **View & Edit PowerPoint Metadata**을 사용해 보세요:

View & Edit PowerPoint Metadata

FAQ

프레젠테이션에서 내장 속성을 제거하려면 어떻게 해야 하나요?

내장 속성은 프레젠테이션의 필수 부분이며 완전히 제거할 수 없습니다. 그러나 해당 속성을 변경하거나, 해당 속성이 허용하는 경우 빈 값으로 설정할 수 있습니다.

이미 존재하는 사용자 정의 속성을 추가하면 어떻게 되나요?

이미 존재하는 사용자 정의 속성을 추가하면 기존 값이 새 값으로 덮어써집니다. 속성을 미리 제거하거나 확인할 필요 없이 Aspose.Slides가 자동으로 값을 업데이트합니다.

프레젠테이션을 전체 로드하지 않고 속성에 접근할 수 있나요?

예. PresentationFactory.getPresentationInfo를 사용한 뒤 IPresentationInfo.readDocumentProperties를 호출하면 Presentation 인스턴스를 만들지 않고 저장된 문서 메타데이터를 읽을 수 있습니다. 전체 보고 예제와 형식별 제한 사항은 Build a Lightweight Presentation Inventory를 참고하십시오.

암호화된 프레젠테이션의 공개 속성을 개방 암호 없이 읽을 수 있나요?

예. 프레젠테이션이 IProtectionManager.setEncryptDocumentProperties을 false로 호출한 후 암호화되었고, LoadOptions.setOnlyLoadDocumentProperties을 true로 호출해 로드된 경우에 가능합니다.

문서 속성 전용 모드에서 암호화된 PPTX 파일을 업데이트할 수 있나요?

아니요. 공개 속성과 암호화된 속성 데이터는 일관성을 유지해야 하므로, 암호화된 PPTX 파일을 업데이트하려면 올바른 개방 암호로 전체 프레젠테이션을 로드해야 합니다.