Lưu bản thuyết trình bằng C++

Tổng quan

Sau khi bạn tạo một bản thuyết trình hoặc mở một bản hiện có, hãy sử dụng phương thức Presentation::Save để ghi kết quả. Aspose.Slides cho C++ có thể lưu bản thuyết trình vào tệp hoặc luồng ở các định dạng PowerPoint, OpenDocument, PDF và các định dạng khác. Các phần sau đây đề cập đến các thao tác lưu chuẩn và các tùy chọn có sẵn cho đầu ra PPTX.

Lưu bản thuyết trình vào tệp

Để lưu một bản thuyết trình vào tệp, truyền đường dẫn đầu ra và một giá trị SaveFormat cho phương thức Presentation::Save. Giá trị định dạng xác định loại tệp mà Aspose.Slides tạo ra.

Ví dụ sau tạo một bản thuyết trình và lưu nó dưới dạng tệp PPTX:

#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/smart_ptr.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;

auto presentation = MakeObject<Presentation>();

// Thêm hoặc chỉnh sửa nội dung bản thuyết trình ở đây.

presentation->Save(u"Output.pptx", SaveFormat::Pptx);
presentation->Dispose();

Lưu bản thuyết trình ở định dạng gốc

Đối với các ví dụ phát hiện tệp và luồng, hành vi của các bản thuyết trình mới tạo và sự khác biệt giữa định dạng nguồn và đầu ra, xem mục Determine the Original Presentation Format.

Trong một ứng dụng xử lý hàng loạt, định dạng đầu vào có thể không được biết trước. Sau khi tải một tệp, đọc định dạng gốc của nó bằng IPresentation::get_SourceFormat. Truyền giá trị SourceFormat thu được cho SlideUtil::ToSaveFormat để lấy giá trị SaveFormat tương ứng, sau đó sử dụng Presentation::Save để ghi bản thuyết trình đã chỉnh sửa.

Ví dụ đầy đủ sau xử lý mọi tệp trong thư mục đầu vào, cập nhật tiêu đề và lưu chúng vào thư mục đầu ra ở định dạng đã được tải:

#include <DOM/IDocumentProperties.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <Util/SlideUtil.h>
#include <system/console.h>
#include <system/exception.h>
#include <system/io/directory.h>
#include <system/io/path.h>
#include <system/smart_ptr.h>
#include <system/string.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace Aspose::Slides::Util;
using namespace System;
using namespace System::IO;

String inputDirectory = u"Input";
String outputDirectory = u"Output";

Directory::CreateDirectory_(outputDirectory);

auto inputPaths = Directory::GetFiles(inputDirectory);
for (const auto& inputPath : inputPaths)
{
    try
    {
        auto presentation = MakeObject<Presentation>(inputPath);

        auto sourceFormat = presentation->get_SourceFormat();
        auto saveFormat = SlideUtil::ToSaveFormat(sourceFormat);

        presentation->get_DocumentProperties()->set_Title(u"Processed by the batch application");

        auto outputPath = Path::Combine(outputDirectory, Path::GetFileName(inputPath));
        presentation->Save(outputPath, saveFormat);
        presentation->Dispose();
    }
    catch (ArgumentException& exception)
    {
        Console::get_Error()->WriteLine(String::Format(u"Cannot map the source format of '{0}': {1}", inputPath, exception->get_Message()));
    }
    catch (Exception& exception)
    {
        Console::get_Error()->WriteLine(String::Format(u"Cannot process '{0}': {1}", inputPath, exception->get_Message()));
    }
}

SlideUtil::ToSaveFormat ánh xạ PPT, PPTX, ODP, PPTM, PPSX, PPSM, POTX, POTM, PPS, POT, OTP, FODP và PowerPoint XML sang các định dạng lưu bản thuyết trình tương ứng. Nó chỉ ánh xạ các định dạng nguồn của bản thuyết trình; không được dùng để chọn các định dạng xuất như PDF, HTML, TIFF hoặc hình ảnh. Truyền một giá trị SourceFormat không được hỗ trợ hoặc không hợp lệ sẽ gây ra một ArgumentException.

Các tệp PPT, PPS và POT cổ điển sử dụng cùng một container nhị phân. Khi một bản thuyết trình như vậy được tải từ luồng mà không có phần mở rộng tệp, một tệp PPS hoặc POT có thể bị nhận dạng là PPT. Nếu cần giữ nguyên các kiểu phụ cổ điển này, hãy lưu tên tệp gốc hoặc siêu dữ liệu định dạng riêng và sử dụng chúng khi chọn tên tệp và định dạng đầu ra.

