Quản lý Thuộc tính Bản trình bày trong .NET
Giới thiệu
Aspose.Slides for .NET hỗ trợ các thuộc tính tài liệu tích hợp sẵn và tùy chỉnh. Sử dụng các thuộc tính này để đọc và cập nhật siêu dữ liệu bản trình bày, chẳng hạn như tác giả, tiêu đề và các giá trị do người dùng định nghĩa.
Aspose.Slides cho phép bạn làm việc với các thuộc tính tài liệu bản trình bày thông qua giao diện IDocumentProperties . Một thể hiện của giao diện này được trả về bởi IPresentation.DocumentProperties. Các ví dụ sau cho thấy cách đọc, sửa đổi và quản lý các thuộc tính này.
Lưu ý
Aspose.Slides quản lý siêu dữ liệu ứng dụng và nhà sản xuất khi lưu bản trình bày; các trường này xác định phần mềm đã tạo ra kết quả.Quản lý Thuộc tính Bản trình bày
Thuộc tính tài liệu lưu trữ thông tin mô tả cùng với nội dung bản trình bày. Chúng thuộc hai loại:
- Thuộc tính tích hợp sẵn chứa siêu dữ liệu tiêu chuẩn, chẳng hạn như tiêu đề, tác giả và thống kê tài liệu.
- Thuộc tính tùy chỉnh lưu trữ các cặp tên/giá trị do người dùng định nghĩa.
Aspose.Slides for .NET cung cấp quyền truy cập vào cả hai loại. Trong PowerPoint, chọn File → Info → Properties → Advanced Properties để xem siêu dữ liệu tài liệu. Hộp thoại nhóm thông tin vào các tab như Summary, Statistics, Contents và Custom. Sử dụng tab Custom để quản lý các thuộc tính do người dùng định nghĩa.
Đọc Thuộc tính Công khai từ Bản trình bày Được Mã hoá
Mật khẩu mở thường bảo vệ cả nội dung bản trình bày và các thuộc tính tài liệu. Khi một bản trình bày được mã hoá bằng IProtectionManager.EncryptDocumentProperties đặt thành false, các thuộc tính tài liệu của nó vẫn công khai. Ứng dụng sau đó có thể đặt LoadOptions.OnlyLoadDocumentProperties thành true và đọc siêu dữ liệu công khai mà không cần cung cấp mật khẩu mở.
LoadOptions.OnlyLoadDocumentProperties kiểm soát những gì Aspose.Slides tải; nó không giải mã bất kỳ thứ gì. Nếu các thuộc tính đã được bao gồm trong quá trình mã hoá, việc tải chúng mà không có mật khẩu sẽ thất bại. Nếu bản trình bày không được mã hoá, tùy chọn này sẽ bị bỏ qua và toàn bộ bản trình bày sẽ được tải.
Ví dụ yêu cầu một bản trình bày được mã hoá mà các thuộc tính tài liệu của nó được để công khai. Nó xác minh chế độ tải qua IProtectionManager.IsOnlyDocumentPropertiesLoaded và sau đó đọc các thuộc tính tích hợp sẵn qua IPresentation.DocumentProperties:
using System;
using Aspose.Slides;
var loadOptions = new LoadOptions { OnlyLoadDocumentProperties = true };
using var presentation = new Presentation("public-properties-encrypted.pptx", loadOptions);
if (presentation.ProtectionManager.IsOnlyDocumentPropertiesLoaded)
{
var documentProperties = presentation.DocumentProperties;
Console.WriteLine("Author: " + documentProperties.Author);
Console.WriteLine("Title: " + documentProperties.Title);
Console.WriteLine("Keywords: " + documentProperties.Keywords);
}
else
{
Console.WriteLine("The presentation was not loaded in document-properties-only mode.");
}
Trong chế độ này, nội dung slide không được tải. Các slide, master, layout, shape, phương tiện và các đối tượng khác của bản trình bày sẽ không khả dụng. Ứng dụng luôn nên kiểm tra IProtectionManager.IsOnlyDocumentPropertiesLoaded trước khi thực hiện thao tác yêu cầu mô hình đối tượng bản trình bày đầy đủ.
Bảo mật
Siêu dữ liệu công khai có thể lộ tên tác giả, tiêu đề, chủ đề, từ khóa, thông tin công ty, bình luận và các giá trị tùy chỉnh. Hãy mã hoá các thuộc tính nhạy cảm cùng với bản trình bày. Chỉ để chúng công khai khi việc lập chỉ mục, phân loại, tìm kiếm hoặc hệ thống quản lý tài liệu có yêu cầu cụ thể để truy cập chúng mà không cần mật khẩu.Cập nhật Thuộc tính của Bản trình bày Được Mã hoá
Đối với tệp PPTX được mã hoá, một bản trình bày được tải bằng LoadOptions.OnlyLoadDocumentProperties nhằm mục đích đọc siêu dữ liệu công khai. Aspose.Slides không thể lưu các thuộc tính đã thay đổi từ đối tượng chỉ chứa siêu dữ liệu này vì các thuộc tính công khai phải giữ nhất quán với dữ liệu tương ứng trong bản trình bày được mã hoá. Vì vậy, việc cập nhật chúng yêu cầu mật khẩu mở đúng và tải đầy đủ bản trình bày.
Ví dụ yêu cầu một bản trình bày PPTX được mã hoá với các thuộc tính tài liệu công khai và mật khẩu mở đã biết. Nó mở toàn bộ bản trình bày bằng LoadOptions.Password, cập nhật các thuộc tính tích hợp sẵn công khai, và lưu kết quả. Sau đó nó sử dụng IPresentationInfo.IsEncrypted để xác minh rằng việc mã hoá được giữ nguyên và mở lại siêu dữ liệu công khai mà không có mật khẩu để kiểm tra các giá trị mới:
using System;
using Aspose.Slides;
using Aspose.Slides.Export;
const string inputPath = "public-properties-encrypted.pptx";
const string outputPath = "updated-public-properties-encrypted.pptx";
var loadOptions = new LoadOptions { Password = "open_password" };
using var presentation = new Presentation(inputPath, loadOptions);
var documentProperties = presentation.DocumentProperties;
documentProperties.Title = "Updated Product Roadmap";
documentProperties.Keywords = "roadmap, planning, indexed";
presentation.Save(outputPath, SaveFormat.Pptx);
var presentationInfo = PresentationFactory.Instance.GetPresentationInfo(outputPath);
Console.WriteLine("The presentation is encrypted: " + presentationInfo.IsEncrypted);
var metadataLoadOptions = new LoadOptions { OnlyLoadDocumentProperties = true };
using var metadataPresentation = new Presentation(outputPath, metadataLoadOptions);
if (metadataPresentation.ProtectionManager.IsOnlyDocumentPropertiesLoaded)
{
Console.WriteLine("Title: " + metadataPresentation.DocumentProperties.Title);
Console.WriteLine("Keywords: " + metadataPresentation.DocumentProperties.Keywords);
}
else
{
Console.WriteLine("The presentation was not loaded in document-properties-only mode.");
}
Nếu một ứng dụng không được phép giải mã hoặc tải nội dung bản trình bày, nó phải coi các thuộc tính công khai của tệp PPTX được mã hoá là chỉ đọc.
Truy cập Thuộc tính Tích hợp sẵn
Giao diện IDocumentProperties cung cấp siêu dữ liệu tích hợp sẵn thông qua các thuộc tính kiểu, bao gồm Author, Title, và CreatedTime. Ví dụ này mở một bản trình bày tồn tại và in siêu dữ liệu đã lưu của nó ra console. Các giá trị phụ thuộc vào bản trình bày đầu vào; một số trường có thể để trống.
using System;
using Aspose.Slides;
using var presentation = new Presentation("AccessBuiltInProperties.pptx");
var documentProperties = presentation.DocumentProperties;
Console.WriteLine("Category: " + documentProperties.Category);
Console.WriteLine("Content status: " + documentProperties.ContentStatus);
Console.WriteLine("Creation date: " + documentProperties.CreatedTime);
Console.WriteLine("Author: " + documentProperties.Author);
Console.WriteLine("Comments: " + documentProperties.Comments);
Console.WriteLine("Keywords: " + documentProperties.Keywords);
Console.WriteLine("Last modified by: " + documentProperties.LastSavedBy);
Console.WriteLine("Manager: " + documentProperties.Manager);
Console.WriteLine("Modified date: " + documentProperties.LastSavedTime);
Console.WriteLine("Presentation format: " + documentProperties.PresentationFormat);
Console.WriteLine("Last print date: " + documentProperties.LastPrinted);
Console.WriteLine("Is shared between producers: " + documentProperties.SharedDoc);
Console.WriteLine("Subject: " + documentProperties.Subject);
Console.WriteLine("Title: " + documentProperties.Title);
Báo cáo Số phiên bản và Thời gian chỉnh sửa
Để báo cáo một dòng cho mỗi tệp PPTX trong thư mục presentations, gọi ReadDocumentProperties để lấy IDocumentProperties của nó mà không tải các slide để chỉnh sửa. Định dạng chuẩn c cho TimeSpan bao gồm số ngày khi thời lượng đạt 24 giờ.
using System;
using System.IO;
using Aspose.Slides;
Console.WriteLine("File | RevisionNumber | TotalEditingTime");
foreach (var path in Directory.EnumerateFiles("presentations", "*.pptx"))
{
var info = PresentationFactory.Instance.GetPresentationInfo(path);
var properties = info.ReadDocumentProperties();
Console.WriteLine($"{Path.GetFileName(path)} | {properties.RevisionNumber} | {properties.TotalEditingTime:c}");
}
Ví dụ đầu ra cho một bản trình bày có 1 ngày, 2 giờ và 5 phút thời gian chỉnh sửa được ghi lại:
File | RevisionNumber | TotalEditingTime
long.pptx | 17 | 1.02:05:00
RevisionNumber là siêu dữ liệu có thể ghi. Giá trị của nó một mình không thể xác định số lần bản trình bày được lưu hoặc tái tạo lịch sử phiên bản.
TotalEditingTime là thời lượng chỉnh sửa được ghi trong tệp. Giá trị 00:00:00 có nghĩa là thời lượng lưu trữ là zero. Nó không chứng minh rằng không ai chỉnh sửa bản trình bày.
Sửa đổi Thuộc tính Tích hợp sẵn
Để cập nhật siêu dữ liệu tích hợp sẵn, gán giá trị cho các thuộc tính có thể ghi được mở ra bởi IPresentation.DocumentProperties. Ví dụ này mở một bản trình bày tồn tại, đặt Author, Title, Subject, Comments, và Manager, sau đó lưu bản trình bày đã cập nhật bằng Presentation.Save. PPTX đã lưu chứa siêu dữ liệu mới.
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("ModifyBuiltInProperties.pptx");
var documentProperties = presentation.DocumentProperties;
documentProperties.Author = "Aspose.Slides for .NET";
documentProperties.Title = "Manage PowerPoint Presentation Properties";
documentProperties.Subject = "Modify Built-in Properties";
documentProperties.Comments = "Aspose description";
documentProperties.Manager = "Aspose manager";
presentation.Save("DocumentProperties_output.pptx", SaveFormat.Pptx);
Quản lý Cơ sở Siêu liên kết
IDocumentProperties.HyperlinkBase lưu trữ cơ sở mức tài liệu cho các siêu liên kết tương đối. Điều này hữu ích khi di chuyển một bản trình bày và các tệp liên kết tới vị trí khác. Nó tách biệt với IHyperlink.ExternalUrl của một siêu liên kết riêng lẻ: việc thay đổi cơ sở không ghi lại lại các địa chỉ liên kết đã lưu hoặc thay đổi đích tuyệt đối.
Ví dụ yêu cầu một bản trình bày chứa cả liên kết tương đối, như target.html, và liên kết tuyệt đối, như https://example.org/absolute.html. Nó đọc cơ sở hiện có, gán địa chỉ cơ sở mới, và lưu và mở lại bản trình bày để kiểm tra giá trị. Sau đó nó xóa cơ sở bằng string.Empty và xác minh giá trị rỗng trong một tệp đã lưu riêng. Thay thế địa chỉ cơ sở mẫu bằng vị trí của các tệp liên kết của bạn.
using System;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("hyperlinks.pptx");
Console.WriteLine("Original base: " + presentation.DocumentProperties.HyperlinkBase);
const string newBase = "https://example.com/new/";
presentation.DocumentProperties.HyperlinkBase = newBase;
presentation.Save("hyperlinks-updated.pptx", SaveFormat.Pptx);
using var updatedPresentation = new Presentation("hyperlinks-updated.pptx");
Console.WriteLine("Base preserved: " + (updatedPresentation.DocumentProperties.HyperlinkBase == newBase));
updatedPresentation.DocumentProperties.HyperlinkBase = string.Empty;
updatedPresentation.Save("hyperlinks-cleared.pptx", SaveFormat.Pptx);
using var clearedPresentation = new Presentation("hyperlinks-cleared.pptx");
Console.WriteLine("Base cleared: " + (clearedPresentation.DocumentProperties.HyperlinkBase == string.Empty));
Hai thông báo xác minh nên in ra True. Xóa cơ sở để lại các địa chỉ siêu liên kết riêng lẻ không thay đổi.
Một liên kết tương đối tới target.pptx đã mở old/target.pptx khi cơ sở trỏ tới thư mục old. Sau khi thay đổi cơ sở thành thư mục new, lưu và mở lại bản trình bày, cùng một liên kết đã mở new/target.pptx. Liên kết tuyệt đối vẫn mở cùng một tệp. Hãy kiểm tra các liên kết tương đối của bạn trong PowerPoint trước và sau khi di chuyển bản trình bày hoặc thay đổi cơ sở.
Thêm Thuộc tính Bản trình bày Tùy chỉnh
Các thuộc tính tùy chỉnh lưu trữ siêu dữ liệu không có trường tích hợp sẵn tương ứng. IDocumentProperties indexer cho phép bạn gán giá trị theo tên thuộc tính. Ví dụ này tạo một bản trình bày, thêm hai giá trị văn bản và một giá trị phiên bản kiểu nguyên, và lưu bản trình bày bằng Presentation.Save. Gán giá trị cho một tên đã tồn tại sẽ thay thế giá trị của thuộc tính đó.
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var documentProperties = presentation.DocumentProperties;
documentProperties["Reviewed by"] = "John Smith";
documentProperties["Confidentiality level"] = "Internal";
documentProperties["Document version"] = 2;
presentation.Save("CustomDocumentProperties_output.pptx", SaveFormat.Pptx);
Truy cập và Sửa đổi Thuộc tính Tùy chỉnh
Ví dụ này mở một bản trình bày chứa các thuộc tính tùy chỉnh và sử dụng CountOfCustomProperties để lặp qua chúng. GetCustomPropertyName nhận một chỉ mục bắt đầu từ 0 và trả về tên được sử dụng bởi IDocumentProperties indexer. Ví dụ in mỗi tên và giá trị gốc, thay thế mọi giá trị bằng một chuỗi đánh số bất kể kiểu gốc, và lưu các thay đổi. Nếu không có thuộc tính tùy chỉnh, vòng lặp không thực hiện thay đổi nào.
using System;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("AccessAndModifyProperties.pptx");
var documentProperties = presentation.DocumentProperties;
for (var i = 0; i < documentProperties.CountOfCustomProperties; i++)
{
var propertyName = documentProperties.GetCustomPropertyName(i);
var propertyValue = documentProperties[propertyName];
Console.WriteLine("Custom property name: " + propertyName);
Console.WriteLine("Custom property value: " + propertyValue);
documentProperties[propertyName] = "New Value " + (i + 1);
}
presentation.Save("CustomProperties_output.pptx", SaveFormat.Pptx);
Ví dụ Trực tiếp
Hãy thử ứng dụng trực tuyến View & Edit PowerPoint Metadata để xem cách làm việc với các thuộc tính tài liệu bằng API Aspose.Slides:
Câu hỏi thường gặp
Làm thế nào để tôi xóa một thuộc tính tích hợp sẵn khỏi bản trình bày?
Các thuộc tính tích hợp sẵn là một phần không thể tách rời của bản trình bày và không thể bị xóa hoàn toàn. Tuy nhiên, bạn có thể thay đổi giá trị của chúng hoặc đặt chúng thành rỗng nếu thuộc tính cụ thể cho phép.
Điều gì xảy ra nếu tôi thêm một thuộc tính tùy chỉnh đã tồn tại?
Nếu bạn thêm một thuộc tính tùy chỉnh đã tồn tại, giá trị hiện tại của nó sẽ bị ghi đè bằng giá trị mới. Bạn không cần phải xóa hoặc kiểm tra thuộc tính trước, vì Aspose.Slides sẽ tự động cập nhật giá trị của thuộc tính.
Tôi có thể truy cập các thuộc tính bản trình bày mà không tải đầy đủ bản trình bày không?
Có. Sử dụng PresentationFactory.GetPresentationInfo và sau đó IPresentationInfo.ReadDocumentProperties để đọc siêu dữ liệu tài liệu đã lưu mà không tạo một thể hiện Presentation . Xem Build a Lightweight Presentation Inventory để có ví dụ báo cáo đầy đủ và các hạn chế theo định dạng.
Tôi có thể đọc các thuộc tính công khai của một bản trình bày được mã hoá mà không có mật khẩu mở không?
Có. Bản trình bày phải đã được mã hoá bằng IProtectionManager.EncryptDocumentProperties đặt thành false, và phải được tải bằng LoadOptions.OnlyLoadDocumentProperties đặt thành true.
Tôi có thể cập nhật một tệp PPTX được mã hoá trong chế độ chỉ tải thuộc tính tài liệu không?
Không. Dữ liệu thuộc tính công khai và được mã hoá phải giữ nhất quán, vì vậy việc cập nhật tệp PPTX được mã hoá yêu cầu tải toàn bộ bản trình bày với mật khẩu mở đúng.
