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

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

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

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

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

แทรกเอกสาร

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

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document srcDoc = new Document(getMyDir() + "Document source.docx");
Document dstDoc = new Document(getMyDir() + "Northwind traders.docx");
DocumentBuilder builder = new DocumentBuilder(dstDoc);
builder.moveToDocumentEnd();
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
builder.getDocument().save(getArtifactsDir() + "JoinAndAppendDocuments.insertDocument.docx");

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
DocumentBuilder srcDoc = new DocumentBuilder();
srcDoc.write("[src content]");
// Create destination document.
DocumentBuilder dstDoc = new DocumentBuilder();
dstDoc.write("Before ");
dstDoc.insertNode(new BookmarkStart(dstDoc.getDocument(), "src_place"));
dstDoc.insertNode(new BookmarkEnd(dstDoc.getDocument(), "src_place"));
dstDoc.write(" after");
Assert.assertEquals("Before after", dstDoc.getDocument().getText().trim());
// Insert source document into destination inline.
dstDoc.moveToBookmark("src_place");
dstDoc.insertDocumentInline(srcDoc.getDocument(), ImportFormatMode.USE_DESTINATION_STYLES, new ImportFormatOptions());
Assert.assertEquals("Before [src content] after", dstDoc.getDocument().getText().trim());

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

แทรกเอกสารระหว่างการค้นหาและแทนที่การทำงาน

คุณสามารถแทรกเอกสารขณะดำเนินการค้นหาและแทนที่การดำเนินงาน ตัวอย่างเช่นเอกสารสามารถประกอบด้วยย่อหน้าที่มีข้อความ[INTRODUCTION]และ[CONCLUSION] แต่ในเอกสารสุดท้ายคุณจะต้องเปลี่ยนย่อหน้าเหล่านั้นด้วยเนื้อหาที่ได้รับจากเอกสารภายนอกอื่น เพื่อให้บรรลุว่า,คุณจะต้องสร้างตัวจัดการสำหรับเหตุการณ์แทนที่.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการสร้างตัวจัดการสำหรับเหตุการณ์แทนที่จะใช้ในภายหลังใ:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
private static class InsertDocumentAtReplaceHandler implements IReplacingCallback
{
public /*ReplaceAction*/int /*IReplacingCallback.*/replacing(ReplacingArgs args) throws Exception
{
Document subDoc = new Document(getMyDir() + "Document insertion 2.docx");
// Insert a document after the paragraph, containing the match text.
Paragraph para = (Paragraph)args.getMatchNode().getParentNode();
insertDocument(para, subDoc);
// Remove the paragraph with the match text.
para.remove();
return ReplaceAction.SKIP;
}
}

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document mainDoc = new Document(getMyDir() + "Document insertion 1.docx");
// Set find and replace options.
FindReplaceOptions options = new FindReplaceOptions();
{
options.setDirection(FindReplaceDirection.BACKWARD);
options.setReplacingCallback(new InsertDocumentAtReplaceHandler());
}
// Call the replace method.
mainDoc.getRange().replace(Pattern.compile("\\[MY_DOCUMENT\\]"), "", options);
mainDoc.save(getArtifactsDir() + "CloneAndCombineDocuments.InsertDocumentAtReplace.docx");

แทรกเอกสารระหว่างการทำงานMail Merge

คุณสามารถแทรกเอกสารลงในฟิลด์ผสานระหว่างการดำเนินการMail Merge ตัวอย่างเช่นเทมเพลตMail Mergeสามารถมีฟิลด์ผสานเช่น[สรุป] แต่ในเอกสารสุดท้ายคุณต้องแทรกเนื้อหาที่ได้รับจากเอกสารภายนอกอื่นลงในฟิลด์นี้ผสาน เพื่อให้บรรลุที่,คุณจะต้องสร้างตัวจัดการสำหรับเหตุการณ์ผสาน.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการสร้างตัวจัดการสำหรับเหตุการณ์การรวมการใช้ในภายห:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
private static class InsertDocumentAtMailMergeHandler implements IFieldMergingCallback
{
// This handler makes special processing for the "Document_1" field.
// The field value contains the path to load the document.
// We load the document and insert it into the current merge field.
public void /*IFieldMergingCallback.*/fieldMerging(FieldMergingArgs args) throws Exception
{
if ("Document_1".equals(args.getDocumentFieldName()))
{
// Use document builder to navigate to the merge field with the specified name.
DocumentBuilder builder = new DocumentBuilder(args.getDocument());
builder.moveToMergeField(args.getDocumentFieldName());
// The name of the document to load and insert is stored in the field value.
Document subDoc = new Document((String)args.getFieldValue());
insertDocument(builder.getCurrentParagraph(), subDoc);
// The paragraph that contained the merge field might be empty now, and you probably want to delete it.
if (!builder.getCurrentParagraph().hasChildNodes())
builder.getCurrentParagraph().remove();
// Indicate to the mail merge engine that we have inserted what we wanted.
args.setText(null);
}
}
public void /*IFieldMergingCallback.*/imageFieldMerging(ImageFieldMergingArgs args)
{
// Do nothing.
}
}

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกเอกสารลงในฟิลด์ผสานโดยใช้ตัวจัดการที่สร้างขึ้น:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document mainDoc = new Document(getMyDir() + "Document insertion 1.docx");
mainDoc.getMailMerge().setFieldMergingCallback(new InsertDocumentAtMailMergeHandler());
// The main document has a merge field in it called "Document_1".
// The corresponding data for this field contains a fully qualified path to the document.
// That should be inserted to this field.
mainDoc.getMailMerge().execute(new String[] { "Document_1" }, new Object[] { getMyDir() + "Document insertion 2.docx" });
mainDoc.save(getArtifactsDir() + "CloneAndCombineDocuments.InsertDocumentAtMailMerge.doc");

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document mainDoc = new Document(getMyDir() + "Document insertion 1.docx");
Document subDoc = new Document(getMyDir() + "Document insertion 2.docx");
Bookmark bookmark = mainDoc.getRange().getBookmarks().get("insertionPlace");
insertDocument(bookmark.getBookmarkStart().getParentNode(), subDoc);
mainDoc.save(getArtifactsDir() + "CloneAndCombineDocuments.InsertDocumentAtBookmark.docx");

