บทความนี้จะแนะนำวิธีแทรกหัวข้อย่อยใน Word โดยใช้ Java โดยจะแบ่งปันรายละเอียดเกี่ยวกับการตั้งค่าสภาพแวดล้อมการพัฒนา รายการขั้นตอนในการเขียนแอปพลิเคชัน และตัวอย่างโค้ดที่สาธิตการสร้างหัวข้อย่อยของ Microsoft Word โดยใช้ Java คุณจะได้เรียนรู้การใช้งานหัวข้อย่อยในหลายระดับ และการแทรกรายการตัวเลขหากจำเป็น
ขั้นตอนการเพิ่มหัวข้อย่อยใน Word โดยใช้ Java
- ตั้งค่าสภาพแวดล้อมให้ใช้ Aspose.Words for Java เพื่อเพิ่มจุดหัวข้อย่อย
- สร้างไฟล์ Word โดยใช้คลาส Document เชื่อมโยงวัตถุ DocumentBuilder และใช้ applyBulletDefault() เพื่อเริ่มรายการแบบมีหัวข้อย่อย
- เพิ่มข้อความหัวข้อย่อยโดยใช้วิธี writeln()
- ตั้งค่าการเยื้องของรายการหัวข้อย่อยโดยใช้เมธอด listIndent() และ listOutdent()
- เรียกใช้เมธอด removeNumbers() เพื่อสิ้นสุดรายการหัวข้อย่อย
- Save ผลลัพธ์หลังจากเพิ่มรายการหัวข้อย่อย
ขั้นตอนเหล่านี้จะอธิบายวิธีเพิ่มจุดหัวข้อย่อยใน Word โดยใช้ Java เริ่มต้นกระบวนการโดยสร้างเอกสาร Word เชื่อมโยงอ็อบเจ็กต์ DocumentBuilder และเริ่มรายการโดยเรียกใช้เมธอด applyBulletDefault() สุดท้าย เพิ่มข้อความและตั้งค่าการเยื้องเข้าและออกโดยใช้เมธอด listIndet() และ listOutdent()
โค้ดสำหรับสร้างเครื่องหมายหัวข้อย่อยใน Word โดยใช้ Java
import com.aspose.words.*; | |
public class Main | |
{ | |
public static void main(String[] args) throws Exception // Add Bullets | |
{ | |
// Set the licenses | |
new License().setLicense("License.lic"); | |
Document newDoc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(newDoc); | |
//docBuilder.getListFormat().applyNumberDefault(); | |
builder.getListFormat().applyBulletDefault(); | |
builder.writeln("Continent 1"); | |
builder.writeln("Continent 2"); | |
builder.getListFormat().listIndent(); | |
builder.writeln("Continent 2 Country 1"); | |
builder.writeln("Continent 2 Country 2"); | |
builder.getListFormat().listIndent(); | |
builder.writeln("Continent 2 Country 2 City 1"); | |
builder.writeln("Continent 2 Country 2 City 2"); | |
builder.getListFormat().listOutdent(); | |
builder.writeln("Continent 2 Country 3"); | |
builder.getListFormat().listOutdent(); | |
builder.writeln("Continent 3"); | |
builder.getListFormat().removeNumbers(); | |
newDoc.save("BulletsDocument.docx"); | |
System.out.println("Bullets added in Word file successfully"); | |
} | |
} |
โค้ดนี้สาธิตการใช้งาน สัญลักษณ์หัวข้อย่อยใน Microsoft Word โดยใช้ Java ApplyBulletDefault() จะเริ่มรายการสัญลักษณ์หัวข้อย่อย และวิธี removeNumbers() จะหยุดรายการสัญลักษณ์หัวข้อย่อยในย่อหน้าปัจจุบัน หากคุณต้องการสร้างรายการตัวเลข ให้ใช้ applyNumberDefault() แทน applyBulletDefault()
บทช่วยสอนนี้แนะนำเราเกี่ยวกับการสร้างไฟล์ Word และการเพิ่มรายการหัวข้อย่อยในเอกสาร Word หากคุณต้องการแทรกรูปภาพในไฟล์ Word โปรดดูบทความใน วิธีการแทรกภาพใน Word โดยใช้ Java