Lưu bản thuyết trình vào luồng

Để ghi một bản thuyết trình mà không dựa vào đường dẫn tệp cuối cùng, truyền một Stream có khả năng ghi và một giá trị SaveFormat cho phương thức Presentation::Save. Cách này hữu ích khi đầu ra phải được trả về từ một dịch vụ web, lưu trong cơ sở dữ liệu hoặc xử lý trong bộ nhớ.

Ví dụ sau lưu một bản thuyết trình mới vào luồng tệp:

#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/io/file_mode.h>
#include <system/io/file_stream.h>
#include <system/smart_ptr.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::IO;

auto presentation = MakeObject<Presentation>();
auto outputStream = MakeObject<FileStream>(u"Output.pptx", FileMode::Create);

presentation->Save(outputStream, SaveFormat::Pptx);

outputStream->Close();
presentation->Dispose();

Lưu bản thuyết trình với Kiểu xem được định trước

Bạn có thể chỉ định chế độ xem mà PowerPoint sẽ mở bản thuyết trình đã lưu ban đầu. Gọi ViewProperties::set_LastView với một giá trị ViewType trước khi lưu.

Ví dụ sau cấu hình chế độ xem Slide Master làm chế độ xem ban đầu:

#include <DOM/IViewProperties.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <ViewType.h>
#include <system/smart_ptr.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;

auto presentation = MakeObject<Presentation>();

presentation->get_ViewProperties()->set_LastView(ViewType::SlideMasterView);
presentation->Save(u"SlideMasterView.pptx", SaveFormat::Pptx);

presentation->Dispose();

Lưu bản thuyết trình ở định dạng Office Open XML Strict

Để tạo một tệp PPTX tuân thủ hồ sơ Strict của Office Open XML, tạo một thể hiện PptxOptions và gọi PptxOptions::set_Conformance với Conformance::Iso29500_2008_Strict. Sau đó truyền các tùy chọn này cho phương thức Presentation::Save.

#include <DOM/Presentation.h>
#include <Export/Conformance.h>
#include <Export/PptxOptions.h>
#include <Export/SaveFormat.h>
#include <system/smart_ptr.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;

auto options = MakeObject<PptxOptions>();
options->set_Conformance(Conformance::Iso29500_2008_Strict);

auto presentation = MakeObject<Presentation>();

presentation->Save(u"StrictOfficeOpenXml.pptx", SaveFormat::Pptx, options);
presentation->Dispose();

Lưu bản thuyết trình ở định dạng Office Open XML ở chế độ Zip64

Một kho ZIP chuẩn giới hạn kích thước nén và giải nén của mỗi mục, tổng kích thước kho và số mục. Vì tệp PPTX là một kho ZIP, một bản thuyết trình rất lớn có thể vượt quá các giới hạn này. Các phần mở rộng ZIP64 nâng cao các giới hạn kích thước và số mục áp dụng.

Sử dụng PptxOptions::set_Zip64Mode để kiểm soát việc Aspose.Slides có ghi phần mở rộng ZIP64 hay không:

  • IfNecessary chỉ sử dụng ZIP64 khi bản thuyết trình vượt quá giới hạn ZIP chuẩn. Đây là chế độ mặc định.
  • Never tắt các phần mở rộng ZIP64.
  • Always luôn ghi các phần mở rộng ZIP64.

Ví dụ sau luôn bật các phần mở rộng ZIP64 cho bản thuyết trình đầu ra:

#include <DOM/Presentation.h>
#include <Export/PptxOptions.h>
#include <Export/SaveFormat.h>
#include <Export/Zip64Mode.h>
#include <system/smart_ptr.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;

auto presentation = MakeObject<Presentation>(u"Sample.pptx");

auto options = MakeObject<PptxOptions>();
options->set_Zip64Mode(Zip64Mode::Always);

presentation->Save(u"OutputZip64.pptx", SaveFormat::Pptx, options);
presentation->Dispose();

Lưu bản thuyết trình ở định dạng Office Open XML với mức nén

Đối với đầu ra PPTX, bạn có thể cân bằng tốc độ lưu và kích thước tệp bằng cách gọi PptxOptions::set_CompressionLevel. Enum CompressionLevel cung cấp các giá trị sau:

  • None lưu dữ liệu mà không nén.
  • Level1 cung cấp mức nén nhanh nhất và kích thước nén lớn nhất.
  • Level2 đến Level5 dần dần ưu tiên kết quả nhỏ hơn hơn tốc độ lưu.
  • Level6 cân bằng tốc độ lưu và kích thước tệp. Đây là mức mặc định.
  • Level7 và Level8 tiếp tục ưu tiên kết quả nhỏ hơn hơn tốc độ lưu.
  • Level9 cung cấp mức nén mạnh nhất và cần thời gian xử lý nhiều nhất.

