แทรกและผนวกเอกสาร
บางครั้งก็จะต้องรวมเอกสารหลายเป็นหนึ่ง คุณสามารถทำเช่นนี้ด้วยตนเองหรือคุณสามารถใช้คุณลักษณะแทรกAspose.Wordsหรือต่อท้าย.
การดำเนินการแทรกช่วยให้คุณสามารถแทรกเนื้อหาของเอกสารที่สร้างไว้ก่อนหน้านี้ลงใ.
ในทางกลับกันคุณลักษณะภาคผนวกช่วยให้คุณสามารถเพิ่มเอกสารเฉพาะที่ส่วนท้ายของเอ.
บทความนี้อธิบายวิธีการแทรกหรือต่อท้ายเอกสารไปยังอีกหนึ่งวิธีอื่นและอธิบายถึงคุณสมบั.
แทรกเอกสาร
ดังกล่าวข้างต้นในAspose.Wordsเอกสารจะแสดงเป็นต้นไม้ของโหนดและการดำเนินงานของการแทรก.
คุณสามารถแทรกเอกสารในสถานที่ต่างๆได้หลายรูปแบบ ตัวอย่างเช่นคุณสามารถแทรกเอกสารผ่านการดำเนินการแทนที่ฟิลด์ผสานระหว่างการ.
คุณยังสามารถใช้วิธีการInsertDocumentซึ่งคล้ายกับการแทรกเอกสารในMicrosoft Wordเพื่อแทรกเอกสารทั้งหมดที่ตำแ.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกเอกสารโดยใช้วิธีการInsertDocument
:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
auto srcDoc = MakeObject<Document>(MyDir + u"Document source.docx"); | |
auto dstDoc = MakeObject<Document>(MyDir + u"Northwind traders.docx"); | |
auto builder = MakeObject<DocumentBuilder>(dstDoc); | |
builder->MoveToDocumentEnd(); | |
builder->InsertBreak(BreakType::PageBreak); | |
builder->InsertDocument(srcDoc, ImportFormatMode::KeepSourceFormatting); | |
builder->get_Document()->Save(ArtifactsDir + u"JoinAndAppendDocuments.InsertDocument.docx"); |
ย่อยต่อไปนี้อธิบายตัวเลือกในระหว่างที่คุณสามารถแทรกเอกสารหนึ่งไปยังอีก.
แทรกเอกสารระหว่างการค้นหาและแทนที่การทำงาน
คุณสามารถแทรกเอกสารขณะดำเนินการค้นหาและแทนที่การดำเนินงาน ตัวอย่างเช่นเอกสารสามารถประกอบด้วยย่อหน้าที่มีข้อความ[INTRODUCTION]และ[CONCLUSION] แต่ในเอกสารสุดท้ายคุณจะต้องเปลี่ยนย่อหน้าเหล่านั้นด้วยเนื้อหาที่ได้รับจากเอกสารภายนอกอื่น เพื่อให้บรรลุว่า,คุณจะต้องสร้างตัวจัดการสำหรับเหตุการณ์แทนที่.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการสร้างตัวจัดการสำหรับเหตุการณ์แทนที่จะใช้ในภายหลังใ:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
class InsertDocumentAtReplaceHandler : public IReplacingCallback | |
{ | |
private: | |
ReplaceAction Replacing(SharedPtr<ReplacingArgs> args) override | |
{ | |
auto subDoc = MakeObject<Document>(MyDir + u"Document insertion 2.docx"); | |
// Insert a document after the paragraph, containing the match text. | |
auto para = System::ExplicitCast<Paragraph>(args->get_MatchNode()->get_ParentNode()); | |
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-C.git. | |
auto mainDoc = MakeObject<Document>(MyDir + u"Document insertion 1.docx"); | |
// Set find and replace options. | |
auto options = MakeObject<FindReplaceOptions>(); | |
options->set_Direction(FindReplaceDirection::Backward); | |
options->set_ReplacingCallback(MakeObject<CloneAndCombineDocuments::InsertDocumentAtReplaceHandler>()); | |
// Call the replace method. | |
mainDoc->get_Range()->Replace(MakeObject<System::Text::RegularExpressions::Regex>(u"\\[MY_DOCUMENT\\]"), u"", options); | |
mainDoc->Save(ArtifactsDir + u"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-C.git. | |
class InsertDocumentAtMailMergeHandler : public IFieldMergingCallback | |
{ | |
private: | |
void FieldMerging(SharedPtr<FieldMergingArgs> args) override | |
{ | |
if (args->get_DocumentFieldName() == u"Document_1") | |
{ | |
// Use document builder to navigate to the merge field with the specified name. | |
auto builder = MakeObject<DocumentBuilder>(args->get_Document()); | |
builder->MoveToMergeField(args->get_DocumentFieldName()); | |
// The name of the document to load and insert is stored in the field value. | |
auto subDoc = MakeObject<Document>(System::ObjectExt::Unbox<String>(args->get_FieldValue())); | |
InsertDocument(builder->get_CurrentParagraph(), subDoc); | |
// The paragraph that contained the merge field might be empty now, and you probably want to delete it. | |
if (!builder->get_CurrentParagraph()->get_HasChildNodes()) | |
{ | |
builder->get_CurrentParagraph()->Remove(); | |
} | |
// Indicate to the mail merge engine that we have inserted what we wanted. | |
args->set_Text(nullptr); | |
} | |
} | |
void ImageFieldMerging(SharedPtr<ImageFieldMergingArgs> args) override | |
{ | |
ASPOSE_UNUSED(args); | |
// Do nothing. | |
} | |
}; |
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกเอกสารลงในฟิลด์ผสานโดยใช้ตัวจัดการที่สร้างขึ้น:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
auto mainDoc = MakeObject<Document>(MyDir + u"Document insertion 1.docx"); | |
mainDoc->get_MailMerge()->set_FieldMergingCallback(MakeObject<CloneAndCombineDocuments::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->get_MailMerge()->Execute(MakeArray<String>({u"Document_1"}), | |
MakeArray<SharedPtr<System::Object>>({System::ObjectExt::Box<String>(MyDir + u"Document insertion 2.docx")})); | |
mainDoc->Save(ArtifactsDir + u"CloneAndCombineDocuments.InsertDocumentAtMailMerge.doc"); |
แทรกเอกสารที่บุ๊กมาร์ก
คุณสามารถนำเข้าแฟ้มข้อความลงในเอกสารและแทรกทันทีหลังจากที่บุ๊กมาร์กที่คุณได้กำหน เมื่อต้องการทำเช่นนี้สร้างย่อหน้าที่คั่นหน้าที่คุณต้องการแทรกเอกสาร.
ตัวอย่างการเข้ารหัสต่อไปนี้แสดงวิธีการแทรกเนื้อหาของเอกสารหนึ่งไปยังคั่นหน้าในเอก:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
auto mainDoc = MakeObject<Document>(MyDir + u"Document insertion 1.docx"); | |
auto subDoc = MakeObject<Document>(MyDir + u"Document insertion 2.docx"); | |
SharedPtr<Bookmark> bookmark = mainDoc->get_Range()->get_Bookmarks()->idx_get(u"insertionPlace"); | |
InsertDocument(bookmark->get_BookmarkStart()->get_ParentNode(), subDoc); | |
mainDoc->Save(ArtifactsDir + u"CloneAndCombineDocuments.InsertDocumentAtBookmark.docx"); |
ต่อท้ายเอกสาร
คุณอาจมีกรณีการใช้งานที่คุณต้องการรวมหน้าเพิ่มเติมจากเอกสารไปยังจุดสิ้นสุดของเอก การทำเช่นนี้,คุณเพียงแค่ต้องเรียกวิธีการAppendDocumentเพื่อเพิ่มเอกสารไปยังจุดสิ้นสุดของอีกคนหนึ่ง.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการต่อท้ายเอกสารไปยังจุดสิ้นสุดของเอกสารอื่น:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
auto dstDoc = MakeObject<Document>(); | |
dstDoc->get_FirstSection()->get_Body()->AppendParagraph(u"Destination document text. "); | |
auto srcDoc = MakeObject<Document>(); | |
srcDoc->get_FirstSection()->get_Body()->AppendParagraph(u"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::KeepSourceFormatting); | |
dstDoc->Save(ArtifactsDir + u"JoinAndAppendDocuments.KeepSourceFormatting.docx"); |
นำเข้าและแทรกโหนดด้วยตนเอง
Aspose.Wordsช่วยให้คุณสามารถแทรกและผนวกเอกสารโดยอัตโนมัติโดยไม่มีข้อกำหนดการนำเข้าก่อนหน้านี้ อย่างไรก็ตามถ้าคุณต้องการแทรกหรือต่อท้ายโหนดเฉพาะของเอกสารของคุณเช่นส่วนหรื.
เมื่อคุณต้องการแทรกหรือต่อท้ายส่วนหรือย่อหน้าหนึ่งไปยังอีกส่วนหนึ่งคุณจะต้องอิมพอร์ตโหนดของโครงสร้างโหนดเอกสารแรกลงในโหนดที่สองโดยใช้วิธีการImportNode หลังจากอิมพอร์ตโหนดของคุณคุณต้องใช้วิธีการInsertAfter/InsertBeforeเพื่อแทรกโหนดใหม่หลัง/ก่อนโหนดอ้างอิง นี้ช่วยให้คุณสามารถกำหนดกระบวนการแทรกโดยการอิมพอร์ตโหนดจากเอกสารและแทรก.
นอกจากนี้คุณยังสามารถใช้วิธีการAppendChildเพื่อเพิ่มโหนดที่ระบุใหม่ในตอนท้ายของรายการของโหนด.
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีอิมพอร์ตโหนดด้วยตนเองและแทรกโหนดเหล่านี้หลังจากโหนดที่เฉพาะเจาะจงโดยใช้วิธีการInsertAfter:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.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> | |
static void InsertDocument(SharedPtr<Node> insertionDestination, SharedPtr<Document> docToInsert) | |
{ | |
if (insertionDestination->get_NodeType() == NodeType::Paragraph || insertionDestination->get_NodeType() == NodeType::Table) | |
{ | |
SharedPtr<CompositeNode> destinationParent = insertionDestination->get_ParentNode(); | |
auto importer = MakeObject<NodeImporter>(docToInsert, insertionDestination->get_Document(), ImportFormatMode::KeepSourceFormatting); | |
// 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 (const auto& srcSection : System::IterateOver(docToInsert->get_Sections()->LINQ_OfType<SharedPtr<Section>>())) | |
{ | |
for (const auto& srcNode : System::IterateOver(srcSection->get_Body())) | |
{ | |
if (srcNode->get_NodeType() == NodeType::Paragraph) | |
{ | |
auto para = System::ExplicitCast<Paragraph>(srcNode); | |
if (para->get_IsEndOfSection() && !para->get_HasChildNodes()) | |
{ | |
continue; | |
} | |
} | |
SharedPtr<Node> newNode = importer->ImportNode(srcNode, true); | |
destinationParent->InsertAfter(newNode, insertionDestination); | |
insertionDestination = newNode; | |
} | |
} | |
} | |
else | |
{ | |
throw System::ArgumentException(u"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-C.git. | |
auto srcDoc = MakeObject<Document>(MyDir + u"Document source.docx"); | |
auto dstDoc = MakeObject<Document>(MyDir + u"Northwind traders.docx"); | |
// Set the source document to continue straight after the end of the destination document. | |
srcDoc->get_FirstSection()->get_PageSetup()->set_SectionStart(SectionStart::Continuous); | |
// Restart the page numbering on the start of the source document. | |
srcDoc->get_FirstSection()->get_PageSetup()->set_RestartPageNumbering(true); | |
srcDoc->get_FirstSection()->get_PageSetup()->set_PageStartingNumber(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->get_FirstSection()->get_PageSetup()->set_PageWidth(dstDoc->get_LastSection()->get_PageSetup()->get_PageWidth()); | |
srcDoc->get_FirstSection()->get_PageSetup()->set_PageHeight(dstDoc->get_LastSection()->get_PageSetup()->get_PageHeight()); | |
srcDoc->get_FirstSection()->get_PageSetup()->set_Orientation(dstDoc->get_LastSection()->get_PageSetup()->get_Orientation()); | |
// Iterate through all sections in the source document. | |
for (const auto& para : System::IterateOver<Paragraph>(srcDoc->GetChildNodes(NodeType::Paragraph, true))) | |
{ | |
para->get_ParagraphFormat()->set_KeepWithNext(true); | |
} | |
dstDoc->AppendDocument(srcDoc, ImportFormatMode::KeepSourceFormatting); | |
dstDoc->Save(ArtifactsDir + u"JoinAndAppendDocuments.DifferentPageSetup.docx"); |