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

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

Set Microsoft Word גרסה לשינוי המראה

גרסאות שונות של Microsoft Word יישום יכול להציג מסמכים באופן שונה. לדוגמה, יש בעיה ידועה עם מסמכי OOXML כגון DOCX או DOCX. DOTX מיוצר באמצעות WPS Office. במקרה כזה, רכיבי סימון מסמכים חיוניים עשויים להיות חסרים או עשויים להיות מפורשים באופן שונה. Microsoft Word 2019 להציג מסמך כזה שונה בהשוואה Microsoft Word 2010 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-.NET
// Create a new LoadOptions object, which will load documents according to MS Word 2019 specification by default
// and change the loading version to Microsoft Word 2010.
LoadOptions loadOptions = new LoadOptions { MswVersion = MsWordVersion.Word2010 };
Document doc = new Document(MyDir + "Document.docx", loadOptions);
doc.Save(ArtifactsDir + "WorkingWithLoadOptions.SetMsWordVersion.docx");

הגדרת העדפות שפה כדי לשנות את המראה

פרטים על הצגת מסמך Microsoft Word תלוי לא רק בגרסת היישום ובגרסה MswVersion ערך רכוש אך גם על הגדרות השפה. Microsoft Word עשוי להראות מסמכים באופן שונה בהתאם להגדרות הדו-שיח “שפת Office” שניתן למצוא ב-“File , Options - Languаge”. באמצעות דיאלוג זה משתמש יכול לבחור, למשל, שפה ראשית, הוכחת שפות, להציג שפות וכן הלאה. 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-.NET
LoadOptions loadOptions = new LoadOptions();
// Set language preferences that will be used when document is loading.
loadOptions.LanguagePreferences.AddEditingLanguage(EditingLanguage.Japanese);
Document doc = new Document(MyDir + "No default editing language.docx", loadOptions);
int localeIdFarEast = doc.Styles.DefaultFont.LocaleIdFarEast;
Console.WriteLine(
localeIdFarEast == (int)EditingLanguage.Japanese
? "The document either has no any FarEast language set in defaults or it was set to Japanese originally."
: "The document default FarEast language was set to another than Japanese language originally, so it is not overridden.");

שימוש בשימוש WarningCallback בעיות שליטה תוך שמירה על מסמך

מסמכים מסוימים עשויים להיות מושחתים, מכילים ערכים לא חוקיים, או שיש להם תכונות שאינן נתמכות כיום על ידי Aspose.Words. אם אתה רוצה לדעת על בעיות שהתרחשו בעת טעינת מסמך, Aspose.Words מספק IWarningCallback ממשק.

הדוגמה הבאה מציגה את יישום IWarningCallback ממשק:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public class DocumentLoadingWarningCallback : IWarningCallback
{
public void Warning(WarningInfo info)
{
// Prints warnings and their details as they arise during document loading.
Console.WriteLine($"WARNING: {info.WarningType}, source: {info.Source}");
Console.WriteLine($"\tDescription: {info.Description}");
}
}

כדי לקבל מידע על כל הבעיות לאורך זמן העומס, השתמש WarningCallback רכוש.

דוגמה לקוד הבא מראה כיצד להשתמש בנכס זה:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
LoadOptions loadOptions = new LoadOptions { WarningCallback = new DocumentLoadingWarningCallback() };
Document doc = new Document(MyDir + "Document.docx", loadOptions);

שימוש ב-Loading קריאה לשליטה במשאבים החיצוניים

מסמך עשוי להכיל קישורים חיצוניים לתמונות הנמצאות במקום כלשהו בדיסק מקומי, ברשת או באינטרנט. Aspose.Words באופן אוטומטי נטען תמונות כאלה במסמך, אך יש מצבים שבהם יש לשלוט בתהליך זה. לדוגמה, כדי להחליט אם אנחנו באמת צריכים לטעון תמונה מסוימת או אולי לדלג עליה. The The The ResourceLoadingCallback אפשרות לטעון מאפשרת לך לשלוט בזה.