ต่อท้ายเอกสาร

คุณอาจมีกรณีการใช้งานที่คุณต้องการรวมหน้าเพิ่มเติมจากเอกสารไปยังจุดสิ้นสุดของเอก การทำเช่นนี้,คุณเพียงแค่ต้องเรียกวิธีการAppendDocumentเพื่อเพิ่มเอกสารไปยังจุดสิ้นสุดของอีกคนหนึ่ง.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการต่อท้ายเอกสารไปยังจุดสิ้นสุดของเอกสารอื่น:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document dstDoc = new Document();
dstDoc.getFirstSection().getBody().appendParagraph("Destination document text. ");
Document srcDoc = new Document();
srcDoc.getFirstSection().getBody().appendParagraph("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.
dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
dstDoc.save(getArtifactsDir() + "JoinAndAppendDocuments.KeepSourceFormatting.docx");

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

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

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
/// <summary>
/// Inserts content of the external document after the specified node.
/// Section breaks and section formatting of the inserted document are ignored.
/// </summary>
/// <param name="insertionDestination">Node in the destination document after which the content
/// Should be inserted. This node should be a block level node (paragraph or table).</param>
/// <param name="docToInsert">The document to insert.</param>
private static void insertDocument(Node insertionDestination, Document docToInsert)
{
if (insertionDestination.getNodeType() == NodeType.PARAGRAPH || insertionDestination.getNodeType() == NodeType.TABLE)
{
CompositeNode destinationParent = insertionDestination.getParentNode();
NodeImporter importer =
new NodeImporter(docToInsert, insertionDestination.getDocument(), 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 (Section srcSection : docToInsert.getSections())
for (Node srcNode : srcSection.getBody())
{
if (srcNode.getNodeType() == NodeType.PARAGRAPH)
{
Paragraph para = (Paragraph)srcNode;
if (para.isEndOfSection() && !para.hasChildNodes())
continue;
}
Node newNode = importer.importNode(srcNode, true);
destinationParent.insertAfter(newNode, insertionDestination);
insertionDestination = newNode;
}
}
else
{
throw new IllegalArgumentException("The destination node should be either a paragraph or table.");
}
}

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

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

ทั้งวิธีการInsertDocumentและAppendDocumentยอมรับImportFormatModeและImportFormatOptionsเป็นพารามิเตอร์อินพุต ImportFormatModeช่วยให้คุณสามารถควบคุมวิธีการรวมการจัดรูปแบบเอกสารเมื่อคุณนำเข้าเนื้อหาจากเอกสารหนึ่งไปยังอีกเอกสารหนึ่งโดยเลือกโหมดรูปแบบต่างๆเช่นUseDestinationStyles,KeepSourceFormattingและKeepDifferentStyles ImportFormatOptionsช่วยให้คุณสามารถเลือกตัวเลือกการนำเข้าที่แตกต่างกันเช่นIgnoreHeaderFooter, IgnoreTextBoxes, KeepSourceNumbering, MergePastedLists, และSmartStyleBehavior.

Aspose.Wordsช่วยให้คุณสามารถปรับการแสดงภาพของเอกสารที่เกิดขึ้นเมื่อเอกสารสองฉบับถูกเพิ่มเข้าด้วยกันในการแทรกหรือต่อท้ายการทำงานโดยใช้คุณสมบัติSectionและPageSetup คุณสมบัติPageSetupมีคุณลักษณะทั้งหมดของส่วนเช่นSectionStart, RestartPageNumbering, PageStartingNumber, Orientation, และอื่นๆ กรณีการใช้งานที่พบบ่อยที่สุดคือการตั้งค่าคุณสมบัติSectionStartเพื่อกำหนดว่าเนื้อหาที่เพิ่มจะปรากฏใน.

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการต่อท้ายเอกสารหนึ่งไปยังอีกในขณะที่รักษาเนื้อหาจากกา:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document srcDoc = new Document(getMyDir() + "Document source.docx");
Document dstDoc = new Document(getMyDir() + "Northwind traders.docx");
// Set the source document to continue straight after the end of the destination document.
srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
// Restart the page numbering on the start of the source document.
srcDoc.getFirstSection().getPageSetup().setRestartPageNumbering(true);
srcDoc.getFirstSection().getPageSetup().setPageStartingNumber(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.
srcDoc.getFirstSection().getPageSetup().setPageWidth(dstDoc.getLastSection().getPageSetup().getPageWidth());
srcDoc.getFirstSection().getPageSetup().setPageHeight(dstDoc.getLastSection().getPageSetup().getPageHeight());
srcDoc.getFirstSection().getPageSetup().setOrientation(dstDoc.getLastSection().getPageSetup().getOrientation());
// Iterate through all sections in the source document.
for (Paragraph para : (Iterable<Paragraph>) srcDoc.getChildNodes(NodeType.PARAGRAPH, true))
{
para.getParagraphFormat().setKeepWithNext(true);
}
dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
dstDoc.save(getArtifactsDir() + "JoinAndAppendDocuments.DifferentPageSetup.docx");