حدد خيارات الحفظ
عند حفظ مستند، يمكنك تعيين بعض الخصائص المتقدمة. يوفر لك Aspose.Words فئة SaveOptions، والتي تتيح تحكمًا أكثر دقة في عملية الحفظ. توجد عمليات تحميل زائدة لأسلوب Save تقبل كائن SaveOptions - يجب أن يكون كائنًا من فئة مشتقة من فئة SaveOptions. يحتوي كل تنسيق حفظ على فئة مقابلة تحتوي على خيارات الحفظ لتنسيق الحفظ هذا، على سبيل المثال، يوجد PdfSaveOptions للحفظ بتنسيق PDF، أو MarkdownSaveOptions للحفظ بتنسيق Markdown، أو 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، استخدم خاصية Password لفئة DocSaveOptions.
يوضح مثال التعليمات البرمجية التالي كيفية تعيين كلمة مرور لتشفير مستند باستخدام طريقة التشفير 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، استخدم خاصية Password لفئة OdtSaveOptions.
يوضح مثال التعليمات البرمجية التالي كيفية تحميل 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; | |
} |
احفظ الصورة بالأبيض والأسود بتنسيق بت واحد لكل بكسل
للتحكم في خيارات حفظ الصورة، يتم استخدام فئة ImageSaveOptions. على سبيل المثال، يمكنك استخدام خاصية PixelFormat لتعيين تنسيق البكسل للصور التي تم إنشاؤها. يرجى ملاحظة أن تنسيق البكسل للصورة الناتجة قد يختلف عن القيمة المحددة بسبب عمل GDI+.
يوضح مثال التعليمات البرمجية التالي كيفية حفظ صورة بالأبيض والأسود بتنسيق بت واحد لكل بكسل:
// 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); |