保存オプションの指定

ドキュメントを保存するときに、いくつかの高度なプロパティを設定できます。 Aspose.Wordsは、保存プロセスのより正確な制御を可能にするSaveOptionsクラスを提供します。 SaveOptionsオブジェクトを受け入れるSaveメソッドのオーバーロードがあります–それはSaveOptionsクラスから派生したクラスのオブジェクトでなければなりません。 たとえば、PDF形式に保存する場合はPdfSaveOptions、Markdown形式に保存する場合はMarkdownSaveOptions、または画像に保存する場合はImageSaveOptionsがあります。PDF形式に保存する場合はPdfSaveOptions、PDF形式に保存す この記事では、SaveOptionsから派生したいくつかのオプションクラスを使用する例を示します。

次のコード例は、ドキュメントをHTMLに保存する前に保存オプションを設定する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_LoadingAndSaving();
System::String outputDataDir = GetOutputDataDir_LoadingAndSaving();
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile RenderShape.docx");
// This is the directory we want the exported images to be saved to.
System::String imagesDir = System::IO::Path::Combine(outputDataDir, u"SpecifySaveOption.Images");
// The folder specified needs to exist and should be empty.
if (System::IO::Directory::Exists(imagesDir))
{
System::IO::Directory::Delete(imagesDir, true);
}
System::IO::Directory::CreateDirectory_(imagesDir);
// Set an option to export form fields as plain text, not as HTML input elements.
System::SharedPtr<HtmlSaveOptions> options = System::MakeObject<HtmlSaveOptions>(SaveFormat::Html);
options->set_ExportTextInputFormFieldAsText(true);
options->set_ImagesFolder(imagesDir);
System::String outputPath = outputDataDir + u"SpecifySaveOption.html";
doc->Save(outputPath, options);

この記事では、ドキュメントを保存するときに制御できるいくつかのプロパティについて説明します。

パスワードで文書を暗号化する

暗号化された文書のパスワードを取得または設定するには、Passwordプロパティを使用します。 対応するクラスのPasswordプロパティを使用して、選択したドキュメント形式を操作します。

たとえば、ドキュメントをDOCまたはDOT形式で保存する場合は、DocSaveOptionsクラスのPasswordプロパティを使用します。

次のコード例は、RC4暗号化メソッドを使用してドキュメントを暗号化するためのパスワードを設定する方法を示しています:

ドキュメントをODT形式で保存する場合は、OdtSaveOptionsクラスのPasswordプロパティを使用します。

次のコード例は、パスワードで暗号化されたOpenDocumentを読み込んで保存する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"encrypted.odt", System::MakeObject<LoadOptions>(u"password"));
System::String outputPath = outputDataDir + u"Load_Options.LoadAndSaveEncryptedODT.odt";
doc->Save(outputPath, System::MakeObject<OdtSaveOptions>(u"newpassword"));

すべての形式が暗号化とPasswordプロパティの使用をサポートしているわけではありません。

ドキュメント保存の進行状況通知を表示する

Aspose.Wordsは、ProgressCallbackプロパティを使用して、ドキュメントの保存の進行状況に関する通知を取得する機能を提供します。

に保存するときに利用できるようになりましたDOCX, FlatOpc, DOCM, DOTM, DOTX, HTML、MHTML、EPUB、XamlFlow、XamlFlowPack、またはTXT形式。

文書の作成時間を更新する

Aspose.Wordsは、CreatedTimeプロパティを使用してutcでドキュメント作成日を取得または設定する機能を提供します。 UpdateCreatedTimePropertyオプションを使用して保存する前にこの値を更新することもできます。

次のコード例は、ドキュメントの作成時間を更新する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Open a document
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.docx");
System::SharedPtr<SaveOptions> options = System::MakeObject<PdfSaveOptions>();
options->set_UpdateLastPrintedProperty(false);
System::String outputPath = outputDataDir + u"WorkingWithPdfSaveOptions.UpdateIfLastPrinted.pdf";
doc->Save(outputPath, options);

最後に保存したプロパティを更新する

Aspose.Wordsは、UpdateLastSavedTimePropertyプロパティを使用して、LastSavedTimeプロパティが保存前に更新されるかどうかを決定する値を取得または設定する機能を提供します。

次のコード例は、このプロパティを設定してドキュメントを保存する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Document.doc");
System::SharedPtr<OoxmlSaveOptions> ooxmlSaveOptions = System::MakeObject<OoxmlSaveOptions>();
ooxmlSaveOptions->set_UpdateLastSavedTimeProperty(true);
System::String outputPath = outputDataDir + u"WorkingWithOoxml.UpdateLastSavedTimeProperty.docx";
// Save the document to disk.
doc->Save(outputPath, ooxmlSaveOptions);

白黒画像を1ピクセルあたり1ビットの形式で保存する

画像保存オプションを制御するには、ImageSaveOptionsクラスが使用されます。 たとえば、PixelFormatプロパティを使用して、生成された画像のピクセル形式を設定できます。 出力画像のピクセル形式は、GDI+の作業のために設定値と異なる場合がありますのでご注意ください。

次のコード例は、白黒画像をピクセルごとに1ビットの形式で保存する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<ImageSaveOptions> opt = System::MakeObject<ImageSaveOptions>(SaveFormat::Png);
auto pageSet = System::MakeObject<PageSet>(System::MakeArray<System::SharedPtr<PageRange>>({System::MakeObject<PageRange>(1,1)}));
opt->set_PageSet(pageSet);
opt->set_ImageColorMode(ImageColorMode::BlackAndWhite);
opt->set_PixelFormat(ImagePixelFormat::Format1bppIndexed);
System::String outputPath = outputDataDir + u"ImageColorFilters.SaveImageToOnebitPerPixel.png";
doc->Save(outputPath, opt);