ציין אפשרויות טעינה

בעת טעינת מסמך, באפשרותך להגדיר כמה מאפיינים מתקדמים. Aspose.Words מספק לך את הכיתה LoadOptions, המאפשרת שליטה מדויקת יותר בתהליך העומס. בחלק מהפורמטים של עומס יש מחלקה מתאימה שמכילה אפשרויות עומס עבור פורמט עומס זה, לדוגמה, יש PdfLoadOptions לטעינה ל PDF פורמט או TxtLoadOptions לטעינה ל TXT. מאמר זה מספק דוגמאות לעבודה עם אפשרויות של הכיתה LoadOptions.

הגדר Microsoft Word גרסה כדי לשנות את המראה

גרסאות שונות של היישום Microsoft Word יכולות להציג מסמכים באדישות. לדוגמה, יש בעיה ידועה עם OOXML מסמכים כגון DOCX או DOTX המיוצר באמצעות WPS משרד. במקרה כזה, אלמנטים חיוניים של סימון מסמכים עשויים להיות חסרים או עשויים להיות מפורשים באופן שונה, מה שגורם ל Microsoft Word 2019 להציג מסמך כזה באופן שונה בהשוואה ל Microsoft Word 2010.

כברירת מחדל Aspose.Words פותח מסמכים באמצעות Microsoft Word 2019 כללים. אם אתה צריך לעשות זאת כדי לגרום לטעינת המסמך להופיע כפי שהיה קורה באחת מגרסאות היישום הקודמות 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 עשוי להציג מסמכים באופן שונה בהתאם להגדרות הדו-שיח “העדפות שפת משרד”, שניתן למצוא ב"קובץ אפשרויות שפות". באמצעות תיבת דו-שיח זו משתמש יכול לבחור, למשל, שפה ראשית, שפות הגהה, שפות תצוגה וכן הלאה. 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);

טען מסמכים מוצפנים

ניתן לטעון Word מסמכים מוצפנים באמצעות סיסמה. לשם כך, השתמש בעומס קונסטרוקטור מיוחד, המקבל אובייקט 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;