ใช้DocumentBuilderเพื่อแทรกองค์ประกอบของเอกสาร
DocumentBuilderถูกใช้เพื่อแก้ไขเอกสาร บทความนี้อธิบายและอธิบายวิธีการดำเนินการจำนวนของงาน.
การแทรกสตริงข้อความ
เพียงแค่ผ่านสตริงข้อความที่คุณต้องแทรกลงในเอกสารไปยังวิธีการDocumentBuilder.write การจัดรูปแบบข้อความจะถูกกำหนดโดยคุณสมบัติFont
วัตถุนี้มีแอตทริบิวต์แบบอักษรที่แตกต่างกัน(ชื่อแบบอักษรขนาดตัวอักษรสีและอื่นๆ) คุณลักษณะฟอนต์ที่สำคัญบางอย่างจะแสดงด้วยคุณสมบัติDocumentBuilderเพื่อให้คุณสามารถเข้าถึงได้โดยต เหล่านี้เป็นคุณสมบัติบูลีนFont.getBold,Font.getItalicและFont.getUnderline.
ตัวอย่างรหัสต่อไปนี้จะแทรกข้อความที่จัดรูปแบบโดยใช้DocumentBuilder.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Font font = builder.getFont(); | |
font.setSize(16); | |
font.setColor(Color.blue); | |
font.setBold(true); | |
font.setName("Algerian"); | |
font.setUnderline(Underline.DOUBLE); | |
builder.write("aspose......... aspose_words_java"); |
การแทรกย่อหน้า
DocumentBuilder.เขียนแทรกสตริงของข้อความลงในเอกสารเช่นกันแต่นอกจากนี้จะเพิ่มตัวแบ่งย่อหน้า การฟอร์แมตแบบอักษรปัจจุบันจะถูกระบุโดยDocumentBuildergetFontคุณสมบัติและการจัดรูปแบบย่อหน้าปัจจุบันจะถูกกำหนดโดยDocumentBuildergetParagraphFormatทรัพย์สิน.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกย่อหน้าลงในเอกสาร.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Font font = builder.getFont(); | |
font.setSize(16); | |
font.setColor(Color.DARK_GRAY); | |
font.setBold(true); | |
font.setName("Algerian"); | |
font.setUnderline(2); | |
ParagraphFormat paragraphFormat = builder.getParagraphFormat(); | |
paragraphFormat.setFirstLineIndent(12); | |
paragraphFormat.setAlignment(1); | |
paragraphFormat.setKeepTogether(true); | |
builder.write("This is a sample Paragraph"); | |
doc.save(dataDir + "InsertParagraph_out.doc"); |
การใส่ตาราง
อัลกอริทึมพื้นฐานในการสร้างตารางโดยใช้DocumentBuilder
เป็นเรื่องง่าย:
- เริ่มต้นตารางด้วยDocumentBuilder.startTable.
- แทรกเซลล์โดยใช้DocumentBuilder.insertCell นี้จะเริ่มต้นแถวใหม่โดยอัตโนมัติ หากจำเป็นให้ใช้คุณสมบัติDocumentBuilder.getCellFormatเพื่อระบุการจัดรูปแบบเซลล์.
- แทรกเนื้อหาของเซลล์โดยใช้วิธีการ
DocumentBuilder
. - ทำซ้ำขั้นตอนที่ 2 และ 3 จนกว่าแถวจะเสร็จสมบูรณ์.
- โทรDocumentBuilder.endRowเพื่อสิ้นสุดแถวปัจจุบัน หากจำเป็นให้ใช้คุณสมบัติDocumentBuilder.RowFormatเพื่อระบุการจัดรูปแบบแถว.
- ทำซ้ำขั้นตอนที่ 2-5 จนกว่าตารางจะเสร็จสมบูรณ์.
- โทรDocumentBuilder.endTableเพื่อเสร็จสิ้นการสร้างโต๊ะ วิธีการสร้างตารางDocumentBuilderที่เหมาะสมอธิบายไว้ด้านล่าง.
การเริ่มต้นตาราง
โทรDocumentBuilderstartTableเป็นขั้นตอนแรกในการสร้างโต๊ะ มันสามารถเรียกว่าภายในเซลล์ในกรณีนี้มันเริ่มต้นตารางที่ซ้อนกัน วิธีต่อไปในการโทรคือDocumentBuilderinsertCell.
การใส่เซลล์
หลังจากที่คุณโทรDocumentBuilderinsertCell,เซลล์ใหม่จะถูกสร้างขึ้นและเนื้อหาใดๆที่คุณเพิ่มโดยใช้วิธีการอื่นๆของชั้นเรียนDocumentBuilder
จะถูกเพิ่มลงในเซลล์ปัจจุบัน ในการเริ่มเซลล์ใหม่ในแถวเดียวกันให้โทรDocumentBuilderinsertCellอีกครั้ง ใช้DocumentBuildergetCellFormatคุณสมบัติเพื่อระบุการจัดรูปแบบเซลล์ ออบเจกต์getCellFormatที่แสดงการจัดรูปแบบทั้งหมดสำหรับเซลล์ตาราง.
สิ้นสุดแถว
โทรDocumentBuilder.endRowเพื่อจบแถวปัจจุบัน ถ้าคุณโทรDocumentBuilderinsertCellทันทีหลังจากนั้นตารางยังคงอยู่ในแถวใหม่ ใช้คุณสมบัติDocumentBuilder.RowFormat
เพื่อระบุการจัดรูปแบบแถว ออบเจกต์RowFormatที่แสดงการจัดรูปแบบทั้งหมดสำหรับแถวตาราง.
สิ้นสุดตาราง
โทรDocumentBuilderendTableเพื่อจบตารางปัจจุบัน วิธีนี้ควรจะเรียกว่าเพียงครั้งเดียวหลังจากDocumentBuilderendRowถูกเรียก เมื่อเรียกDocumentBuilderendTableเลื่อนเคอร์เซอร์ออกจากเซลล์ปัจจุบันไปยังตำแหน่งหลังตาราง ตัวอย่างต่อไปนี้แสดงให้เห็นถึงวิธีการสร้างตารางที่จัดรูปแบบที่มี 2 แถวและ 2 คอลัมน์.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DocumentBuilderBuildTable.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Table table = builder.startTable(); | |
builder.insertCell(); | |
table.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS); | |
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER); | |
builder.write("This is Row 1 Cell 1"); | |
builder.insertCell(); | |
builder.write("This is Row 1 Cell 2"); | |
builder.endRow(); | |
builder.getRowFormat().setHeight(100); | |
builder.getRowFormat().setHeightRule(HeightRule.EXACTLY); | |
builder.getCellFormat().setOrientation(TextOrientation.UPWARD); | |
builder.write("This is Row 2 Cell 1"); | |
builder.insertCell(); | |
builder.getCellFormat().setOrientation(TextOrientation.DOWNWARD); | |
builder.write("This is Row 2 Cell 2"); | |
builder.endRow(); | |
builder.endTable(); | |
doc.save(dataDir + "output.doc"); |
การใส่ตัวหยุดพัก
หากคุณต้องการเริ่มต้นบรรทัดใหม่อย่างชัดเจนย่อหน้าคอลัมน์ส่วนหรือหน้าให้โทรDocumentBuilderinsertBreak. ส่งผ่านไปยังวิธีการนี้ชนิดของการหยุดพักที่คุณจำเป็นต้องแทรกที่จะแสดงโดยการแจงนับBreakType
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกตัวแบ่งหน้าในเอกสาร.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DocumentBuilderInsertBreak.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.write("This is Page 1"); | |
builder.insertBreak(BreakType.PAGE_BREAK); | |
builder.write("This is Page 2"); | |
builder.insertBreak(BreakType.PAGE_BREAK); | |
builder.write("This is Page 3"); | |
doc.save(dataDir + "output.doc"); |
การแทรกรูปภาพ
DocumentBuilderให้การโอเวอร์โหลดหลายวิธีของDocumentBuilder.insertImageที่ช่วยให้คุณสามารถแทรกรูปภาพแบบอินไลน์หรือแบบลอ หากรูปภาพเป็นEMFหรือWMFเมตาไฟล์จะถูกแทรกลงในเอกสารในรูปแบบเมตาไฟล์ ภาพอื่นๆทั้งหมดจะถูกเก็บไว้ในรูปแบบPNG DocumentBuilderinsertImageวิธีการสามารถใช้ภาพจากแหล่งที่มาที่แตกต่างกัน:
- จากแฟ้มหรือ
URL
โดยการส่งผ่านพารามิเตอร์สตริง - จากสตรีมโดยผ่านพารามิเตอร์
Stream
- จากอ็อบเจ็กต์รูปภาพโดยผ่านพารามิเตอร์รูปภาพ
- จากอาร์เรย์ไบต์โดยผ่านพารามิเตอร์อาร์เรย์ไบต์
- และอื่นๆ
แต่ละDocumentBuilderinsertImageวิธีการ,มีการโอเวอร์โหลดเพิ่มเติมซึ่งช่วยให้คุณสามารถแทรกภาพที่มีตัวเลือกต่อไปนี้:
- อินไลน์หรือลอยตัวในตำแหน่งที่เฉพาะเจาะจง
- ขนาดเปอร์เซ็นต์หรือขนาดที่กำหนดเอง
นอกจากนี้DocumentBuilderinsertImageวิธีส่งกลับวัตถุShapeที่เพิ่งสร้างและแทรกเพื่อให้คุณสามารถปรับเปลี่ยนคุณสมบัติของรูปร่าง.
การแทรกรูปภาพแบบอินไลน์
ส่งสตริงเดียวที่แสดงถึงไฟล์ที่มีรูปภาพเป็นDocumentBuilderinsertImageเพื่อแทรกรูปภาพลงในเอกสารเป็นกราฟิกแบบอินไลน์ ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกรูปภาพอินไลน์ที่ตำแหน่งเคอร์เซอร์ลงในเอกสาร.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DocumentBuilderInsertInlineImage.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.insertImage(dataDir + "test.jpg"); | |
doc.save(dataDir + "output.doc"); |
การแทรกรูปภาพแบบลอยตัว(ตำแหน่งที่แน่นอน)
ตัวอย่างนี้แทรกรูปภาพที่ลอยจากแฟ้มหรือURL
ที่ตำแหน่งและขนาดที่ระบุ.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DocumentBuilderInsertFloatingImage.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.insertImage(dataDir + "test.jpg", | |
RelativeHorizontalPosition.MARGIN, | |
100, | |
RelativeVerticalPosition.MARGIN, | |
100, | |
200, | |
100, | |
WrapType.SQUARE); | |
doc.save(dataDir + "output.doc"); |
การใส่บุ๊กมาร์ก
เมื่อต้องการแทรกบุ๊กมาร์กลงในเอกสาร,คุณควรทำต่อไปนี้:
- โทรDocumentBuilder.startBookmarkผ่านชื่อที่ต้องการของบุ๊กมาร์ก.
- แทรกข้อความที่คั่นหน้าด้วยวิธีการ
DocumentBuilder
. - โทรDocumentBuilder.endBookmarkผ่านมันชื่อเดียวกันกับที่คุณใช้กับDocumentBuilderstartBookmark.
ที่คั่นหน้าสามารถทับซ้อนกันและขยายช่วงใดๆ ในการสร้างบุ๊กมาร์กที่ถูกต้องคุณจะต้องเรียกทั้งสองDocumentBuilderstartBookmarkและDocumentBuilderendBookmarkที่มีชื่อบุ๊กมาร์กเดียวกัน.
บุ๊คมาร์คที่เกิดขึ้นไม่ดีหรือบุ๊คมาร์คที่มีชื่อที่ซ้ำกันจะถูกละเว้นเมื่อเอกสารจะถูกบันทึกไว้.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกบุ๊กมาร์กลงในเอกสารโดยใช้โปรแกรมสร้างเอกส.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DocumentBuilderInsertBookmark.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.startBookmark("FineBookMark"); | |
builder.write("This is just a fine bookmark."); | |
builder.endBookmark("FineBookmark"); | |
doc.save(dataDir + "output.doc");// |
การใส่ฟิลด์
ฟิลด์ในMicrosoft Wordเอกสารประกอบด้วยรหัสฟิลด์และผลลัพธ์ฟิลด์ รหัสฟิลด์เป็นเหมือนสูตรและผลลัพธ์ฟิลด์คือค่าที่สูตรผลิต รหัสฟิลด์อาจประกอบด้วยสวิตช์ฟิลด์ที่มีคำแนะนำเพิ่มเติมเพื่อดำเนินการดำเนินการเฉพา คุณสามารถสลับไปมาระหว่างการแสดงรหัสฟิลด์และผลลัพธ์ในเอกสารของคุณในMicrosoft Wordโดยใช้แป้นพิมพ์ลัด+F9 รหัสฟิลด์จะปรากฏระหว่างวงเล็บปีกกา({ }
)ใช้DocumentBuilder.insertFieldเพื่อสร้างฟิลด์ในเอกสาร คุณต้องระบุชนิดฟิลด์รหัสฟิลด์และค่าฟิลด์ หากคุณไม่แน่ใจเกี่ยวกับไวยากรณ์รหัสฟิลด์เฉพาะ,สร้างฟิลด์ในMicrosoft Wordก่อนและสลับเพื่อดูรหัสฟิลด์.
ตัวอย่างรหัสต่อไปนี้แทรกฟิลด์ผสานลงในเอกสารโดยใช้DocumentBuilder.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DocumentBuilderInsertField.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.getFont().setLocaleId(1031); | |
builder.insertField("MERGEFIELD Date1 \\@ \"dddd, d MMMM yyyy\""); | |
builder.write(" - "); | |
builder.insertField("MERGEFIELD Date2 \\@ \"dddd, d MMMM yyyy\""); | |
doc.save(dataDir + "output.doc"); |
การแทรกฟิลด์Form
เขตข้อมูลฟอร์มเป็นกรณีเฉพาะของเขตข้อมูลคำที่ช่วยให้"โต้ตอบ"กับผู้ใช้ ฟิลด์แบบฟอร์มในMicrosoft Wordรวมถึงกล่องข้อความComboboxและcheckboxDocumentBuilderมีวิธีการพิเศษในการแทรกฟิลด์แบบฟอร์มแต่ละชนิดลงในเอกสาร:DocumentBuilder.insertTextInput,DocumentBuilder.insertCheckBoxและDocumentBuilder.insertComboBox โปรดทราบว่าถ้าคุณระบุชื่อสำหรับฟิลด์ฟอร์มบุ๊กมาร์กจะถูกสร้างขึ้นโดยอัตโนมัติด้วยชื่อเดีย.
การใส่การป้อนข้อความ
DocumentBuilder.insertTextInputเพื่อแทรกกล่องข้อความลงในเอกสาร ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกฟิลด์ฟอร์มการป้อนข้อความลงในเอกสาร.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DocumentBuilderInsertTextInputFormField.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.insertTextInput("TextInput", TextFormFieldType.REGULAR, "", "Hello", 0); | |
doc.save(dataDir + "output.doc"); |
การใส่CheckBox
โทรDocumentBuilderinsertCheckBoxเพื่อแทรกcheckboxลงในเอกสาร ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกฟิลด์ฟอร์มcheckboxลงในเอกสาร.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DocumentBuilderInsertCheckBoxFormField.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.insertCheckBox("CheckBox", true, true, 0); | |
doc.save(dataDir + "output.doc"); |
การใส่กล่องคำสั่งผสม
โทรDocumentBuilderinsertComboBoxเพื่อใส่กล่องคำสั่งผสมลงในเอกสาร ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกฟิลด์ฟอร์มกล่องคำสั่งผสมลงในเอกสาร.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DocumentBuilderInsertComboBoxFormField.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
String[] items = {"One", "Two", "Three"}; | |
builder.insertComboBox("DropDown", items, 0); | |
doc.save(dataDir + "output.doc"); |
การแทรกโลแคลที่ระดับฟิลด์
ลูกค้าสามารถระบุสถานที่ที่ระดับสนามในขณะนี้และสามารถบรรลุการควบคุมที่ดีขึ้น รหัสโลแคลสามารถเชื่อมโยงกับแต่ละฟิลด์ภายในDocumentBuilder ตัวอย่างด้านล่างแสดงให้เห็นถึงวิธีการใช้ตัวเลือกนี้.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
DocumentBuilder builder = new DocumentBuilder(); | |
Field field = builder.insertField(FieldType.FIELD_DATE, true); | |
field.setLocaleId(1049); | |
builder.getDocument().save(getArtifactsDir() + "WorkingWithFields.SpecifylocaleAtFieldlevel.docx"); |
การแทรกHTML
คุณสามารถแทรกสตริงHTMLที่มีส่วนHTMLหรือเอกสารทั้งหมดHTMLลงในเอกสารคำ เพียงแค่ผ่านสตริงนี้ไปDocumentBuilderinsertHtmlวิธี. หนึ่งในการใช้งานที่มีประโยชน์ของวิธีนี้คือการจัดเก็บสตริงของHTMLในฐานข้อมูลและแทรกลงในเอกสารระหว่างMail Mergeเพื่อรับเนื้อหาที่จัดรูปแบบเพิ่มขึ้นแทนที่จะสร้างโดยใช้วิธีการต่างๆของตัวสร้างเอกสาร ตัวอย่างรหัสต่อไปนี้แสดงแทรกHTMLลงในเอกสารโดยใช้DocumentBuilder.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DocumentBuilderInsertHtml.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.insertHtml( | |
"<P align='right'>Paragraph right</P>" + | |
"<b>Implicit paragraph left</b>" + | |
"<div align='center'>Div center</div>" + | |
"<h1 align='left'>Heading 1 left.</h1>"); | |
doc.save(dataDir + "output.doc"); |
การแทรกการเชื่อมโยงหลายมิติ
ใช้DocumentBuilderinsertHyperlinkเพื่อแทรกการเชื่อมโยงหลายมิติในเอกสาร (URLหรือชื่อของบุ๊กมาร์กภายในเอกสาร)และพารามิเตอร์บูลีนที่ควรเป็นจริงถ้าURL
เป็นชื่อของบุ๊กมDocumentBuilder.insertHyperlinkภายในโทรDocumentBuilder.insertField. วิธีการเพิ่มเครื่องหมายวรรคตอนที่จุดเริ่มต้นและจุดสิ้นสุดของURLเสมอ โปรดทราบว่าคุณต้องระบุการจัดรูปแบบแบบอักษรสำหรับข้อความแสดงการเชื่อมโยงหลายมิติอย่างชัดเจนโดยใช้คุณสมบัติFont
ตัวอย่างรหัสต่อไปนี้แทรกการเชื่อมโยงหลายมิติลงในเอกสารโดยใช้DocumentBuilder.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.write("Please make sure to visit "); | |
builder.getFont().setColor(Color.BLUE); | |
builder.getFont().setUnderline(Underline.SINGLE); | |
builder.insertHyperlink("Aspose Website", "http://www.aspose.com", false); | |
builder.getFont().clearFormatting(); | |
builder.write(" for more information."); | |
doc.save(getArtifactsDir() + "AddContentUsingDocumentBuilder.InsertHyperlink.docx"); |
การแทรกสารบัญ
คุณสามารถแทรกฟิลด์TOC
(สารบัญ)ลงในเอกสารที่ตำแหน่งปัจจุบันโดยการเรียกวิธีการDocumentBuilder.insertTableOfContents DocumentBuilderวิธีinsertTableOfContentsจะแทรกช่องข้อมูลTOC
ลงในเอกสารเท่านั้น เพื่อที่จะสร้างสารบัญและแสดงตามหมายเลขหน้า,ทั้งสองวิธีDocument.UpdateFieldsต้องเรียกว่าหลังจากการแทรกของฟิลด์. ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกตารางของฟิลด์เนื้อหาลงในเอกสาร.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
String dataDir = Utils.getDataDir(DocumentBuilderInsertTableOfContents.class); | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u"); | |
builder.insertBreak(BreakType.PAGE_BREAK); | |
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1); | |
builder.writeln("Heading 1"); | |
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2); | |
builder.writeln("Heading 1.1"); | |
builder.writeln("Heading 1.2"); | |
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1); | |
builder.writeln("Heading 2"); | |
builder.writeln("Heading 3"); | |
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2); | |
builder.writeln("Heading 3.1"); | |
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_3); | |
builder.writeln("Heading 3.1.1"); | |
builder.writeln("Heading 3.1.2"); | |
builder.writeln("Heading 3.1.3"); | |
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2); | |
builder.writeln("Heading 3.2"); | |
builder.writeln("Heading 3.3"); | |
doc.updateFields(); | |
doc.save(dataDir + "output.doc"); |
การแทรกวัตถุOle
ถ้าคุณต้องการOleโทรวัตถุDocumentBuilder.insertOleObjectAsIcon.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.insertOleObject("http://www.aspose.com", "htmlfile", true, true, null); | |
doc.save(getArtifactsDir() + "WorkingWithOleObjectsAndActiveX.InsertOleObject.docx"); |
ตั้งชื่อไฟล์และนามสกุลเมื่อใส่วัตถุOle
OLEแพคเกจเป็นมรดกและ"ไม่มีเอกสาร"วิธีการจัดเก็บวัตถุที่ฝังตัวถ้าไม่เป็นที่รู้จักOLEตัวจัดการ ต้นWindowsรุ่นเช่นWindows 3.1,95 และ 98 มีPackager.exeแอพลิเคชันซึ่งสามารถใช้ในการฝังชนิดของข้อมูลใดๆลงในเอกสาร ตอนนี้,โปรแกรมนี้ถูกแยกออกจากWindowsแต่MSคำและโปรแกรมประยุกต์อื่นๆยังคงใช้มันเพื่อฝังข้อมูลหากOLEตัวจัดการหายไปหรือไม่ทราบ. OlePackageชั้นอนุญาตให้เข้าถึงOLE Packageคุณสมบัติตัวอย่างรหัสต่อไปนี้แสดงวิธีการตั้งค่าชื่อไฟล์ส่วนขยายและชื่อที่แสดงสำหรับOLE Package.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
byte[] bs = FileUtils.readFileToByteArray(new File(getMyDir() + "Zip file.zip")); | |
try (ByteArrayInputStream stream = new ByteArrayInputStream(bs)) | |
{ | |
Shape shape = builder.insertOleObject(stream, "Package", true, null); | |
OlePackage olePackage = shape.getOleFormat().getOlePackage(); | |
olePackage.setFileName("filename.zip"); | |
olePackage.setDisplayName("displayname.zip"); | |
doc.save(getArtifactsDir() + "WorkingWithOleObjectsAndActiveX.InsertOleObjectWithOlePackage.docx"); | |
} |
เข้าถึงข้อมูลดิบของวัตถุOLE
ตัวอย่างรหัสต่อไปนี้แสดงให้เห็นถึงวิธีการรับOLEวัตถุข้อมูลดิบโดยใช้วิธีการOleFormat.GetRawData
().
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Shape oleShape = (Shape) doc.getChild(NodeType.SHAPE, 0, true); | |
byte[] oleRawData = oleShape.getOleFormat().getRawData(); |
แทรกกฎแนวนอนลงในเอกสาร
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกรูปร่างกฎแนวนอนลงในเอกสารโดยใช้วิธีการDocumentBuilder.InsertHorizontalRule
.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Initialize document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.writeln("Insert a horizontal rule shape into the document."); | |
builder.insertHorizontalRule(); | |
dataDir = dataDir + "DocumentBuilder.InsertHorizontalRule_out.doc"; | |
doc.save(dataDir); |
การทำงานกับรูปร่าง
การใส่รูปร่างแบบอินไลน์และแบบลอยตัวฟรี
คุณสามารถแทรกรูปร่างแบบอินไลน์ที่มีชนิดและขนาดที่ระบุและรูปร่างแบบลอยตัวได้โดยมีตำแหน่งขนาดและชนิดการตัดข้อความที่ระบุลงในเอกสารโดยใช้วิธีการDocumentBuilder.InsertShape
วิธีการDocumentBuilder.InsertShape
ช่วยให้แทรกรูปร่างDMLลงในรูปแบบเอกสาร เอกสารต้องถูกบันทึกในรูปแบบที่รองรับDMLรูปร่างมิฉะนั้นโหนดดังกล่าวจะถูกแปลงเป็นVMLรูปร่าง ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกรูปร่างชนิดนี้ลงในเอกสาร.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
//Free-floating shape insertion. | |
Shape shape = builder.insertShape(ShapeType.TEXT_BOX, | |
RelativeHorizontalPosition.PAGE, 100, | |
RelativeVerticalPosition.PAGE, 100, | |
50, 50, | |
WrapType.NONE); | |
shape.setRotation(30.0); | |
builder.writeln(); | |
//Inline shape insertion. | |
shape = builder.insertShape(ShapeType.TEXT_BOX, 50, 50); | |
shape.setRotation(30.0); | |
OoxmlSaveOptions so = new OoxmlSaveOptions(SaveFormat.DOCX); | |
// "Strict" or "Transitional" compliance allows to save shape as DML. | |
so.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL); | |
dataDir = dataDir + "Shape_InsertShapeUsingDocumentBuilder_out.docx"; | |
// Save the document to disk. | |
doc.save(dataDir, so); |
สร้างมุมตัดสี่เหลี่ยมผืนผ้า
คุณสามารถสร้างสี่เหลี่ยมมุมตัดด้วยAspose.Words ประเภทรูปร่างคือSingleCornerSnipped, TopCornersSnipped, DiagonalCornersSnipped, TopCornersOneRoundedOneSnipped, SingleCornerRounded, TopCornersRounded, และDiagonalCornersRounded รูปร่างDMLถูกสร้างขึ้นโดยใช้วิธีการDocumentBuilder.InsertShape
กับประเภทรูปร่างเหล่านี้ ชนิดเหล่านี้ไม่สามารถใช้เพื่อสร้างVMLรูปร่าง พยายามที่จะสร้างรูปร่างโดยใช้ตัวสร้างสาธารณะของชั้นเรียน"รูปร่าง"ยก"NotSupportedException"ข้อยกเว้น ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกรูปร่างชนิดนี้ลงในเอกสาร.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.insertShape(ShapeType.TOP_CORNERS_SNIPPED, 50, 50); | |
OoxmlSaveOptions so = new OoxmlSaveOptions(SaveFormat.DOCX); | |
so.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL); | |
dataDir = dataDir + "AddCornersSnipped_out.docx"; | |
//Save the document to disk. | |
doc.save(dataDir, so); |
นำเข้ารูปร่างด้วยคณิตศาสตร์XMLเป็นรูปร่างเป็นDOM
คุณสามารถใช้คุณสมบัติLoadOptions.ConvertShapeToOfficeMath
เพื่อแปลงรูปร่างด้วยEquationXMLไปยังวัตถุทางคณิตศาสตร์สำนักงาน ค่าเริ่มต้นของคุณสมบัตินี้สอดคล้องกับMSลักษณะการทำงานของคำเช่นรูปร่างที่มีสมการXMLจะไม่.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
LoadOptions lo = new LoadOptions(); | |
lo.setConvertShapeToOfficeMath(true); | |
// Specify load option to use previous default behaviour i.e. convert math | |
// shapes to office math ojects on loading stage. | |
Document doc = new Document(dataDir + "OfficeMath.docx", lo); | |
// Save the document into DOCX | |
doc.save(dataDir + "ConvertShapeToOfficeMath_out.docx", SaveFormat.DOCX); |