הכנס וצרף מסמכים
לפעמים זה נדרש לשלב כמה מסמכים לתוך אחד. אתה יכול לעשות זאת באופן ידני או שאתה יכול להשתמש בתכונה 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"); |