แทรกและผนวกเอกสาร

บางครั้งจำเป็นต้องรวมเอกสารหลายฉบับเป็นเอกสารเดียว คุณสามารถดำเนินการนี้ได้ด้วยตนเองหรือใช้คุณลักษณะการแทรกหรือต่อท้าย Aspose.Words

การแทรกช่วยให้คุณสามารถแทรกเนื้อหาของเอกสารที่สร้างไว้ก่อนหน้านี้ลงในเอกสารใหม่หรือที่มีอยู่ได้

ในทางกลับกัน คุณลักษณะการผนวกช่วยให้คุณสามารถเพิ่มเอกสารได้เฉพาะที่ส่วนท้ายของเอกสารอื่นเท่านั้น

บทความนี้จะอธิบายวิธีการแทรกหรือผนวกเอกสารเข้ากับเอกสารอื่นด้วยวิธีต่างๆ และอธิบายคุณสมบัติทั่วไปที่คุณสามารถใช้ได้ในขณะที่แทรกหรือผนวกเอกสาร

แทรกเอกสาร

ตามที่กล่าวไว้ข้างต้น ใน Aspose.Words เอกสารจะแสดงเป็นแผนผังของโหนด และการดำเนินการแทรกเอกสารหนึ่งไปยังอีกเอกสารหนึ่งคือการคัดลอกโหนดจากแผนผังเอกสารแรกไปยังเอกสารที่สอง

คุณสามารถแทรกเอกสารในตำแหน่งต่างๆ ได้หลายวิธี ตัวอย่างเช่น คุณสามารถแทรกเอกสารผ่านการดำเนินการแทนที่ ฟิลด์ผสานระหว่างการดำเนินการผสาน หรือผ่านทางบุ๊กมาร์ก

คุณยังสามารถใช้วิธี insert_document หรือ insert_document_inline ซึ่งคล้ายกับการแทรกเอกสารใน Microsoft Word เพื่อแทรกเอกสารทั้งหมดที่ตำแหน่งเคอร์เซอร์ปัจจุบันโดยไม่ต้องนำเข้าก่อนหน้านี้

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกเอกสารโดยใช้วิธี insert_document:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
src_doc = aw.Document(MY_DIR + "Document source.docx")
dst_doc = aw.Document(MY_DIR + "Northwind traders.docx")
builder = aw.DocumentBuilder(dst_doc)
builder.move_to_document_end()
builder.insert_break(aw.BreakType.PAGE_BREAK)
builder.insert_document(src_doc, aw.ImportFormatMode.KEEP_SOURCE_FORMATTING)
builder.document.save(ARTIFACTS_DIR + "JoinAndAppendDocuments.insert_document.docx")

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกเอกสารโดยใช้วิธี insert_document_inline:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
src_doc = aw.DocumentBuilder()
src_doc.write("[src content]")
# Create destination document.
dst_doc = aw.DocumentBuilder()
dst_doc.write("Before ")
dst_doc.insert_node(aw.BookmarkStart(dst_doc.document, "src_place"))
dst_doc.insert_node(aw.BookmarkEnd(dst_doc.document, "src_place"))
dst_doc.write(" after")
self.assertEqual("Before after", dst_doc.document.get_text().strip())
# Insert source document into destination inline.
dst_doc.move_to_bookmark("src_place")
dst_doc.insert_document_inline(src_doc.document, aw.ImportFormatMode.USE_DESTINATION_STYLES, aw.ImportFormatOptions())
self.assertEqual("Before [src content] after", dst_doc.document.get_text().strip())

ส่วนย่อยต่อไปนี้จะอธิบายตัวเลือกต่างๆ ที่คุณสามารถแทรกเอกสารหนึ่งลงในอีกเอกสารหนึ่งได้

แทรกเอกสารที่บุ๊กมาร์ก

คุณสามารถนำเข้าไฟล์ข้อความลงในเอกสารและแทรกไว้หลังบุ๊กมาร์กที่คุณกำหนดไว้ในเอกสารได้ เมื่อต้องการทำเช่นนี้ ให้สร้างย่อหน้าบุ๊กมาร์กที่คุณต้องการแทรกเอกสาร

