保存オプションの指定
ドキュメントを保存するときに、いくつかの詳細プロパティを設定できます。 Aspose.Words は、保存プロセスをより正確に制御できる SaveOptions クラスを提供します。 SaveOptions オブジェクトを受け入れる Save メソッドのオーバーロードがあります。これは、SaveOptions クラスから派生したクラスのオブジェクトである必要があります。各保存形式には、その保存形式の保存オプションを保持する対応するクラスがあります。たとえば、PDF 形式に保存する場合は PdfSaveOptions、Markdown 形式に保存する場合は MarkdownSaveOptions、画像に保存する場合は ImageSaveOptions があります。この記事では、SaveOptions から派生したいくつかのオプション クラスの操作例を示します。
次のコード例は、ドキュメントを HTML に保存する前に保存オプションを設定する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_LoadingAndSaving(); | |
string fileName = "TestFile RenderShape.docx"; | |
Document doc = new Document(dataDir + fileName); | |
// This is the directory we want the exported images to be saved to. | |
string imagesDir = Path.Combine(dataDir, "Images"); | |
// The folder specified needs to exist and should be empty. | |
if (Directory.Exists(imagesDir)) | |
Directory.Delete(imagesDir, true); | |
Directory.CreateDirectory(imagesDir); | |
// Set an option to export form fields as plain text, not as HTML input elements. | |
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html); | |
options.ExportTextInputFormFieldAsText = true; | |
options.ImagesFolder = imagesDir; | |
dataDir = dataDir + "Document.SaveWithOptions_out.html"; | |
doc.Save(dataDir, options); | |
この記事では、ドキュメントを保存するときに制御できるいくつかのプロパティについて説明します。
パスワードを使用してドキュメントを暗号化する
Password プロパティを使用して、暗号化されたドキュメントのパスワードを取得または設定します。選択したドキュメント形式を操作するには、対応するクラスの Password プロパティを使用します。
たとえば、ドキュメントを DOC または DOT 形式で保存する場合は、DocSaveOptions クラスの Password プロパティを使用します。
次のコード例は、RC4 暗号化方式を使用してドキュメントを暗号化するためのパスワードを設定する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + "Document.docx"); | |
DocSaveOptions docSaveOptions = new DocSaveOptions(); | |
docSaveOptions.Password = "password"; | |
dataDir = dataDir + "Document.Password_out.docx"; | |
doc.Save(dataDir, docSaveOptions); |
ドキュメントを Odt 形式で保存する場合は、OdtSaveOptions クラスの Password プロパティを使用します。
次のコード例は、パスワードで暗号化された OpenDocument をロードおよび保存する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + @"encrypted.odt", new Aspose.Words.LoadOptions("password")); | |
doc.Save(dataDir + "out.odt", new OdtSaveOptions("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-.NET | |
Document doc = new Document(MyDir + "Rendering.docx"); | |
PdfSaveOptions saveOptions = new PdfSaveOptions { UpdateLastPrintedProperty = true }; | |
doc.Save(ArtifactsDir + "WorkingWithPdfSaveOptions.UpdateIfLastPrinted.pdf", saveOptions); |
最後に保存したプロパティを更新する
Aspose.Words は、UpdateLastSavedTimeProperty プロパティを使用して、LastSavedTime プロパティが保存前に更新されるかどうかを決定する値を取得または設定する機能を提供します。
次のコード例は、このプロパティを設定してドキュメントを保存する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + "Document.docx"); | |
OoxmlSaveOptions ooxmlSaveOptions = new OoxmlSaveOptions(); | |
ooxmlSaveOptions.UpdateLastSavedTimeProperty = true; | |
dataDir = dataDir + "UpdateLastSavedTimeProperty_out.docx"; | |
// Save the document to disk. | |
doc.Save(dataDir, ooxmlSaveOptions); |
ドキュメントを HTML または SVG に保存するときに外部リソースを制御する
HTML または SVG を PDF に変換するには、Save メソッドを呼び出して、「.PDF」拡張子の付いたファイル名を指定するだけです。画像や CSS などを外部ソースから読み込みたい場合は、IResourceSavingCallback を使用できます。
次のコード例は、HTML を PDF に変換し、外部ソースから画像をロードする方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
public class ImageLoadingWithCredentialsHandler : IResourceLoadingCallback | |
{ | |
public ImageLoadingWithCredentialsHandler() | |
{ | |
mWebClient = new WebClient(); | |
} | |
public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args) | |
{ | |
if (args.ResourceType == ResourceType.Image) | |
{ | |
Uri uri = new Uri(args.Uri); | |
if (uri.Host == "www.aspose.com") | |
mWebClient.Credentials = new NetworkCredential("User1", "akjdlsfkjs"); | |
else | |
mWebClient.Credentials = new NetworkCredential("SomeOtherUserID", "wiurlnlvs"); | |
// Download the bytes from the location referenced by the URI. | |
byte[] imageBytes = mWebClient.DownloadData(args.Uri); | |
args.SetData(imageBytes); | |
return ResourceLoadingAction.UserProvided; | |
} | |
else | |
{ | |
return ResourceLoadingAction.Default; | |
} | |
} | |
private WebClient mWebClient; | |
} |
白黒画像を 1 ビット/ピクセル形式で保存する
画像保存オプションを制御するには、ImageSaveOptions クラスが使用されます。たとえば、PixelFormat プロパティを使用して、生成される画像のピクセル形式を設定できます。 GDI+の働きにより、出力画像のピクセル形式が設定値と異なる場合がありますのでご注意ください。
次のコード例は、白黒画像を 1 ピクセルあたり 1 ビットの形式で保存する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
ImageSaveOptions opt = new ImageSaveOptions(SaveFormat.Png); | |
PageSet pageSet = new PageSet(new PageRange(1, 1)); | |
opt.PageSet = pageSet; | |
opt.ImageColorMode = ImageColorMode.BlackAndWhite; | |
opt.PixelFormat = ImagePixelFormat.Format1bppIndexed; | |
dataDir = dataDir + "Format1bppIndexed_Out.Png"; | |
doc.Save(dataDir, opt); |