ทำงานกับตัวเลือกและรูปลักษณ์ของเอกสาร Word
บางครั้งคุณอาจต้องเปลี่ยนรูปลักษณ์ของเอกสาร เช่น ตั้งค่าภาษาหรือจำนวนบรรทัดต่อหน้า Aspose.Words ให้ความสามารถในการควบคุมวิธีการแสดงเอกสาร รวมถึงตัวเลือกเพิ่มเติมบางอย่าง บทความนี้จะอธิบายความเป็นไปได้ดังกล่าว
ตั้งค่าตัวเลือกการแสดงเอกสาร
คุณสามารถควบคุมวิธีการแสดงเอกสารใน Microsoft Word ได้โดยใช้คลาส ViewOptions ตัวอย่างเช่น คุณสามารถตั้งค่าการซูมเอกสารโดยใช้คุณสมบัติ zoom_percent หรือโหมดมุมมองโดยใช้คุณสมบัติ view_type
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีตรวจสอบให้แน่ใจว่าเอกสารแสดงที่ 50% เมื่อเปิดใน Microsoft Word:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Document.docx") | |
doc.view_options.view_type = aw.settings.ViewType.PAGE_LAYOUT | |
doc.view_options.zoom_percent = 50 | |
doc.save(docs_base.artifacts_dir + "WorkingWithDocumentOptionsAndSettings.view_options.docx") |
ตั้งค่าตัวเลือกการแสดงหน้า
หากคุณต้องการกำหนดจำนวนอักขระต่อบรรทัด ให้ใช้คุณสมบัติ characters_per_line คุณยังสามารถกำหนดจำนวนบรรทัดต่อหน้าสำหรับเอกสาร Word ได้ โดยใช้คุณสมบัติ lines_per_page เพื่อรับหรือกำหนดจำนวนบรรทัดต่อหน้าในตารางเอกสาร
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีตั้งค่าจำนวนอักขระต่อบรรทัดและจำนวนบรรทัดต่อหน้าสำหรับเอกสาร Microsoft Word:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Document.docx") | |
# Set the layout mode for a section allowing to define the document grid behavior. | |
# Note that the Document Grid tab becomes visible in the Page Setup dialog of MS Word | |
# if any Asian language is defined as editing language. | |
doc.first_section.page_setup.layout_mode = aw.SectionLayoutMode.GRID | |
doc.first_section.page_setup.characters_per_line = 30 | |
doc.first_section.page_setup.lines_per_page = 10 | |
doc.save(docs_base.artifacts_dir + "WorkingWithDocumentOptionsAndSettings.document_page_setup.docx") |
ตั้งค่ากำหนดภาษา
การแสดงเอกสารใน Microsoft Word ขึ้นอยู่กับภาษาที่ถูกตั้งค่าเป็นค่าเริ่มต้นสำหรับเอกสารนี้ หากไม่มีการตั้งค่าภาษาเป็นค่าเริ่มต้น Microsoft Word จะใช้ข้อมูลจากกล่องโต้ตอบ “ตั้งค่าการตั้งค่าภาษาของ Office” ซึ่งสามารถพบได้ภายใต้ “ไฟล์ → ตัวเลือก → ภาษา” ใน Microsoft Word 2019
ด้วย Aspose.Words คุณยังสามารถตั้งค่ากำหนดภาษาโดยใช้คลาส LanguagePreferences ได้อีกด้วย โปรดทราบว่าเพื่อให้การแสดงเอกสารของคุณถูกต้อง จำเป็นต้องตั้งค่าเวอร์ชัน Microsoft Word ที่กระบวนการโหลดเอกสารควรตรงกัน ซึ่งสามารถทำได้โดยใช้คุณสมบัติ msw_version
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการเพิ่มภาษาญี่ปุ่นให้กับภาษาสำหรับการแก้ไข:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
loadOptions = aw.loading.LoadOptions() | |
# Set language preferences that will be used when document is loading. | |
loadOptions.language_preferences.add_editing_language(aw.loading.EditingLanguage.JAPANESE) |
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าภาษารัสเซียเป็นภาษาสำหรับการแก้ไขเริ่มต้น:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
loadOptions = aw.loading.LoadOptions() | |
loadOptions.language_preferences.default_editing_language = aw.loading.EditingLanguage.RUSSIAN | |
doc = aw.Document(docs_base.my_dir + "No default editing language.docx", loadOptions) | |
localeId = doc.styles.default_font.locale_id | |
print("The document either has no any language set in defaults or it was set to Russian originally." if (localeId == aw.loading.EditingLanguage.RUSSIAN) | |
else "The document default language was set to another than Russian language originally, so it is not overridden.") |
ปรับเอกสารให้เหมาะสมสำหรับเวอร์ชัน Word โดยเฉพาะ
วิธีการ optimize_for ช่วยให้สามารถปรับเนื้อหาเอกสารให้เหมาะสม รวมถึงลักษณะการทำงานของ Aspose.Words เริ่มต้นสำหรับ Microsoft Word เวอร์ชันใดเวอร์ชันหนึ่งโดยเฉพาะ คุณสามารถใช้วิธีนี้เพื่อป้องกันไม่ให้ Microsoft Word แสดงริบบิ้น “โหมดความเข้ากันได้” เมื่อโหลดเอกสาร โปรดทราบว่าคุณอาจต้องตั้งค่าคุณสมบัติ compliance เป็น ISO29500_2008_TRANSITIONAL หรือสูงกว่าด้วย
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีปรับเนื้อหาเอกสารให้เหมาะสมสำหรับ Microsoft Word 2016:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Document.docx") | |
doc.compatibility_options.optimize_for(aw.settings.MsWordVersion.WORD2016) | |
doc.save(docs_base.artifacts_dir + "WorkingWithDocumentOptionsAndSettings.optimize_for_ms_word.docx") |