แยกเนื้อหาระหว่างโหนดในเอกสาร
เมื่อทำงานกับเอกสารมันเป็นสิ่งสำคัญที่จะสามารถแยกเนื้อหาจากช่วงเฉพาะภายในเอก อย่างไรก็ตามเนื้อหาอาจประกอบด้วยองค์ประกอบที่ซับซ้อนเช่นย่อหน้าตารางรูปภาพฯลฯ.
โดยไม่คำนึงถึงสิ่งที่เนื้อหาจะต้องถูกแยกเมธอดในการแยกเนื้อหานั้นจะถูกกำหนดโดยที่โห เหล่านี้สามารถเป็นเนื้อหาข้อความทั้งหมดหรือข้อความที่เรียบง่ายทำงาน.
มีหลายสถานการณ์ที่เป็นไปได้และดังนั้นจึงมีหลายชนิดโหนดที่แตกต่างกันที่จะต้องพิจารณา ตัวอย่างเช่นคุณอาจต้องการแยกเนื้อหาระหว่าง:
- สองย่อหน้าที่
- รันข้อความที่เฉพาะเจาะจง
- ฟิลด์ประเภทต่างๆเช่นฟิลด์ผสาน
- ช่วงเริ่มต้นและสิ้นสุดของบุ๊กมาร์กหรือแสดงความคิดเห็น
- เนื้อหาต่างๆของข้อความที่มีอยู่ในส่วนที่แยกต่างหาก
ในบางสถานการณ์คุณอาจจำเป็นต้องรวมชนิดโหนดต่างๆเช่นการแยกเนื้อหาระหว่างย่อหน้า.
บทความนี้จัดเตรียมการติดตั้งโค้ดสำหรับการแยกข้อความระหว่างโหนดต่างๆรวมทั้งตัวอย่.
ทำไมสารสกัดจากเนื้อหา
บ่อยครั้งที่เป้าหมายของการแยกเนื้อหาคือการทำซ้ำหรือบันทึกแยกต่างหากในเอกสารให ตัวอย่างเช่นคุณสามารถแยกเนื้อหาและ:
- คัดลอกลงในเอกสารแยกต่างหาก
- แปลงส่วนเฉพาะของเอกสารเป็นPDFหรือรูปภาพ
- ทำซ้ำเนื้อหาในเอกสารหลายครั้ง
- ทำงานกับเนื้อหาที่แยกออกจากส่วนที่เหลือของเอกสาร
นี้สามารถทำได้ง่ายโดยใช้Aspose.Wordsและการดำเนินการรหัสด้านล่าง.
การแยกอัลกอริทึมเนื้อหา
รหัสในส่วนนี้อยู่ทั้งหมดของสถานการณ์ที่เป็นไปได้ที่อธิบายไว้ข้างต้นด้วยวิธีการทั่วไปและ โครงร่างทั่วไปของเทคนิคนี้เกี่ยวข้องกับ:
- รวบรวมโหนดซึ่งกำหนดพื้นที่ของเนื้อหาที่จะสกัดจากเอกสารของคุณ การดึงโหนดเหล่านี้ถูกจัดการโดยผู้ใช้ในโค้ดตามสิ่งที่พวกเขาต้องการถูกดึงออก.
- การส่งผ่านโหนดเหล่านี้ไปยังวิธีการExtractContentที่ให้ไว้ด้านล่าง นอกจากนี้คุณยังต้องผ่านพารามิเตอร์บูลีนซึ่งระบุว่าโหนดเหล่านี้ทำหน้าที่เป็นเครื่องหมาย.
- การเรียกดูรายการของเนื้อหาที่โคลน(โหนดที่คัดลอก)ที่ระบุให้แยก คุณสามารถใช้รายการโหนดนี้ในลักษณะใดๆที่เกี่ยวข้องตัวอย่างเช่นการสร้างเอกสารใหม่.
วิธีการแยกเนื้อหา
เราจะทำงานกับเอกสารด้านล่างในบทความนี้ ที่คุณสามารถเห็นมันมีความหลากหลายของเนื้อหา นอกจากนี้หมายเหตุเอกสารประกอบด้วยส่วนที่สองเริ่มต้นตรงกลางของหน้าแรก บุ๊กมาร์กและข้อคิดเห็นมีอยู่ในเอกสารด้วยแต่ไม่สามารถมองเห็นได้ในภาพหน้าจอด้านล่าง.
ในการดึงเนื้อหาจากเอกสารของคุณคุณต้องเรียกวิธีการExtractContent
ด้านล่างและส่งผ่านพารามิเตอร์ที่เหมาะสม.
พื้นฐานพื้นฐานของวิธีนี้เกี่ยวข้องกับการค้นหาโหนดระดับบล็อก(ย่อหน้าและตาราง)และโคลน ถ้าโหนดเครื่องหมายผ่านเป็นระดับบล็อกแล้ววิธีการที่จะสามารถที่จะเพียงแค่คัดลอกเนื้อ.
แต่ถ้าโหนดเครื่องหมายเป็นแบบอินไลน์(ลูกของย่อหน้า)แล้วสถานการณ์จะซับซ้อนมากขึ้นเนื่ เนื้อหาในโหนดพาเรนต์ที่โคลนไม่อยู่ระหว่างเครื่องหมายจะถูกลบออก โพรเซสนี้ถูกใช้เพื่อให้แน่ใจว่าโหนดอินไลน์จะยังคงเก็บการจัดรูปแบบของย่อหน้าพาเรนต์.
เมธอดจะรันการตรวจสอบบนโหนดที่ผ่านเป็นพารามิเตอร์และโยนข้อยกเว้นถ้าโหนดใดไม่ พารามิเตอร์ที่จะส่งผ่านไปยังวิธีนี้คือ:
-
StartNodeและEndNode สองพารามิเตอร์แรกคือโหนดที่กำหนดที่การสกัดของเนื้อหาคือการเริ่มต้นและสิ้นสุดตามลำ โหนดเหล่านี้สามารถเป็นได้ทั้งระดับบล็อก(Paragraph,Table)หรือระดับอินไลน์(เช่นRun, FieldStart, BookmarkStart ฯลฯ):
- ที่จะผ่านเขตข้อมูลที่คุณควรผ่านวัตถุFieldStartที่สอดคล้องกัน.
- เมื่อต้องการส่งบุ๊กมาร์กโหนดBookmarkStartและBookmarkEndควรส่งผ่าน.
- เมื่อต้องการส่งข้อคิดเห็นควรใช้โหนดCommentRangeStartและCommentRangeEnd.
-
IsInclusive. กำหนดว่าเครื่องหมายจะรวมอยู่ในการสกัดหรือไม่ นูป๊อปอัปที่จะให้เลือกความช่วยเหลือหากต้องการทดสอบให้คลิกเมาส์ปุ่มขวาบนที่ใดๆของข้:
- ถ้าโหนดFieldStartถูกส่งผ่านแล้วตัวเลือกนี้จะกำหนดว่าฟิลด์ทั้งหมดจะถูกรวมหรือยกเว้น.
- ถ้าโหนดBookmarkStartหรือBookmarkEndถูกส่งผ่านตัวเลือกนี้จะกำหนดว่าบุ๊กมาร์กถูกรวมหรือเพียงเนื้อหาระหว่างช่.
- ถ้าโหนดCommentRangeStartหรือCommentRangeEndถูกส่งผ่านอ็อพชันนี้จะกำหนดว่าต้องการรวมความคิดเห็นหรือเฉพาะเนื้อหาใ.
การดำเนินการของExtractContentวิธีที่คุณสามารถหา ที่นี่. วิธีการนี้จะถูกอ้างถึงในสถานการณ์ในบทความนี้.
นอกจากนี้เรายังจะกำหนดวิธีการที่กำหนดเองเพื่อให้ง่ายต่อการสร้างเอกสารจากโหนด วิธีนี้ถูกใช้ในหลายสถานการณ์ด้านล่างและเพียงแค่สร้างเอกสารใหม่และนำเข้าเนื้อหาที่.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีใช้รายการของโหนดและแทรกลงในเอกสารใหม่:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
public static Document generateDocument(Document srcDoc, ArrayList<Node> nodes) throws Exception | |
{ | |
Document dstDoc = new Document(); | |
// Remove the first paragraph from the empty document. | |
dstDoc.getFirstSection().getBody().removeAllChildren(); | |
// Import each node from the list into the new document. Keep the original formatting of the node. | |
NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING); | |
for (Node node : nodes) | |
{ | |
Node importNode = importer.importNode(node, true); | |
dstDoc.getFirstSection().getBody().appendChild(importNode); | |
} | |
return dstDoc; | |
} |
แยกเนื้อหาระหว่างย่อหน้า
นี้แสดงให้เห็นถึงวิธีการใช้วิธีการข้างต้นเพื่อดึงเนื้อหาระหว่างย่อหน้าที่ ในกรณีนี้เราต้องการที่จะดึงร่างกายของตัวอักษรที่พบในช่วงครึ่งแรกของเอกสาร เราสามารถบอกได้ว่านี่คือระหว่าง 7 และ 11 ย่อหน้า.
รหัสด้านล่างทำงานนี้สำเร็จ ย่อหน้าที่เหมาะสมจะถูกแยกออกโดยใช้วิธีการgetChildบนเอกสารและส่งผ่านดัชนีที่ระบุ จากนั้นเราจะส่งผ่านโหนดเหล่านี้ไปยังExtractContentวิธีการและรัฐที่เหล่านี้จะรวมอยู่ในการสกัด. เมธอดนี้จะส่งคืนเนื้อหาที่คัดลอกระหว่างโหนดเหล่านี้ซึ่งถูกแทรกลงในเอกสารใหม่.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแยกเนื้อหาระหว่างย่อหน้าที่โดยใช้วิธีการExtractContent
ด้านบน:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Extract content.docx"); | |
Paragraph startPara = (Paragraph) doc.getFirstSection().getBody().getChild(NodeType.PARAGRAPH, 6, true); | |
Paragraph endPara = (Paragraph) doc.getFirstSection().getBody().getChild(NodeType.PARAGRAPH, 10, true); | |
// Extract the content between these nodes in the document. Include these markers in the extraction. | |
ArrayList<Node> extractedNodes = ExtractContentHelper.extractContent(startPara, endPara, true); | |
Document dstDoc = ExtractContentHelper.generateDocument(doc, extractedNodes); | |
dstDoc.save(getArtifactsDir() + "ExtractContent.ExtractContentBetweenParagraphs.docx"); |
เอกสารผลลัพธ์ประกอบด้วยสองย่อหน้าที่ถูกแยกออก.
แยกเนื้อหาระหว่างโหนดประเภทต่างๆ
เราสามารถแยกเนื้อหาระหว่างชุดใดๆของโหนดระดับบล็อกหรือแบบอินไลน์ ในสถานการณ์สมมตินี้ด้านล่างเราจะแยกเนื้อหาระหว่างย่อหน้าแรกและตารางในส่วนที่ส เราได้รับโหนดเครื่องหมายโดยการเรียกgetFirstParagraphและgetChildวิธีการในส่วนที่สองของเอกสารที่จะดึงที่เหมาะสมParagraphและTableโหนด สำหรับรูปแบบเล็กน้อยให้แทนซ้ำเนื้อหาและแทรกไว้ด้านล่างเดิม.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแยกเนื้อหาระหว่างย่อหน้าและตารางโดยใช้วิธีการExtractContent:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Extract content.docx"); | |
Paragraph startPara = (Paragraph) doc.getLastSection().getChild(NodeType.PARAGRAPH, 2, true); | |
Table endTable = (Table) doc.getLastSection().getChild(NodeType.TABLE, 0, true); | |
// Extract the content between these nodes in the document. Include these markers in the extraction. | |
ArrayList<Node> extractedNodes = ExtractContentHelper.extractContent(startPara, endTable, true); | |
// Let's reverse the array to make inserting the content back into the document easier. | |
Collections.reverse(extractedNodes); | |
for (Node extractedNode : extractedNodes) | |
// Insert the last node from the reversed list. | |
endTable.getParentNode().insertAfter(extractedNode, endTable); | |
doc.save(getArtifactsDir() + "ExtractContent.ExtractContentBetweenBlockLevelNodes.docx"); |
เนื้อหาระหว่างย่อหน้าและตารางได้รับการทำซ้ำด้านล่างเป็นผล.
สารสกัดจากเนื้อหาระหว่างย่อหน้าขึ้นอยู่กับรูปแบบ
คุณอาจต้องแยกเนื้อหาระหว่างย่อหน้าของสไตล์เดียวกันหรือแตกต่างกันเช่นระหว่างย่อห.
นล่างแสดงให้เห็นถึงวิธีการเพื่อให้บรรลุนี้. มันเป็นตัวอย่างง่ายๆที่จะดึงเนื้อหาระหว่างตัวอย่างแรกของ"Heading 1"และ"ส่วนหัว 3"รูปแบบโดยไม่ต้องแยกส่วนหัวเช่นกัน การทำเช่นนี้เราตั้งค่าพารามิเตอร์ที่ผ่านมาเป็นเท็จซึ่งระบุว่าโหนดเครื่องหมายไม่ควรรว.
ในการใช้งานที่เหมาะสมนี้ควรจะรันในวงเพื่อแยกเนื้อหาระหว่างย่อหน้าทั้งหมดของลักษณ เนื้อหาที่แยกจะถูกคัดลอกลงในเอกสารใหม่.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแยกเนื้อหาระหว่างย่อหน้าที่มีลักษณะเฉพาะโดยใช้วิธีการExtractContent:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Extract content.docx"); | |
// Gather a list of the paragraphs using the respective heading styles. | |
ArrayList<Paragraph> parasStyleHeading1 = paragraphsByStyleName(doc, "Heading 1"); | |
ArrayList<Paragraph> parasStyleHeading3 = paragraphsByStyleName(doc, "Heading 3"); | |
// Use the first instance of the paragraphs with those styles. | |
Node startPara1 = parasStyleHeading1.get(0); | |
Node endPara1 = parasStyleHeading3.get(0); | |
// Extract the content between these nodes in the document. Don't include these markers in the extraction. | |
ArrayList<Node> extractedNodes = ExtractContentHelper.extractContent(startPara1, endPara1, false); | |
Document dstDoc = ExtractContentHelper.generateDocument(doc, extractedNodes); | |
dstDoc.save(getArtifactsDir() + "ExtractContent.ExtractContentBetweenParagraphStyles.docx"); |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
public static ArrayList<Paragraph> paragraphsByStyleName(Document doc, String styleName) | |
{ | |
// Create an array to collect paragraphs of the specified style. | |
ArrayList<Paragraph> paragraphsWithStyle = new ArrayList<Paragraph>(); | |
NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true); | |
// Look through all paragraphs to find those with the specified style. | |
for (Paragraph paragraph : (Iterable<Paragraph>) paragraphs) | |
{ | |
if (paragraph.getParagraphFormat().getStyle().getName().equals(styleName)) | |
paragraphsWithStyle.add(paragraph); | |
} | |
return paragraphsWithStyle; | |
} |
ด้านล่างเป็นผลมาจากการดำเนินการก่อนหน้านี้.
แยกเนื้อหาระหว่างการทำงานที่เฉพาะเจาะจง
คุณสามารถแยกเนื้อหาระหว่างโหนดอินไลน์เช่นRunได้เป็นอย่างดี Runsจากย่อหน้าต่างๆสามารถส่งผ่านเป็นเครื่องหมาย รหัสด้านล่างแสดงวิธีการแยกข้อความเฉพาะในระหว่างโหนดParagraphเดียวกัน.
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีแยกเนื้อหาระหว่างการทำงานเฉพาะของย่อหน้าเดียวกันโดยใช้เมธอด ExtractContent:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Extract content.docx"); | |
Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 7, true); | |
Run startRun = para.getRuns().get(1); | |
Run endRun = para.getRuns().get(4); | |
// Extract the content between these nodes in the document. Include these markers in the extraction. | |
ArrayList<Node> extractedNodes = ExtractContentHelper.extractContent(startRun, endRun, true); | |
for (Node extractedNode : extractedNodes) | |
System.out.println(extractedNode.toString(SaveFormat.TEXT)); |
ข้อความที่แยกจะแสดงบนคอนโซล
แยกเนื้อหาโดยใช้ฟิลด์
เมื่อต้องการใช้ฟิลด์เป็นเครื่องหมายโหนดFieldStart
ควรส่งผ่าน พารามิเตอร์สุดท้ายของวิธีการExtractContent
จะกำหนดว่าฟิลด์ทั้งหมดจะรวมหรือไม่ ลองดึงเนื้อหาระหว่างฟิลด์"FullName"ผสานและย่อหน้าในเอกสาร เราใช้วิธีการmoveToMergeFieldของDocumentBuilderชั้น นี้จะส่งคืนโหนดFieldStartจากชื่อของฟิลด์ผสานที่ส่งผ่านไปยังโหนดนั้น.
ในกรณีของเราให้ตั้งค่าพารามิเตอร์ที่ผ่านมาผ่านไปExtractContentวิธีการที่จะเป็นเท็จที่จะไม่รวมสนาม เราจะแสดงเนื้อหาที่สกัดเป็นPDF.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแยกเนื้อหาระหว่างฟิลด์ที่เฉพาะเจาะจงและย่อหน้าในเอกสารโดยใช้วิธีการExtractContent:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Extract content.docx"); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Pass the first boolean parameter to get the DocumentBuilder to move to the FieldStart of the field. | |
// We could also get FieldStarts of a field using GetChildNode method as in the other examples. | |
builder.moveToMergeField("Fullname", false, false); | |
// The builder cursor should be positioned at the start of the field. | |
FieldStart startField = (FieldStart) builder.getCurrentNode(); | |
Paragraph endPara = (Paragraph) doc.getFirstSection().getChild(NodeType.PARAGRAPH, 5, true); | |
// Extract the content between these nodes in the document. Don't include these markers in the extraction. | |
ArrayList<Node> extractedNodes = ExtractContentHelper.extractContent(startField, endPara, false); | |
Document dstDoc = ExtractContentHelper.generateDocument(doc, extractedNodes); | |
dstDoc.save(getArtifactsDir() + "ExtractContent.ExtractContentUsingField.docx"); |
เนื้อหาที่แยกระหว่างฟิลด์และย่อหน้าโดยไม่มีโหนดเครื่องหมายฟิลด์และย่อหน้าแสดงผลเป็นPDF.
แยกเนื้อหาจากบุ๊กมาร์ก
ในเอกสารเนื้อหาที่กำหนดภายในบุ๊กมาร์กถูกห่อหุ้มโดยโหนดBookmarkStart
และBookmarkEnd เนื้อหาที่พบระหว่างทั้งสองโหนดทำขึ้นที่คั่นหน้าเว็บ คุณสามารถส่งผ่านโหนดใดๆเหล่านี้เป็นเครื่องหมายใดๆแม้แต่คนจากบุ๊กมาร์กที่แตกต่างกันต.
ในเอกสารตัวอย่างของเราเรามีบุ๊คมาร์คหนึ่งชื่อ"บุ๊คมาร์ค 1" เนื้อหาของบุ๊กมาร์กนี้จะถูกเน้นเนื้อหาในเอกสารของเรา:
เราจะดึงเนื้อหานี้ลงในเอกสารใหม่โดยใช้รหัสด้านล่าง ตัวเลือกพารามิเตอร์IsInclusiveจะแสดงวิธีเก็บรักษาหรือยกเลิกบุ๊กมาร์ก.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแยกเนื้อหาที่อ้างอิงบุ๊กมาร์กโดยใช้วิธีการExtractContent:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Extract content.docx"); | |
Bookmark bookmark = doc.getRange().getBookmarks().get("Bookmark1"); | |
BookmarkStart bookmarkStart = bookmark.getBookmarkStart(); | |
BookmarkEnd bookmarkEnd = bookmark.getBookmarkEnd(); | |
// Firstly, extract the content between these nodes, including the bookmark. | |
ArrayList<Node> extractedNodesInclusive = ExtractContentHelper.extractContent(bookmarkStart, bookmarkEnd, true); | |
Document dstDoc = ExtractContentHelper.generateDocument(doc, extractedNodesInclusive); | |
dstDoc.save(getArtifactsDir() + "ExtractContent.ExtractContentBetweenBookmark.IncludingBookmark.docx"); | |
// Secondly, extract the content between these nodes this time without including the bookmark. | |
ArrayList<Node> extractedNodesExclusive = ExtractContentHelper.extractContent(bookmarkStart, bookmarkEnd, false); | |
dstDoc = ExtractContentHelper.generateDocument(doc, extractedNodesExclusive); | |
dstDoc.save(getArtifactsDir() + "ExtractContent.ExtractContentBetweenBookmark.WithoutBookmark.docx"); |
เอาต์พุตที่แยกกับพารามิเตอร์IsInclusive
ตั้งค่าเป็นจริง สำเนาจะเก็บบุ๊กมาร์กไว้เช่นกัน.
เอาต์พุตที่แยกกับพารามิเตอร์IsInclusiveตั้งค่าเป็นเท็จ สำเนาประกอบด้วยเนื้อหาแต่ไม่มีบุ๊กมาร์ก.
แยกเนื้อหาจากความคิดเห็น
ความคิดเห็นถูกสร้างขึ้นจากCommentRangeStart,CommentRangeEndและโหนดความคิดเห็น ทั้งหมดของโหนดเหล่านี้เป็นแบบอินไลน์ สองโหนดแรกแคปซูลเนื้อหาในเอกสารที่มีการอ้างอิงโดยความคิดเห็น,เท่าที่เห็นในภาพหน้าจอด้านล่าง.
โหนดCommentตัวเองเป็นInlineStoryที่สามารถประกอบด้วยย่อหน้าและรัน มันหมายถึงข้อความของความคิดเห็นที่เห็นเป็นฟองความคิดเห็นในบานหน้าต่างการตรวจ เป็นโหนดนี้เป็นแบบอินไลน์และลูกหลานของร่างกายคุณยังสามารถดึงเนื้อหาจากภายในข้.
ในเอกสารของเราเรามีหนึ่งความคิดเห็น ลองแสดงโดยการแสดงมาร์กอัปในแท็บรีวิว:
ความคิดเห็นที่ห่อหุ้มหัวข้อ,ย่อหน้าแรกและตารางในส่วนที่สอง. ลองดึงความคิดเห็นนี้ลงในเอกสารใหม่ อ็อพชันIsInclusiveจะบอกถ้าความคิดเห็นถูกเก็บไว้หรือถูกยกเลิก.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการทำเช่นนี้อยู่ด้านล่าง:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Extract content.docx"); | |
CommentRangeStart commentStart = (CommentRangeStart) doc.getChild(NodeType.COMMENT_RANGE_START, 0, true); | |
CommentRangeEnd commentEnd = (CommentRangeEnd) doc.getChild(NodeType.COMMENT_RANGE_END, 0, true); | |
// Firstly, extract the content between these nodes including the comment as well. | |
ArrayList<Node> extractedNodesInclusive = ExtractContentHelper.extractContent(commentStart, commentEnd, true); | |
Document dstDoc = ExtractContentHelper.generateDocument(doc, extractedNodesInclusive); | |
dstDoc.save(getArtifactsDir() + "ExtractContent.ExtractContentBetweenCommentRange.IncludingComment.docx"); | |
// Secondly, extract the content between these nodes without the comment. | |
ArrayList<Node> extractedNodesExclusive = ExtractContentHelper.extractContent(commentStart, commentEnd, false); | |
dstDoc = ExtractContentHelper.generateDocument(doc, extractedNodesExclusive); | |
dstDoc.save(getArtifactsDir() + "ExtractContent.ExtractContentBetweenCommentRange.WithoutComment.docx"); |
ประการแรกผลลัพธ์ที่สกัดด้วยพารามิเตอร์IsInclusive
ตั้งเป็นจริง สำเนาจะมีความคิดเห็นเช่นกัน.
ประการที่สองเอาต์พุตสกัดด้วยisInclusiveตั้งเป็นเท็จ สำเนาประกอบด้วยเนื้อหาแต่ไม่มีความคิดเห็น.
แยกเนื้อหาโดยใช้DocumentVisitor
Aspose.Wordsสามารถใช้ได้ไม่เพียงแต่สำหรับการสร้างเอกสารMicrosoft Wordโดยการสร้างเอกสารแบบไดนามิกหรื อีกงานที่เป็นไปได้คือการหาข้อความทั้งหมดของการจัดรูปแบบที่เฉพาะเจาะจงหรือรูปแบ.
ใช้คลาสDocumentVisitorเพื่อใช้สถานการณ์การใช้งานนี้ ชั้นนี้สอดคล้องกับรูปแบบการออกแบบของผู้เข้าชมที่รู้จักกันดี ด้วยDocumentVisitorคุณสามารถกำหนดและดำเนินการการดำเนินงานแบบกำหนดเองที่ต้องการการแจง.
DocumentVisitorให้ชุดของVisitXXXเมธอดที่ถูกเรียกใช้เมื่อพบองค์ประกอบของเอกสาร(โหนด)โดยเฉพาะอย่างยิ่ง ตัวอย่างเช่นVisitParagraphStartถูกเรียกเมื่อพบจุดเริ่มต้นของย่อหน้าข้อความและVisitParagraphEndถูกเรียกเมื่อพบจุดสิ้นสุดของย่อหน้าข้อความ แต่ละวิธีDocumentVisitor.VisitXXXจะยอมรับวัตถุที่สอดคล้องกันซึ่งพบเพื่อให้คุณสามารถใช้ได้ตามต้องการ(พูดเรียกการจัดรูปแบบ)เช่นทั้งVisitParagraphStartและVisitParagraphEndacceptวัตถุParagraph.
แต่ละเมธอดDocumentVisitor.VisitXXXจะส่งคืนค่าVisitorActionที่ควบคุมการแจงนับของโหนด คุณสามารถร้องขออย่างใดอย่างหนึ่งเพื่อดำเนินการต่อการนับข้ามโหนดปัจจุบัน(แต่ยังคง.
เหล่านี้เป็นขั้นตอนที่คุณควรปฏิบัติตามเพื่อตรวจสอบและแยกส่วนต่างๆของเอกสาร:
- สร้างคลาสที่ได้มาจากDocumentVisitor.
- แทนที่และจัดเตรียมการนำไปใช้สำหรับวิธีการDocumentVisitor.VisitXXXบางอย่างหรือทั้งหมดเพื่อดำเนินการดำเนิ.
- โทรNode.acceptบนโหนดจากตำแหน่งที่คุณต้องการเริ่มต้นการแจงนับ ตัวอย่างเช่นถ้าคุณต้องการระบุเอกสารทั้งหมดให้ใช้accept(DocumentVisitor).
DocumentVisitorจัดเตรียมการติดตั้งดีฟอลต์สำหรับเมธอดDocumentVisitor.VisitXXXทั้งหมด นี้ทำให้ง่ายต่อการสร้างผู้เข้าชมเอกสารใหม่เป็นเพียงวิธีการที่จำเป็นสำหรับผู้เข้าชมเฉ ไม่จำเป็นต้องแทนที่ทั้งหมดของวิธีการเข้าชม.
ตัวอย่างต่อไปนี้แสดงวิธีการใช้รูปแบบผู้เยี่ยมชมเพื่อเพิ่มการดำเนินงานใหม่กับรุ่นออบเจกต์Aspose.Words ในกรณีนี้เราสร้างแปลงเอกสารที่เรียบง่ายในรูปแบบข้อความ:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Extract content.docx"); | |
ConvertDocToTxt convertToPlainText = new ConvertDocToTxt(); | |
// Note that every node in the object model has the accept method so the visiting | |
// can be executed not only for the whole document, but for any node in the document. | |
doc.accept(convertToPlainText); | |
// Once the visiting is complete, we can retrieve the result of the operation, | |
// That in this example, has accumulated in the visitor. | |
System.out.println(convertToPlainText.getText()); |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
/// <summary> | |
/// Simple implementation of saving a document in the plain text format. Implemented as a Visitor. | |
/// </summary> | |
static class ConvertDocToTxt extends DocumentVisitor { | |
public ConvertDocToTxt() { | |
mIsSkipText = false; | |
mBuilder = new StringBuilder(); | |
} | |
/// <summary> | |
/// Gets the plain text of the document that was accumulated by the visitor. | |
/// </summary> | |
public String getText() { | |
return mBuilder.toString(); | |
} | |
/// <summary> | |
/// Called when a Run node is encountered in the document. | |
/// </summary> | |
public int visitRun(Run run) { | |
appendText(run.getText()); | |
// Let the visitor continue visiting other nodes. | |
return VisitorAction.CONTINUE; | |
} | |
/// <summary> | |
/// Called when a FieldStart node is encountered in the document. | |
/// </summary> | |
public int visitFieldStart(FieldStart fieldStart) { | |
// In Microsoft Word, a field code (such as "MERGEFIELD FieldName") follows | |
// after a field start character. We want to skip field codes and output field. | |
// Result only, therefore we use a flag to suspend the output while inside a field code. | |
// Note this is a very simplistic implementation and will not work very well. | |
// If you have nested fields in a document. | |
mIsSkipText = true; | |
return VisitorAction.CONTINUE; | |
} | |
/// <summary> | |
/// Called when a FieldSeparator node is encountered in the document. | |
/// </summary> | |
public int visitFieldSeparator(FieldSeparator fieldSeparator) { | |
// Once reached a field separator node, we enable the output because we are | |
// now entering the field result nodes. | |
mIsSkipText = false; | |
return VisitorAction.CONTINUE; | |
} | |
/// <summary> | |
/// Called when a FieldEnd node is encountered in the document. | |
/// </summary> | |
public int visitFieldEnd(FieldEnd fieldEnd) { | |
// Make sure we enable the output when reached a field end because some fields | |
// do not have field separator and do not have field result. | |
mIsSkipText = false; | |
return VisitorAction.CONTINUE; | |
} | |
/// <summary> | |
/// Called when visiting of a Paragraph node is ended in the document. | |
/// </summary> | |
public int visitParagraphEnd(Paragraph paragraph) { | |
// When outputting to plain text we output Cr+Lf characters. | |
appendText(ControlChar.CR_LF); | |
return VisitorAction.CONTINUE; | |
} | |
public int visitBodyStart(Body body) { | |
// We can detect beginning and end of all composite nodes such as Section, Body, | |
// Table, Paragraph etc and provide custom handling for them. | |
mBuilder.append("*** Body Started ***\r\n"); | |
return VisitorAction.CONTINUE; | |
} | |
public int visitBodyEnd(Body body) { | |
mBuilder.append("*** Body Ended ***\r\n"); | |
return VisitorAction.CONTINUE; | |
} | |
/// <summary> | |
/// Called when a HeaderFooter node is encountered in the document. | |
/// </summary> | |
public int visitHeaderFooterStart(HeaderFooter headerFooter) { | |
// Returning this value from a visitor method causes visiting of this | |
// Node to stop and move on to visiting the next sibling node | |
// The net effect in this example is that the text of headers and footers | |
// Is not included in the resulting output | |
return VisitorAction.SKIP_THIS_NODE; | |
} | |
/// <summary> | |
/// Adds text to the current output. Honors the enabled/disabled output flag. | |
/// </summary> | |
private void appendText(String text) { | |
if (!mIsSkipText) | |
mBuilder.append(text); | |
} | |
private StringBuilder mBuilder; | |
private boolean mIsSkipText; | |
} |
แยกข้อความเท่านั้น
วิธีการดึงข้อความจากเอกสารมีดังนี้:
- ใช้Document.saveด้วยSaveFormatเพื่อบันทึกเป็นข้อความธรรมดาลงในแฟ้มหรือสตรีม
- ใช้Node.toStringและส่งพารามิเตอร์
SaveFormat.Text
ภายในนี้เรียกบันทึกเป็นข้อความลงในกระแสหน่วยความจำและส่งกลับสตริงผลลัพธ์ - ใช้Node.getTextเพื่อดึงข้อความที่มีอักขระควบคุมทั้งหมดMicrosoft Wordรวมทั้งรหัสฟิลด์
- ใช้แบบกำหนดเองDocumentVisitorเพื่อทำการสกัดแบบกำหนดเอง
ใช้Node.GetText
และNode.ToString
เอกสารคำสามารถประกอบด้วยอักขระควบคุมที่กำหนดองค์ประกอบพิเศษเช่นฟิลด์สิ้นสุดข รายการอักขระตัวควบคุมคำที่เป็นไปได้ทั้งหมดจะถูกกำหนดในคลาสของControlChar เมธอดGetTextจะส่งคืนข้อความที่มีอักขระตัวควบคุมทั้งหมดที่อยู่ในโหนด.
การโทรToStringจะส่งคืนการแสดงข้อความธรรมดาของเอกสารโดยไม่มีอักขระควบคุมเท่านั้น สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการส่งออกเป็นข้อความธรรมดาโปรดดูที่Using SaveFormat.Text.
ตัวอย่างรหัสต่อไปนี้แสดงความแตกต่างระหว่างการโทรGetTextและToStringเมธอดบนโหนด:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.insertField("MERGEFIELD Field"); | |
// When converted to text it will not retrieve fields code or special characters, | |
// but will still contain some natural formatting characters such as paragraph markers etc. | |
// This is the same as "viewing" the document as if it was opened in a text editor. | |
System.out.println("ToString() Result: " + doc.toString(SaveFormat.TEXT)); |
ใช้SaveFormat.Text
ตัวอย่างนี้บันทึกเอกสารดังนี้:
- กรองอักขระฟิลด์และรหัสฟิลด์,รูปร่าง,เชิงอรรถ,จุดสิ้นสุดและการอ้างอิงแสดงความคิดเห็น
- แทนที่จุดสิ้นสุดของย่อหน้าControlChar.Crอักขระด้วยชุดค่าผสมControlChar.CrLf
- ใช้UTF8การเข้ารหัส
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการบันทึกเอกสารในรูปแบบTXT:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Document.docx"); | |
doc.save(getArtifactsDir() + "BaseConversions.DocxToTxt.txt"); |
แยกภาพจากรูปร่าง
คุณอาจต้องดึงรูปภาพเอกสารเพื่อดำเนินการบางอย่าง Aspose.Wordsช่วยให้คุณสามารถทำเช่นนี้ได้เช่นกัน.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแยกภาพจากเอกสาร:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Images.docx"); | |
NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true); | |
int imageIndex = 0; | |
for (Shape shape : (Iterable<Shape>) shapes) { | |
if (shape.hasImage()) { | |
String imageFileName = | |
MessageFormat.format("Image.ExportImages.{0}_{1}", imageIndex, FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType())); | |
// Note, if you have only an image (not a shape with a text and the image), | |
// you can use shape.getShapeRenderer().save(...) method to save the image. | |
shape.getImageData().save(getArtifactsDir() + imageFileName); | |
imageIndex++; | |
} | |
} |