แทรกสารบัญใน Word โดยใช้ Java โดยปฏิบัติตามขั้นตอนที่กำหนดไว้ในบทความสั้นๆ นี้ โดยจะแนะนำการตั้งค่าสภาพแวดล้อมโดยอธิบายทรัพยากรที่จำเป็นอย่างละเอียด รายการขั้นตอนที่กำหนดไว้สำหรับการพัฒนาแอปพลิเคชัน และโค้ดตัวอย่างการทำงานที่สาธิต วิธีเพิ่มหน้าเนื้อหาใน Word โดยใช้ Java คุณจะได้รับคำแนะนำให้เพิ่ม TOC ลงในไฟล์ Word ที่มีอยู่โดยการแยกวิเคราะห์เนื้อหา
ขั้นตอนในการสร้างสารบัญใน Word โดยใช้ Java
- สร้างสภาพแวดล้อมเพื่อใช้ Aspose.Words for Java เพื่อแทรก TOC
- เข้าถึงไฟล์ Word ลงในวัตถุ Document และเริ่มต้นวัตถุ DocumentBuilder
- ใส่ชื่อเรื่องของ TOC ด้วยสไตล์ที่ต้องการ
- เพิ่มสารบัญและแทรกตัวแบ่งหน้า
- สร้างสารบัญว่าง
- บันทึกไฟล์ Word ใหม่ที่มี TOC อยู่ข้างใน
คุณสามารถ แทรกหน้าเนื้อหาใน Word โดยใช้ Java ได้โดยทำตามขั้นตอนที่กล่าวมาข้างต้น โดยที่กระบวนการจะเริ่มต้นด้วยการโหลดไฟล์ Word ต้นทาง และกำหนดค่าอินสแตนซ์คลาส DocumentBuilder ที่รองรับการแทรกสารบัญ แทรกชื่อเรื่องและสารบัญโดยใช้เมธอด insertTableOfContents() และเติมสารบัญว่างโดยใช้เมธอด updateFields()
รหัสเพื่อสร้างสารบัญใน Word โดยใช้ Java
import com.aspose.words.BreakType; | |
import com.aspose.words.Document; | |
import com.aspose.words.DocumentBuilder; | |
import com.aspose.words.FindReplaceDirection; | |
import com.aspose.words.FindReplaceOptions; | |
import com.aspose.words.License; | |
import com.aspose.words.ParagraphAlignment; | |
import com.aspose.words.ParagraphFormat; | |
public class TocInWord { | |
public static void main(String[] tocArguments) throws Exception { | |
String path ="/Users/Data/"; | |
// Apply API Java license to add TOC | |
new License().setLicense(path+"Conholdate.Total.Product.Family.lic"); | |
// Access the source Word document | |
Document doc = new Document("example03.docx"); | |
// Instantiate the DocumentBuilder class instance | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Create ParagraphFormat instance from the builder | |
ParagraphFormat paragraphFormat = builder.getParagraphFormat(); | |
// Get the existing style name | |
String defaultStyle = paragraphFormat.getStyleName(); | |
// Set the style name and text alignment for the TOC | |
paragraphFormat.setStyleName("Title"); | |
paragraphFormat.setAlignment(ParagraphAlignment.CENTER); | |
// Add title of TOC | |
builder.writeln("Table of contents"); | |
// Set the text style for the TOC | |
paragraphFormat.setStyleName(defaultStyle); | |
//Insert a default table of contents | |
builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u"); | |
//Add a page break for the TOC | |
builder.insertBreak(BreakType.PAGE_BREAK); | |
// Generate the default empty table of contents | |
doc.updateFields(); | |
// Save the Word document having TOC | |
doc.save("output.docx"); | |
} | |
} |
จากตัวอย่างนี้ คุณสามารถเข้าถึงเอกสาร Word ที่มีอยู่ และ สร้างหน้าเนื้อหาใน Word โดยใช้ Java เมธอด insertTableOfContents() ต้องใช้สวิตช์เพื่อควบคุมพฤติกรรมของสารบัญ เช่น 1-3 ใช้สำหรับระบุรูปแบบส่วนหัว 1, 2 และ 3 ‘\h’ ใช้สำหรับการตั้งค่าไฮเปอร์ลิงก์ และ ‘\u’ ใช้สำหรับกำหนดระดับโครงร่าง สารบัญเริ่มต้น (TOC) ว่างเปล่าและสามารถเติมข้อมูลได้โดยใช้เมธอด updateFields()
ตัวอย่างนี้ได้กล่าวถึง วิธีเพิ่มสารบัญใน Word โดยใช้ Java หากต้องการพลิกข้อความภายในเอกสาร Word โปรดดูบทความเกี่ยวกับ พลิกข้อความใน Word โดยใช้ Java