ตัวอย่างการเข้ารหัสต่อไปนี้แสดงวิธีการแทรกเนื้อหาของเอกสารหนึ่งไปยังบุ๊กมาร์กในเอกสารอื่น:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
main_doc = aw.Document(MY_DIR + "Document insertion 1.docx")
sub_doc = aw.Document(MY_DIR + "Document insertion 2.docx")
bookmark = main_doc.range.bookmarks.get_by_name("insertionPlace")
self.insert_document(bookmark.bookmark_start.parent_node, sub_doc)
main_doc.save(ARTIFACTS_DIR + "CloneAndCombineDocuments.insert_document_at_bookmark.docx")

ผนวกเอกสาร

คุณอาจมีกรณีการใช้งานที่คุณต้องรวมหน้าเพิ่มเติมจากเอกสารต่อท้ายเอกสารที่มีอยู่ ในการดำเนินการนี้คุณเพียงแค่ต้องเรียกใช้เมธอด append_document เพื่อเพิ่มเอกสารต่อท้ายเอกสารอื่น

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการผนวกเอกสารต่อท้ายเอกสารอื่น:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
dst_doc = aw.Document()
dst_doc.first_section.body.append_paragraph("Destination document text. ")
src_doc = aw.Document()
src_doc.first_section.body.append_paragraph("Source document text. ")
# Append the source document to the destination document.
# Pass format mode to retain the original formatting of the source document when importing it.
dst_doc.append_document(src_doc, aw.ImportFormatMode.KEEP_SOURCE_FORMATTING)
dst_doc.save(ARTIFACTS_DIR + "JoinAndAppendDocuments.keep_source_formatting.docx")

นำเข้าและแทรกโหนดด้วยตนเอง

Aspose.Words ช่วยให้คุณสามารถแทรกและต่อท้ายเอกสารโดยอัตโนมัติ โดยไม่ต้องมีข้อกำหนดการนำเข้าใดๆ ก่อนหน้านี้ อย่างไรก็ตาม หากคุณต้องการแทรกหรือต่อท้ายโหนดเฉพาะของเอกสารของคุณ เช่น ส่วนหรือย่อหน้า คุณต้องนำเข้าโหนดนี้ด้วยตนเองก่อน

เมื่อคุณต้องการแทรกหรือต่อท้ายส่วนหรือย่อหน้าหนึ่งเข้ากับอีกส่วนหนึ่ง คุณจะต้องนำเข้าโหนดของโครงสร้างโหนดเอกสารแรกไปยังส่วนที่สองโดยใช้วิธี import_node หลังจากนำเข้าโหนดของคุณแล้ว คุณต้องใช้วิธี insert_after / insert_before เพื่อแทรกโหนดใหม่หลัง/ก่อนโหนดอ้างอิง สิ่งนี้ช่วยให้คุณปรับแต่งกระบวนการแทรกได้โดยการนำเข้าโหนดจากเอกสารและแทรกในตำแหน่งที่กำหนด

คุณยังสามารถใช้วิธี append_child เพื่อเพิ่มโหนดที่ระบุใหม่ไปที่ส่วนท้ายของรายการโหนดย่อยได้ เช่น หากคุณต้องการต่อท้ายเนื้อหาในระดับย่อหน้า แทนที่จะเป็นระดับส่วน

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการนำเข้าโหนดด้วยตนเองและแทรกไว้หลังโหนดเฉพาะโดยใช้วิธี insert_after:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
@staticmethod
def insert_document(insertion_destination: aw.Node, doc_to_insert: aw.Document):
"""Inserts content of the external document after the specified node.
Section breaks and section formatting of the inserted document are ignored.
:param insertion_destination: Node in the destination document after which the content
Should be inserted. This node should be a block level node (paragraph or table).
:param doc_to_insert: The document to insert.
"""
if insertion_destination.node_type not in (aw.NodeType.PARAGRAPH, aw.NodeType.TABLE):
raise ValueError("The destination node should be either a paragraph or table.")
destination_parent = insertion_destination.parent_node
importer = aw.NodeImporter(doc_to_insert, insertion_destination.document, aw.ImportFormatMode.KEEP_SOURCE_FORMATTING)
# Loop through all block-level nodes in the section's body,
# then clone and insert every node that is not the last empty paragraph of a section.
for src_section in doc_to_insert.sections:
for src_node in src_section.as_section().body.get_child_nodes(aw.NodeType.ANY, False):
if src_node.node_type == aw.NodeType.PARAGRAPH:
para = src_node.as_paragraph()
if para.is_end_of_section and not para.has_child_nodes:
continue
new_node = importer.import_node(src_node, True)
destination_parent.insert_after(new_node, insertion_destination)
insertion_destination = new_node

