Save Presentations in C++
Overview
After you create a presentation or open an existing one, use the Presentation::Save method to write the result. Aspose.Slides for C++ can save a presentation to a file or stream in PowerPoint, OpenDocument, PDF, and other formats. The following sections cover the standard save operations and the options available for PPTX output.
Save Presentations to Files
To save a presentation to a file, pass the output path and a SaveFormat value to the Presentation::Save method. The format value determines the type of file that Aspose.Slides creates.
The following example creates a presentation and saves it as a PPTX file:
#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>();
// Add or modify presentation content here.
presentation->Save(u"Output.pptx", SaveFormat::Pptx);
presentation->Dispose();
Save Presentations in Their Original Format
In a batch-processing application, the input format may not be known in advance. After loading a file, read its original format with IPresentation::get_SourceFormat. Pass the resulting SourceFormat value to SlideUtil::ToSaveFormat to obtain the corresponding SaveFormat value, and then use Presentation::Save to write the modified presentation.
The following complete example processes every file in an input directory, updates its title, and saves it to an output directory in the format from which it was loaded:
#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 maps PPT, PPTX, ODP, PPTM, PPSX, PPSM, POTX, POTM, PPS, POT, OTP, FODP, and PowerPoint XML to their corresponding presentation save formats. It maps presentation source formats only; it is not intended to select export formats such as PDF, HTML, TIFF, or images. Passing an unsupported or invalid SourceFormat value results in an ArgumentException.
Legacy PPT, PPS, and POT files use the same binary container. When such a presentation is loaded from a stream without a file extension, a PPS or POT file may therefore be identified as PPT. If preserving these legacy subtypes is required, retain the original filename or format metadata separately and use it when choosing the output filename and format.
Save Presentations to Streams
To write a presentation without relying on a final file path, pass a writable Stream and a SaveFormat value to the Presentation::Save method. This approach is useful when the output must be returned from a web service, stored in a database, or processed in memory.
The following example saves a new presentation to a file stream:
#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();
Save Presentations with a Predefined View Type
You can specify the view in which PowerPoint initially opens a saved presentation. Call ViewProperties::set_LastView with a ViewType value before saving.
The following example configures Slide Master view as the initial view:
#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();
Save Presentations in the Strict Office Open XML Format
To create a PPTX file that conforms to the Strict profile of Office Open XML, create a PptxOptions instance and call PptxOptions::set_Conformance with Conformance::Iso29500_2008_Strict. Then pass the options to the Presentation::Save method.
#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();
Save Presentations in Office Open XML Format in Zip64 Mode
A standard ZIP archive limits the compressed and uncompressed size of each entry, the total archive size, and the number of entries. Because a PPTX file is a ZIP archive, a very large presentation can exceed those limits. ZIP64 extensions raise the applicable size and entry-count limits.
Use PptxOptions::set_Zip64Mode to control whether Aspose.Slides writes ZIP64 extensions:
IfNecessaryuses ZIP64 only when the presentation exceeds standard ZIP limits. This is the default mode.Neverdisables ZIP64 extensions.Alwaysalways writes ZIP64 extensions.
The following example always enables ZIP64 extensions for the output presentation:
#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();
Warning
IfZip64Mode is set to Never and the presentation cannot fit within standard ZIP limits, the save operation throws a PptxException.
Save Presentations in Office Open XML Format with Compression Levels
For PPTX output, you can balance saving speed against file size by calling PptxOptions::set_CompressionLevel. The CompressionLevel enumeration provides these values:
Nonestores data without compression.Level1provides the fastest compression and the largest compressed output.Level2throughLevel5progressively favor smaller output over saving speed.Level6balances saving speed and file size. This is the default level.Level7andLevel8further favor smaller output over saving speed.Level9provides the strongest compression and requires the most processing time.
The following example saves a presentation without compression:
#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();
The following example uses the maximum compression level:
#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();
Save Presentations without Refreshing the Thumbnail
When a presentation is saved as PPTX, PptxOptions::set_RefreshThumbnail controls its document thumbnail:
trueregenerates the thumbnail during the save operation. This is the default value.falsepreserves the existing thumbnail. If the presentation has no thumbnail, Aspose.Slides does not generate one.
The following example saves a presentation without refreshing its 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();
Note
Disabling thumbnail refresh can reduce the time required to save a PPTX file.Save Progress Updates in Percentage
To monitor a save operation, implement the IProgressCallback interface and pass the implementation to ISaveOptions::set_ProgressCallback. Aspose.Slides then calls IProgressCallback::Reporting with progress values during the export.
The following example reports the progress of a PDF export to the 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();
Note
Aspose provides a free PowerPoint Splitter built with the Aspose.Slides API. It saves selected slides from a presentation as separate PPT or PPTX files.FAQ
Does Aspose.Slides support incremental or “fast save”?
No. Each save operation writes a complete output file rather than updating only the changed parts.
Can multiple threads save the same Presentation instance?
No. A Presentation instance is not thread-safe. Access and save each instance from only one thread at a time.
What happens to hyperlinks and externally linked files when I save a presentation?
Hyperlinks remain in the presentation. Aspose.Slides does not copy externally linked files, so the saved presentation must still be able to access their locations.
Can I save document metadata such as the author, title, company, and creation date?
Yes. Set the appropriate document properties before saving, and Aspose.Slides writes them to the output file.