ระบุตัวเลือกการบันทึก
เมื่อบันทึกเอกสาร คุณสามารถตั้งค่าคุณสมบัติขั้นสูงบางอย่างได้ 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); |