אפשרויות טעינה
בעת טעינת מסמך, אתה יכול להגדיר כמה תכונות מתקדמות. 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-Java | |
// Specify load option to specify MS Word version | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.setMswVersion(MsWordVersion.WORD_2003); | |
Document doc = new Document(dataDir + "document.doc", loadOptions); | |
doc.save(dataDir + "Word2003_out.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-Java | |
// Specify LoadOptions to add Editing Language | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.getLanguagePreferences().addEditingLanguage(EditingLanguage.JAPANESE); | |
Document doc = new Document(dataDir + "languagepreferences.docx", loadOptions); | |
int localeIdFarEast = doc.getStyles().getDefaultFont().getLocaleIdFarEast(); | |
if (localeIdFarEast == (int) EditingLanguage.JAPANESE) | |
System.out.println("The document either has no any FarEast language set in defaults or it was set to Japanese originally."); | |
else | |
System.out.println("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-Java | |
private static class DocumentLoadingWarningCallback implements IWarningCallback { | |
public void warning(WarningInfo info) { | |
// Prints warnings and their details as they arise during document loading. | |
System.out.println("WARNING: " + info.getWarningType() + " source:" + info.getSource()); | |
System.out.println("\tDescription: " + info.getDescription()); | |
} | |
} |
כדי לקבל מידע על כל הבעיות לאורך זמן העומס, השתמש WarningCallback רכוש.
דוגמה לקוד הבא מראה כיצד להשתמש בנכס זה:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Create a new LoadOptions object and set its WarningCallback property. | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.setWarningCallback(new DocumentLoadingWarningCallback()); | |
Document doc = new Document(dataDir + "input.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-Java | |
private static class HtmlLinkedResourceLoadingCallback implements IResourceLoadingCallback { | |
public int resourceLoading(ResourceLoadingArgs args) throws Exception { | |
switch (args.getResourceType()) { | |
case ResourceType.CSS_STYLE_SHEET: { | |
System.out.println("External CSS Stylesheet found upon loading: " + args.getOriginalUri()); | |
// CSS file will don't used in the document | |
return ResourceLoadingAction.SKIP; | |
} | |
case ResourceType.IMAGE: { | |
// Replaces all images with a substitute | |
String newImageFilename = "Logo.jpg"; | |
System.out.println("\tImage will be substituted with: " + newImageFilename); | |
BufferedImage newImage = ImageIO | |
.read(new File(Utils.getDataDir(LoadOptionsCallbacks.class) + newImageFilename)); | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
ImageIO.write(newImage, "jpg", baos); | |
baos.flush(); | |
byte[] imageBytes = baos.toByteArray(); | |
baos.close(); | |
args.setData(imageBytes); | |
// New images will be used instead of presented in the document | |
return ResourceLoadingAction.USER_PROVIDED; | |
} | |
case ResourceType.DOCUMENT: { | |
System.out.println("External document found upon loading: " + args.getOriginalUri()); | |
// Will be used as usual | |
return ResourceLoadingAction.DEFAULT; | |
} | |
default: | |
throw new Exception("Unexpected ResourceType value."); | |
} | |
} | |
} |
דוגמה לקוד הבא מראה כיצד להשתמש ResourceLoadingCallback רכוש:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Create a new LoadOptions object and set its ResourceLoadingCallback attribute | |
// as an instance of our IResourceLoadingCallback implementation | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.setResourceLoadingCallback(new 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 | |
Document doc = new Document(dataDir + "Images.html", loadOptions); | |
doc.save(dataDir + "Document.LoadOptionsCallback_out.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-Java | |
// Specify LoadOptions to set Temp Folder | |
LoadOptions lo = new LoadOptions(); | |
lo.setTempFolder("C:\\TempFolder\\"); | |
Document doc = new Document(dataDir + "document.doc", lo); |
תגית: 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-Java | |
// Set the Encoding attribute in a LoadOptions object to override the | |
// automatically chosen encoding with the one we know to be correct | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.setEncoding(java.nio.charset.Charset.forName("UTF-8")); | |
Document doc = new Document(dataDir + "Encoded in UTF-8.txt", loadOptions); |
מסמכים מוצפנים
אתה יכול לטעון מסמכי Word מוצפנים עם סיסמה. כדי לעשות זאת, השתמש עומס בנייה מיוחד, אשר מקבל תוספת LoadOptions אובייקט אובייקט זה מכיל Password רכוש, המפרט את מחרוזת הסיסמה.
הדוגמה הבאה של הקוד מראה כיצד לטעון מסמך מוצפן עם סיסמה:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// For complete examples and data files, please go to | |
// https://github.com/aspose-words/Aspose.Words-for-Java | |
// Load the encrypted document from the absolute path on disk. | |
Document doc = new Document(dataDir + "LoadEncrypted.docx", new LoadOptions("aspose")); |
אם אתה לא יודע מראש אם הקובץ מוצפן, אתה יכול להשתמש FileFormatUtil הכיתה, המספקת שיטות שימושיות לעבודה עם פורמטי קובץ, כגון זיהוי תבנית הקובץ או המרת הרחבות קבצים ל / מתבניות תבנית הקובץ. כדי לזהות אם המסמך מוצפן ודורש סיסמה כדי לפתוח אותו, השתמש באפשרות IsEncrypted רכוש.
הדוגמה הקודית הבאה מראה כיצד לאמת את OpenDocument או שהיא מוצפנת או לא:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
FileFormatInfo info = FileFormatUtil.detectFileFormat(dataDir + "encrypted.odt"); | |
System.out.println(info.isEncrypted()); |