ระบุตัวเลือกการโหลด
เมื่อโหลดเอกสารคุณสามารถตั้งค่าคุณสมบัติขั้นสูงบางอย่างได้ 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-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อาจแสดงเอกสารที่แตกต่างกันขึ้นอยู่กับการตั้งค่า"การตั้งค่าภาษาสำนักงาน"ซึ่งสามารถพบได้ใน"ตัวเลือกไฟล์ 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-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); |
ใช้ResourceLoadingCallbackเพื่อควบคุมการโหลดทรัพยากรภายนอก
เอกสารอาจมีลิงก์ภายนอกไปยังรูปภาพที่ตั้งอยู่ที่ใดที่หนึ่งบนดิสก์ภายในเครือข่ายหรืออินเท Aspose.Wordsโหลดภาพดังกล่าวลงในเอกสารโดยอัตโนมัติแต่มีสถานการณ์เมื่อกระบวนการนี้จะต้องถูกควบคุม ตัวอย่างเช่นในการตัดสินใจว่าเราจริงๆต้องโหลดภาพบางอย่างหรือบางทีอาจจะข้ามมัน ตัวเลือกการโหลด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"); |
ใช้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-Java | |
// Specify LoadOptions to set Temp Folder | |
LoadOptions lo = new LoadOptions(); | |
lo.setTempFolder("C:\\TempFolder\\"); | |
Document doc = new Document(dataDir + "document.doc", lo); |
ตั้งค่าการเข้ารหัสอย่างชัดเจน
รูปแบบเอกสารที่ทันสมัยที่สุดจัดเก็บเนื้อหาของพวกเขาในยูนิโค้ดและไม่จำเป็นต้องมีกา บทความนี้ไม่มีการอ้างอิงจากเอกสารอ้างอิงหรือแหล่งข้อมูลโปรดช่วยพัฒนาบทความนี้โด 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); |
โหลดเอกสารที่เข้ารหัส
คุณสามารถป้อนเอกสารคำที่เข้ารหัสด้วยรหัสผ่าน การทำเช่นนี้ให้ใช้ตัวสร้างพิเศษเกินพิกัดซึ่งยอมรับวัตถุ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()); |