הדוגמה הבאה מציגה את יישום IResourceLoadingCallback ממשק:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
private class HtmlLinkedResourceLoadingCallback : IResourceLoadingCallback
{
public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args)
{
switch (args.ResourceType)
{
case ResourceType.CssStyleSheet:
{
Console.WriteLine($"External CSS Stylesheet found upon loading: {args.OriginalUri}");
// CSS file will don't used in the document.
return ResourceLoadingAction.Skip;
}
case ResourceType.Image:
{
// Replaces all images with a substitute.
Image newImage = Image.FromFile(ImagesDir + "Logo.jpg");
ImageConverter converter = new ImageConverter();
byte[] imageBytes = (byte[])converter.ConvertTo(newImage, typeof(byte[]));
args.SetData(imageBytes);
// New images will be used instead of presented in the document.
return ResourceLoadingAction.UserProvided;
}
case ResourceType.Document:
{
Console.WriteLine($"External document found upon loading: {args.OriginalUri}");
// Will be used as usual.
return ResourceLoadingAction.Default;
}
default:
throw new InvalidOperationException("Unexpected ResourceType value.");
}
}
}

דוגמה לקוד הבא מראה כיצד להשתמש ResourceLoadingCallback רכוש:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
LoadOptions loadOptions = new LoadOptions { ResourceLoadingCallback = new HtmlLinkedResourceLoadingCallback() };
// When we open an Html document, external resources such as references to CSS stylesheet files
// and external images will be handled customarily by the loading callback as the document is loaded.
Document doc = new Document(MyDir + "Images.html", loadOptions);
doc.Save(ArtifactsDir + "WorkingWithLoadOptions.ResourceLoadingCallback.pdf");

השתמש טמפלר כדי להימנע מפרש זיכרון

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-.NET
LoadOptions loadOptions = new LoadOptions { TempFolder = ArtifactsDir };
Document doc = new Document(MyDir + "Document.docx", loadOptions);

תגית: Encoding Explicitly

רוב פורמטי המסמך המודרניים מאחסנים את התוכן שלהם Unicode ואינם דורשים טיפול מיוחד. מצד שני, יש עדיין מסמכים רבים המשתמשים בקידוד pre-Unicode ולעיתים גם מפספסים מידע קידודי או אפילו לא תומכים במידע על ידי הטבע. Aspose.Words מנסה לזהות באופן אוטומטי את הקידוד המתאים כברירת מחדל, אבל במקרה נדיר ייתכן שיהיה עליך להשתמש באנתרופולוגיה שונה מזו מזוהה על ידי אלגוריתם ההכרה שלנו. במקרה זה, השתמש Encoding לרכוש או להגדיר את הקידוד.

דוגמה הקוד הבא מראה כיצד להגדיר את הקידוד כדי לעקוף את הקידוד שנבחר באופן אוטומטי:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
LoadOptions loadOptions = new LoadOptions { Encoding = Encoding.UTF7 };
Document doc = new Document(MyDir + "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-.NET
Document doc = new Document(MyDir + "Encrypted.docx", new LoadOptions("docPassword"));

אם אתה לא יודע מראש אם הקובץ מוצפן, אתה יכול להשתמש FileFormatUtil מעמד, המספק שיטות שימושיות לעבודה עם פורמטים קובץ, כגון זיהוי תבנית הקובץ או המרת הרחבות קבצים ל / מתבניות תבנית הקובץ. כדי לזהות אם המסמך מוצפן ודורש סיסמה כדי לפתוח אותו, השתמש באפשרות IsEncrypted רכוש.

הדוגמה הקודית הבאה מראה כיצד לאמת את OpenDocument או שהיא מוצפנת או לא:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
FileFormatInfo info = FileFormatUtil.DetectFileFormat(MyDir + "Encrypted.odt");
Console.WriteLine(info.IsEncrypted);