เนื้อหาจะถูกนำเข้าไปยังส่วนเอกสารปลายทางทีละส่วน ซึ่งหมายความว่าการตั้งค่า เช่น การตั้งค่าหน้าและส่วนหัวหรือส่วนท้าย จะถูกรักษาไว้ในระหว่างการนำเข้า นอกจากนี้ โปรดทราบว่าคุณสามารถกำหนดการตั้งค่าการจัดรูปแบบได้เมื่อคุณแทรกหรือต่อท้ายเอกสารเพื่อระบุวิธีการรวมเอกสารทั้งสองเข้าด้วยกัน

คุณสมบัติทั่วไปสำหรับการแทรกและผนวกเอกสาร

ทั้งวิธี insert_document และ append_document ยอมรับ ImportFormatMode และ ImportFormatOptions เป็นพารามิเตอร์อินพุต ImportFormatMode ช่วยให้คุณควบคุมวิธีการผสานการจัดรูปแบบเอกสารเมื่อคุณนำเข้าเนื้อหาจากเอกสารหนึ่งไปยังอีกเอกสารหนึ่งโดยการเลือกโหมดรูปแบบที่แตกต่างกัน เช่น USE_DESTINATION_STYLES, KEEP_SOURCE_FORMATTING และ KEEP_DIFFERENT_STYLES ImportFormatOptions ช่วยให้คุณเลือกตัวเลือกการนำเข้าต่างๆ เช่น ignore_header_footer, ignore_text_boxes, keep_source_numbering, merge_pasted_lists และ smart_style_behavior

Aspose.Words ช่วยให้คุณปรับการแสดงภาพของเอกสารผลลัพธ์เมื่อมีการเพิ่มเอกสารสองฉบับเข้าด้วยกันในการแทรกหรือผนวกโดยใช้ Section และ PageSetup คุณสมบัติ page_setup มีแอตทริบิวต์ทั้งหมดของส่วน เช่น section_start, restart_page_numbering, page_starting_number, orientation และอื่นๆ กรณีการใช้งานที่พบบ่อยที่สุดคือการตั้งค่าคุณสมบัติ section_start เพื่อกำหนดว่าเนื้อหาที่เพิ่มจะปรากฏในหน้าเดียวกันหรือแยกเป็นเนื้อหาใหม่

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการผนวกเอกสารหนึ่งไปยังอีกเอกสารหนึ่งโดยป้องกันไม่ให้เนื้อหาแยกออกเป็นสองหน้า:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
src_doc = aw.Document(MY_DIR + "Document source.docx")
dst_doc = aw.Document(MY_DIR + "Northwind traders.docx")
# Set the source document to continue straight after the end of the destination document.
src_doc.first_section.page_setup.section_start = aw.SectionStart.CONTINUOUS
# Restart the page numbering on the start of the source document.
src_doc.first_section.page_setup.restart_page_numbering = True
src_doc.first_section.page_setup.page_starting_number = 1
# To ensure this does not happen when the source document has different page setup settings, make sure the
# settings are identical between the last section of the destination document.
# If there are further continuous sections that follow on in the source document,
# this will need to be repeated for those sections.
src_doc.first_section.page_setup.page_width = dst_doc.last_section.page_setup.page_width
src_doc.first_section.page_setup.page_height = dst_doc.last_section.page_setup.page_height
src_doc.first_section.page_setup.orientation = dst_doc.last_section.page_setup.orientation
# Iterate through all sections in the source document.
for para in src_doc.get_child_nodes(aw.NodeType.PARAGRAPH, True):
para = para.as_paragraph()
para.paragraph_format.keep_with_next = True
dst_doc.append_document(src_doc, aw.ImportFormatMode.KEEP_SOURCE_FORMATTING)
dst_doc.save(ARTIFACTS_DIR + "JoinAndAppendDocuments.different_page_setup.docx")