Ví dụ sau lưu một bản thuyết trình mà không nén:

#include <DOM/Presentation.h>
#include <Export/CompressionLevel.h>
#include <Export/PptxOptions.h>
#include <Export/SaveFormat.h>
#include <system/smart_ptr.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;

auto presentation = MakeObject<Presentation>(u"Sample.pptx");

auto options = MakeObject<PptxOptions>();
options->set_CompressionLevel(CompressionLevel::None);

presentation->Save(u"OutputNoCompression.pptx", SaveFormat::Pptx, options);
presentation->Dispose();

Ví dụ sau sử dụng mức nén tối đa:

#include <DOM/Presentation.h>
#include <Export/CompressionLevel.h>
#include <Export/PptxOptions.h>
#include <Export/SaveFormat.h>
#include <system/smart_ptr.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;

auto presentation = MakeObject<Presentation>(u"Sample.pptx");

auto options = MakeObject<PptxOptions>();
options->set_CompressionLevel(CompressionLevel::Level9);

presentation->Save(u"OutputMaximumCompression.pptx", SaveFormat::Pptx, options);
presentation->Dispose();

Lưu bản thuyết trình mà không làm mới thumbnail

Khi một bản thuyết trình được lưu dưới dạng PPTX, PptxOptions::set_RefreshThumbnail điều khiển thumbnail của tài liệu:

  • true tạo lại thumbnail trong quá trình lưu. Đây là giá trị mặc định.
  • false giữ nguyên thumbnail hiện có. Nếu bản thuyết trình không có thumbnail, Aspose.Slides sẽ không tạo.

Ví dụ sau lưu một bản thuyết trình mà không làm mới thumbnail:

#include <DOM/Presentation.h>
#include <Export/PptxOptions.h>
#include <Export/SaveFormat.h>
#include <system/smart_ptr.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;

auto presentation = MakeObject<Presentation>(u"Sample.pptx");

auto options = MakeObject<PptxOptions>();
options->set_RefreshThumbnail(false);

presentation->Save(u"Output.pptx", SaveFormat::Pptx, options);
presentation->Dispose();

Cập nhật tiến trình lưu bằng phần trăm

Để giám sát một thao tác lưu, triển khai giao diện IProgressCallback và truyền triển khai đó cho ISaveOptions::set_ProgressCallback. Aspose.Slides sau đó sẽ gọi IProgressCallback::Reporting với các giá trị tiến độ trong quá trình xuất.

Ví dụ sau báo cáo tiến độ xuất PDF ra console:

#include <DOM/Presentation.h>
#include <Export/PdfOptions.h>
#include <Export/SaveFormat.h>
#include <IProgressCallback.h>
#include <system/console.h>
#include <system/smart_ptr.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;

class ExportProgressHandler : public IProgressCallback
{
public:
    void Reporting(double progressValue) override
    {
        int progress = static_cast<int>(progressValue);
        Console::WriteLine(u"{0}% of the file has been converted.", progress);
    }
};

auto options = MakeObject<PdfOptions>();
options->set_ProgressCallback(MakeObject<ExportProgressHandler>());

auto presentation = MakeObject<Presentation>(u"Sample.pptx");

presentation->Save(u"Output.pdf", SaveFormat::Pdf, options);
presentation->Dispose();

CÂU HỎI THƯỜNG GẶP

Aspose.Slides có hỗ trợ lưu tăng dần hay “lưu nhanh”?

Không. Mỗi thao tác lưu ghi toàn bộ tệp đầu ra thay vì chỉ cập nhật các phần đã thay đổi.

Nhiều luồng có thể lưu cùng một đối tượng Presentation không?

Không. Một đối tượng Presentation không an toàn với đa luồng. Chỉ truy cập và lưu mỗi đối tượng từ một luồng tại một thời điểm.

Điều gì xảy ra với siêu liên kết và các tệp được liên kết bên ngoài khi tôi lưu một bản thuyết trình?

Hyperlinks vẫn còn trong bản thuyết trình. Aspose.Slides không sao chép các tệp liên kết bên ngoài, vì vậy bản thuyết trình đã lưu vẫn phải có khả năng truy cập đến vị trí của chúng.

Tôi có thể lưu siêu dữ liệu tài liệu như tác giả, tiêu đề, công ty và ngày tạo không?

Có. Đặt các thuộc tính tài liệu phù hợp trước khi lưu, và Aspose.Slides sẽ ghi chúng vào tệp đầu ra.