.NET에서 프레젠테이션 열기

소개

스크래치에서 PowerPoint 프레젠테이션을 만드는 것 외에도, Aspose.Slides를 사용하면 기존 프레젠테이션을 열 수 있습니다. 프레젠테이션을 로드한 후에는 해당 정보를 가져오고, 슬라이드 내용을 편집하고, 새 슬라이드를 추가하고, 기존 슬라이드를 제거하는 등 다양한 작업을 수행할 수 있습니다.

프레젠테이션 열기

기존 프레젠테이션을 열려면 Presentation 클래스를 인스턴스화하고 파일 경로를 생성자에 전달합니다.

다음 C# 예제는 프레젠테이션을 열고 슬라이드 수를 가져오는 방법을 보여줍니다:

// Presentation 클래스를 인스턴스화하고 파일 경로를 생성자에 전달합니다.
using (Presentation presentation = new Presentation("Sample.pptx"))
{
    // 프레젠테이션의 총 슬라이드 수를 출력합니다.
    System.Console.WriteLine(presentation.Slides.Count);
}

비밀번호로 보호된 프레젠테이션 열기

비밀번호로 보호된 프레젠테이션을 열어야 할 경우, LoadOptions 클래스의 Password 속성을 통해 비밀번호를 전달하여 복호화하고 로드합니다. 다음 C# 코드는 이 작업을 보여줍니다:

LoadOptions loadOptions = new LoadOptions {Password = "YOUR_PASSWORD"};
using (Presentation presentation = new Presentation("Sample.pptx", loadOptions))
{
    // 복호화된 프레젠테이션에 대해 작업을 수행합니다.
}

대용량 프레젠테이션 열기

Aspose.Slides는 특히 LoadOptions 클래스의 BlobManagementOptions 속성과 같은 옵션을 제공하여 대용량 프레젠테이션을 로드하는 데 도움을 줍니다.

다음 C# 코드는 대용량 프레젠테이션(예: 2GB)을 로드하는 방법을 보여줍니다:

const string filePath = "LargePresentation.pptx";

LoadOptions loadOptions = new LoadOptions
{
    BlobManagementOptions = 
    {
        // KeepLocked 동작을 선택하십시오—프레젠테이션 파일은 수명 동안 잠긴 상태를 유지합니다.
        // Presentation 인스턴스이지만 메모리에 로드되거나 임시 파일로 복사될 필요는 없습니다.
        PresentationLockingBehavior = PresentationLockingBehavior.KeepLocked,
        IsTemporaryFilesAllowed = true,
        MaxBlobsBytesInMemory = 10 * 1024 * 1024 // 10 MB
    }
};

using (Presentation presentation = new Presentation(filePath, loadOptions))
{
    // 대용량 프레젠테이션이 로드되었으며 사용할 수 있습니다. 메모리 사용량은 낮게 유지됩니다.

    // 프레젠테이션을 수정합니다.
    presentation.Slides[0].Name = "Large presentation";

    // 프레젠테이션을 다른 파일에 저장합니다. 이 작업 중에도 메모리 사용량은 낮게 유지됩니다.
    presentation.Save("LargePresentation-copy.pptx", SaveFormat.Pptx);

    // 이렇게 하지 마십시오! 프레젠테이션 객체가 해제될 때까지 파일이 잠겨 있어 I/O 예외가 발생합니다.
    File.Delete(filePath);
}

// 여기에서는 수행해도 됩니다. 소스 파일은 이제 프레젠테이션 객체에 의해 잠겨 있지 않습니다.
File.Delete(filePath);

외부 리소스 제어

Aspose.Slides는 외부 리소스를 관리할 수 있는 IResourceLoadingCallback 인터페이스를 제공합니다. 다음 C# 코드는 IResourceLoadingCallback 인터페이스를 사용하는 방법을 보여줍니다:

LoadOptions loadOptions = new LoadOptions();
loadOptions.ResourceLoadingCallback = new ImageLoadingHandler();

Presentation presentation = new Presentation("Sample.pptx", loadOptions);
public class ImageLoadingHandler : IResourceLoadingCallback
{
    public ResourceLoadingAction ResourceLoading(IResourceLoadingArgs args)
    {
        if (args.OriginalUri.EndsWith(".jpg"))
        {
            try
            {
                // 대체 이미지를 로드합니다.
                byte[] imageData = File.ReadAllBytes("aspose-logo.jpg");
                args.SetData(imageData);
                return ResourceLoadingAction.UserProvided;
            }
            catch (Exception)
            {
                return ResourceLoadingAction.Skip;
            }
        }
        else if (args.OriginalUri.EndsWith(".png"))
        {
            // 대체 URL을 설정합니다.
            args.Uri = "http://www.google.com/images/logos/ps_logo2.png";
            return ResourceLoadingAction.Default;
        }

        // 다른 모든 이미지를 건너뜁니다.
        return ResourceLoadingAction.Skip;
    }
}

임베디드 바이너리 개체 없이 프레젠테이션 로드

PowerPoint 프레젠테이션은 다음과 같은 유형의 임베디드 바이너리 개체를 포함할 수 있습니다:

ILoadOptions.DeleteEmbeddedBinaryObjects 속성을 사용하면 임베디드 바이너리 개체가 전혀 없는 상태로 프레젠테이션을 로드할 수 있습니다.

이 속성은 잠재적으로 악성인 바이너리 콘텐츠를 제거하는 데 유용합니다. 다음 C# 코드는 임베디드 바이너리 콘텐츠가 전혀 없는 프레젠테이션을 로드하는 방법을 보여줍니다:

LoadOptions loadOptions = new LoadOptions()
{
	DeleteEmbeddedBinaryObjects = true
}

using (Presentation presentation = new Presentation("malware.ppt", loadOptions))
{
    // 프레젠테이션에 대한 작업을 수행합니다.
}

FAQ

파일이 손상되어 열 수 없다는 것을 어떻게 알 수 있나요?
로드 중에 구문/형식 유효성 검사 예외가 발생합니다. 이러한 오류는 종종 잘못된 ZIP 구조나 손상된 PowerPoint 레코드를 언급합니다.

열 때 필수 폰트가 누락되면 어떻게 되나요?
파일은 열리지만 이후 rendering/export 시 폰트가 대체될 수 있습니다. 런타임 환경에 폰트 대체 구성하거나 필요한 폰트 추가하십시오.

열 때 임베디드 미디어(비디오/오디오)는 어떻게 처리되나요?
미디어는 프레젠테이션 리소스로 사용 가능해집니다. 미디어가 외부 경로를 통해 참조되는 경우 해당 경로가 환경에서 접근 가능해야 하며, 그렇지 않으면 rendering/export 시 미디어가 제외될 수 있습니다.