ทำงานกับตัวเลือกและรูปลักษณ์ของเอกสาร Word
บางครั้งคุณอาจต้องเปลี่ยนรูปลักษณ์ของเอกสาร เช่น ตั้งค่าภาษาหรือจำนวนบรรทัดต่อหน้า Aspose.Words ให้ความสามารถในการควบคุมวิธีการแสดงเอกสาร รวมถึงตัวเลือกเพิ่มเติมบางอย่าง บทความนี้จะอธิบายความเป็นไปได้ดังกล่าว
ตั้งค่าตัวเลือกการแสดงเอกสาร
คุณสามารถควบคุมวิธีการแสดงเอกสารใน Microsoft Word ได้โดยใช้คลาส ViewOptions ตัวอย่างเช่น คุณสามารถตั้งค่าการซูมเอกสารโดยใช้คุณสมบัติ ZoomPercent หรือโหมดมุมมองโดยใช้คุณสมบัติ ViewType
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีตรวจสอบให้แน่ใจว่าเอกสารแสดงที่ 50% เมื่อเปิดใน Microsoft Word:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithDocument(); | |
// Load the template document. | |
Document doc = new Document(dataDir + "TestFile.doc"); | |
// Set view option. | |
doc.ViewOptions.ViewType = ViewType.PageLayout; | |
doc.ViewOptions.ZoomPercent = 50; | |
dataDir = dataDir + "TestFile.SetZoom_out.doc"; | |
// Save the finished document. | |
doc.Save(dataDir); |
ตั้งค่าตัวเลือกการแสดงหน้า
หากคุณต้องการกำหนดจำนวนอักขระต่อบรรทัด ให้ใช้คุณสมบัติ CharactersPerLine คุณยังสามารถกำหนดจำนวนบรรทัดต่อหน้าสำหรับเอกสาร Word ได้ โดยใช้คุณสมบัติ LinesPerPage เพื่อรับหรือกำหนดจำนวนบรรทัดต่อหน้าในตารางเอกสาร
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีตั้งค่าจำนวนอักขระต่อบรรทัดและจำนวนบรรทัดต่อหน้าสำหรับเอกสาร Microsoft Word:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithDocument(); | |
Document doc = new Document(dataDir + "Document.doc"); | |
//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.FirstSection.PageSetup.LayoutMode = SectionLayoutMode.Grid; | |
//Set the number of characters per line in the document grid. | |
doc.FirstSection.PageSetup.CharactersPerLine = 30; | |
//Set the number of lines per page in the document grid. | |
doc.FirstSection.PageSetup.LinesPerPage = 10; | |
dataDir = dataDir + "Document.PageSetup_out.doc"; | |
doc.Save(dataDir); |
ตั้งค่ากำหนดภาษา
การแสดงเอกสารใน Microsoft Word ขึ้นอยู่กับภาษาที่ถูกตั้งค่าเป็นค่าเริ่มต้นสำหรับเอกสารนี้ หากไม่มีการตั้งค่าภาษาเป็นค่าเริ่มต้น Microsoft Word จะใช้ข้อมูลจากกล่องโต้ตอบ “ตั้งค่าการตั้งค่าภาษาของ Office” ซึ่งสามารถพบได้ภายใต้ “ไฟล์ → ตัวเลือก → ภาษา” ใน Microsoft Word 2019
ด้วย Aspose.Words คุณยังสามารถตั้งค่ากำหนดภาษาโดยใช้คลาส LanguagePreferences ได้อีกด้วย โปรดทราบว่าเพื่อให้การแสดงเอกสารของคุณถูกต้อง จำเป็นต้องตั้งค่าเวอร์ชัน Microsoft Word ที่กระบวนการโหลดเอกสารควรตรงกัน ซึ่งสามารถทำได้โดยใช้คุณสมบัติ MswVersion
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการเพิ่มภาษาญี่ปุ่นให้กับภาษาสำหรับการแก้ไข:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Create a new LoadOptions object. | |
LoadOptions loadOptions = new LoadOptions(); | |
// Set language preferences that will be used when document is loading. | |
loadOptions.LanguagePreferences.AddEditingLanguage(EditingLanguage.Japanese); | |
Document doc = new Document(dataDir + @"languagepreferences.docx", loadOptions); |
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าภาษารัสเซียเป็นภาษาสำหรับการแก้ไขเริ่มต้น:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.LanguagePreferences.DefaultEditingLanguage = EditingLanguage.Russian; | |
Document doc = new Document(dataDir + @"languagepreferences.docx", loadOptions); | |
int localeId = doc.Styles.DefaultFont.LocaleId; | |
if (localeId == (int)EditingLanguage.Russian) | |
Console.WriteLine("The document either has no any language set in defaults or it was set to Russian originally."); | |
else | |
Console.WriteLine("The document default language was set to another than Russian language originally, so it is not overridden."); |
ปรับเอกสารให้เหมาะสมสำหรับเวอร์ชัน Word โดยเฉพาะ
วิธีการ OptimizeFor ช่วยให้สามารถปรับเนื้อหาเอกสารให้เหมาะสม รวมถึงลักษณะการทำงานของ 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-.NET | |
Document doc = new Document(fileName); | |
doc.CompatibilityOptions.OptimizeFor(Settings.MsWordVersion.Word2016); | |
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName); | |
// Save the document to disk. | |
doc.Save(dataDir); |