ระบุตัวเลือกการโหลด
เมื่อโหลดเอกสารคุณสามารถตั้งค่าคุณสมบัติขั้นสูงบางอย่างได้ Aspose.Wordsช่วยให้คุณมีLoadOptionsชั้นซึ่งจะช่วยให้การควบคุมที่แม่นยำมากขึ้นของกระบวนการโหลด รูปแบบการโหลดบางรูปแบบมีคลาสที่สอดคล้องกันซึ่งมีตัวเลือกการโหลดสำหรับรูปแบบการโหลดนี้ตัวอย่างเช่นมีPdfLoadOptionsสำหรับการโหลดไปยังรูปแบบPDFหรือTxtLoadOptionsสำหรับการโหลดไปยังTXT บทความนี้แสดงตัวอย่างของการทำงานกับตัวเลือกของLoadOptionsคลาส.
ตั้งค่าเวอร์ชันMicrosoft Wordเพื่อเปลี่ยนลักษณะที่ปรากฏ
รุ่นที่แตกต่างกันของโปรแกรมMicrosoft Wordสามารถแสดงเอกสารไม่แยแส ตัวอย่างเช่นมีปัญหาที่รู้จักกันดีกับเอกสารOOXMLเช่นDOCXหรือDOTXที่ผลิตโดยใช้สำนักงานWPS ในกรณีดังกล่าวองค์ประกอบมาร์กอัปเอกสารที่จำเป็นอาจขาดหายไปหรืออาจถูกตีความแตกต่างออกไปทำให้Microsoft Word2019 แสดงเอกสารดังกล่าวแตกต่างกันเมื่อเทียบกับMicrosoft Word2010.
โดยค่าเริ่มต้นAspose.Wordsเปิดเอกสารโดยใช้กฎMicrosoft Word2019 หากคุณต้องการทำให้การโหลดเอกสารปรากฏขึ้นตามที่ปรากฏในแอปพลิเคชันรุ่นก่อนหน้าMicrosoft Wordคุณควรระบุเวอร์ชันที่ต้องการอย่างชัดเจนโดยใช้คุณสมบัติของMswVersionของคลาสของLoadOptions.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการตั้งค่าเวอร์ชันMicrosoft Wordด้วยตัวเลือกการโหลด:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>(); | |
// Change the loading version to Microsoft Word 2010. | |
loadOptions->set_MswVersion(MsWordVersion::Word2010); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Document.docx", loadOptions); | |
System::String outputPath = outputDataDir + u"Load_Options.SetMSWordVersion.docx"; | |
doc->Save(outputPath); |
ตั้งค่าการตั้งค่าภาษาเพื่อเปลี่ยนลักษณะที่ปรากฏ
รายละเอียดของการแสดงเอกสารในMicrosoft Wordไม่เพียงแต่ขึ้นอยู่กับรุ่นของโปรแกรมประยุกต์และค่าคุณสมบัติMswVersionแต่ยังเกี่ยวกับการตั้งค่าภาษาอีกด้วย Microsoft Wordอาจแสดงเอกสารที่แตกต่างกันขึ้นอยู่กับการตั้งค่า"การตั้งค่าภาษาสำนักงาน"ซึ่งสามารถพบได้ใน"ตัวเลือกไฟล์ Language ภาษา" ใช้กล่องโต้ตอบนี้ผู้ใช้สามารถเลือกเช่นภาษาหลักภาษาพิสูจน์อักษรภาษาที่แสดงและอื่นๆ Aspose.Wordsให้คุณสมบัติของLanguagePreferencesเทียบเท่ากับกล่องโต้ตอบนี้ ถ้าผลลัพธ์Aspose.Wordsแตกต่างจากผลลัพธ์Microsoft Wordให้ตั้งค่าที่เหมาะสมสำหรับEditingLanguageซึ่งจะสามารถปรับปรุงเอกสารที่ส่งออกได้.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการตั้งค่าภาษาญี่ปุ่นเป็นEditingLanguage:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// Create a new LoadOptions object. | |
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>(); | |
// Set language preferences that will be used when document is loading. | |
loadOptions->get_LanguagePreferences()->AddEditingLanguage(EditingLanguage::Japanese); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"languagepreferences.docx", loadOptions); |
ใช้WarningCallbackเพื่อควบคุมปัญหาขณะโหลดเอกสาร
เอกสารบางอย่างอาจเสียหายมีรายการที่ไม่ถูกต้องหรือมีคุณลักษณะที่ไม่ได้รับการสนับสนุนในปัจจุบันโดยAspose.Words ถ้าคุณต้องการทราบเกี่ยวกับปัญหาที่เกิดขึ้นขณะโหลดเอกสารAspose.Wordsจะมีอินเทอร์เฟซIWarningCallback.
ตัวอย่างรหัสต่อไปนี้แสดงการใช้งานของอินเทอร์เฟซIWarningCallback:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
class DocumentLoadingWarningCallback : public IWarningCallback | |
{ | |
public: | |
System::SharedPtr<WarningInfoCollection> mWarnings; | |
void Warning(System::SharedPtr<WarningInfo> info) override; | |
DocumentLoadingWarningCallback(); | |
}; | |
void DocumentLoadingWarningCallback::Warning(System::SharedPtr<WarningInfo> info) | |
{ | |
// Prints warnings and their details as they arise during document loading. | |
std::cout << "WARNING: {info->get_WarningType} " << std::endl; | |
std::cout << "Source: {info->get_Source} " << std::endl; | |
std::cout << "\tDescription: {info->get_Description} " << std::endl; | |
} | |
DocumentLoadingWarningCallback::DocumentLoadingWarningCallback() : mWarnings(System::MakeObject<WarningInfoCollection>()) | |
{ | |
} |
เมื่อต้องการรับข้อมูลเกี่ยวกับปัญหาทั้งหมดตลอดเวลาในการโหลดให้ใช้คุณสมบัติWarningCallback
.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีใช้คุณสมบัตินี้:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// Create a new LoadOptions object and set its WarningCallback property. | |
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>(); | |
System::SharedPtr<DocumentLoadingWarningCallback> callback = System::MakeObject<DocumentLoadingWarningCallback>(); | |
loadOptions->set_WarningCallback(callback); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Document.docx", loadOptions); |
ใช้ResourceLoadingCallbackเพื่อควบคุมการโหลดทรัพยากรภายนอก
เอกสารอาจมีลิงก์ภายนอกไปยังรูปภาพที่ตั้งอยู่ที่ใดที่หนึ่งบนดิสก์ภายในเครือข่ายหรืออินเท Aspose.Wordsโหลดภาพดังกล่าวลงในเอกสารโดยอัตโนมัติแต่มีสถานการณ์เมื่อกระบวนการนี้จะต้องถูกควบคุม ตัวอย่างเช่นในการตัดสินใจว่าเราจริงๆต้องโหลดภาพบางอย่างหรือบางทีอาจจะข้ามมัน ตัวเลือกการโหลดResourceLoadingCallbackช่วยให้คุณสามารถควบคุมสิ่งนี้ได้.
ตัวอย่างรหัสต่อไปนี้แสดงการใช้งานของอินเทอร์เฟซIResourceLoadingCallback:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
class HtmlLinkedResourceLoadingCallback : public IResourceLoadingCallback | |
{ | |
public: | |
ResourceLoadingAction ResourceLoading(System::SharedPtr<ResourceLoadingArgs> args) override; | |
HtmlLinkedResourceLoadingCallback(); | |
}; | |
HtmlLinkedResourceLoadingCallback::HtmlLinkedResourceLoadingCallback() | |
{ | |
} | |
ResourceLoadingAction HtmlLinkedResourceLoadingCallback::ResourceLoading(System::SharedPtr<ResourceLoadingArgs> args) | |
{ | |
switch (args->get_ResourceType()) | |
{ | |
case ResourceType::CssStyleSheet: | |
{ | |
std::cout << "External CSS Stylesheet found upon loading: " << args->get_OriginalUri().ToUtf8String() << std::endl; | |
// CSS file will don't used in the document | |
return ResourceLoadingAction::Skip; | |
} | |
case ResourceType::Image: | |
{ | |
// Replaces all images with a substitute | |
System::String newImageFilename = u"Logo.jpg"; | |
std::cout << "\tImage will be substituted with: " << newImageFilename.ToUtf8String() << std::endl; | |
System::SharedPtr<System::Drawing::Image> newImage = System::Drawing::Image::FromFile(GetInputDataDir_LoadingAndSaving() + newImageFilename); | |
System::SharedPtr<System::Drawing::ImageConverter> converter = System::MakeObject<System::Drawing::ImageConverter>(); | |
auto imageBytes = System::DynamicCast<System::Array<uint8_t>>(converter->ConvertTo(nullptr, nullptr, newImage, System::ObjectExt::GetType<System::Array<uint8_t>>())); | |
//System::ArrayPtr<uint8_t> imageBytes = System::IO::File::ReadAllBytes(GetInputDataDir_LoadingAndSaving() + newImageFilename); | |
args->SetData(imageBytes); | |
// New images will be used instead of presented in the document | |
return ResourceLoadingAction::UserProvided; | |
} | |
case ResourceType::Document: | |
{ | |
std::cout << "External document found upon loading: " << args->get_OriginalUri().ToUtf8String() << std::endl; | |
// Will be used as usual | |
return ResourceLoadingAction::Default; | |
} | |
default: | |
throw System::InvalidOperationException(u"Unexpected ResourceType value."); | |
} | |
} | |
ตัวอย่างรหัสต่อไปนี้แสดงวิธีใช้คุณสมบัติResourceLoadingCallback:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// Create a new LoadOptions object and set its ResourceLoadingCallback attribute as an instance of our IResourceLoadingCallback implementation | |
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>(); | |
loadOptions->set_ResourceLoadingCallback(System::MakeObject<HtmlLinkedResourceLoadingCallback>()); | |
// When we open an Html document, external resources such as references to CSS stylesheet files and external images | |
// will be handled in a custom manner by the loading callback as the document is loaded | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Images.html", loadOptions); | |
doc->Save(outputDataDir + u"Load_Options.LoadOptionsResourceLoadingCallback.pdf"); |
ใช้TempFolderเพื่อหลีกเลี่ยงการยกเว้นหน่วยความจำ
Aspose.Wordsรองรับเอกสารที่มีขนาดใหญ่มากที่มีหลายพันหน้าเต็มไปด้วยเนื้อหาที่อุดมไปด้วย การโหลดเอกสารดังกล่าวอาจต้องใช้มากRAM ในกระบวนการโหลดAspose.Wordsต้องการหน่วยความจำมากขึ้นเพื่อเก็บโครงสร้างชั่วคราวที่ใช้ในการแยกเอกสาร.
หากคุณมีปัญหากับข้อยกเว้นจากหน่วยความจำขณะโหลดเอกสารให้ลองใช้คุณสมบัติTempFolder ในกรณีนี้Aspose.Wordsจะจัดเก็บข้อมูลบางส่วนไว้ในแฟ้มชั่วคราวแทนที่จะเป็นหน่วยความจำและจะช่วย.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการตั้งค่าTempFolder:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>(); | |
loadOptions->set_TempFolder(u"C:/TempFolder/"); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Document.docx", loadOptions); |
ตั้งค่าการเข้ารหัสอย่างชัดเจน
รูปแบบเอกสารที่ทันสมัยที่สุดจัดเก็บเนื้อหาของพวกเขาในยูนิโค้ดและไม่จำเป็นต้องมีกา บทความนี้ไม่มีการอ้างอิงจากเอกสารอ้างอิงหรือแหล่งข้อมูลโปรดช่วยพัฒนาบทความนี้โด Aspose.Wordsพยายามตรวจสอบการเข้ารหัสที่เหมาะสมโดยอัตโนมัติโดยค่าเริ่มต้น,แต่ในกรณีที่หายาก,คุณอาจจำเป็นต้องใช้การเข้ารหัสที่แตกต่างจากที่ตรวจพบโดยขั้นตอนวิธีการรับรู้การเข้ารหัสของเรา. ในกรณีนี้ให้ใช้คุณสมบัติEncodingเพื่อรับหรือตั้งค่าการเข้ารหัส.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการตั้งค่าการเข้ารหัสเพื่อแทนที่การเข้ารหัสที่เลือกโดยอัตโน:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// Set the Encoding attribute in a LoadOptions object to override the automatically chosen encoding with the one we know to be correct | |
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>(); | |
loadOptions->set_Encoding(System::Text::Encoding::get_UTF7()); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Encoded in UTF-7.txt", loadOptions); |
โหลดเอกสารที่เข้ารหัส
คุณสามารถป้อนเอกสารคำที่เข้ารหัสด้วยรหัสผ่าน การทำเช่นนี้ให้ใช้ตัวสร้างพิเศษเกินพิกัดซึ่งยอมรับวัตถุLoadOptions อ็อบเจ็กต์นี้ประกอบด้วยคุณสมบัติPasswordซึ่งระบุสตริงรหัสผ่าน.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการโหลดเอกสารที่เข้ารหัสด้วยรหัสผ่าน:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// The path to the documents directory. | |
System::String dataDir = GetInputDataDir_LoadingAndSaving(); | |
// Loads encrypted document. | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(dataDir + u"LoadEncrypted.docx", System::MakeObject<LoadOptions>(u"aspose")); | |
หากคุณไม่ทราบล่วงหน้าว่าแฟ้มถูกเข้ารหัสหรือไม่คุณสามารถใช้คลาสของFileFormatUtilซึ่งมีวิธีการอรรถประโยชน์สำหรับการทำงานกับรูปแบบแฟ้มเช่นการตรวจหารูปแบบแฟ้มหรือการแปลงนามสกุลแฟ้มไปยัง/จากการแจงนับรูปแบบแฟ้ม เมื่อต้องการตรวจสอบว่าเอกสารถูกเข้ารหัสและต้องการรหัสผ่านเพื่อเปิดหรือไม่ให้ใช้คุณสมบัติIsEncrypted
.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการตรวจสอบOpenDocumentไม่ว่าจะเป็นรหัสลับหรือไม่:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<FileFormatInfo> info = FileFormatUtil::DetectFileFormat(inputDataDir + u"encrypted.odt"); | |
std::cout << info->get_IsEncrypted() << std::endl; |