Problem: Replace PlainText with a document template (document merge)

Hi there,
I have a problem to replace a placeholder with a merged document, the problem is that the merged document replaces the complete paragraph where he is in place and not only the place holder (you can verify this in the attached doc …08_01_B_Kuendigungsbestaetigung_d.END_RESULT.doc).
This is an urgent case for us, so I may ask you to prioritize this issue.
Here I add the code example I use and attached you’ll find the templates.
thanks and regards
Josue Medrano
UBS AG
Global Wealth Management & Business Banking
-------------------------------------------------------------------------------------------------------

// Open the template document
Document doc = new Document(System.IO.Path.Combine(DocPath, "08_01_B_Kuendigungsbestaetigung_d.doc"));
/* Merge a document by replacing a flat text */
Document margeddoc = new Document(System.IO.Path.Combine(DocPath, "BS12_Pauschalspesen_d.doc"));
ReplaceTextWithDoc(doc, margeddoc, "[%PauschalSpesenAustritt%]");
private void ReplaceTextWithDoc(Document mainDoc, Document insertedDoc, string text)
{
    mainDoc.Range.Replace(new Regex(Regex.Escape(text)), new ReplaceEvaluator(new InsertDoc(insertedDoc).InsertDocEvaluator), false);
}
public class InsertDoc
{
    private readonly Document m_doc;
    public ReplaceAction InsertDocEvaluator(object sender, ReplaceEvaluatorArgs e)
    {
        Paragraph para = (Paragraph)e.MatchNode.ParentNode;
        // para.ParagraphFormat.Style.Font.Color = Color.YellowGreen;
        // Insert document after the paragraph, containing match text.
        InsertDocument(para, m_doc);
        // Remove the paragraph with the match text.
        para.Remove();
        return (ReplaceAction.Skip);
    }
    public void InsertDocument(Node insertAfterNode, Document srcDoc)
    {
        // We need to make sure that the specified node is either pargraph or table.
        if (!((insertAfterNode.NodeType == NodeType.Paragraph) ||
        (insertAfterNode.NodeType == NodeType.Table)))
            throw new ArgumentException("The destination node should be either paragraph or table.");
        // We will be inserting into the parent of the destination paragraph.
        CompositeNode dstStory = insertAfterNode.ParentNode;
        // This object will be translating styles and lists during the import.
        NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document, ImportFormatMode.KeepSourceFormatting);
        // Loop through all sections in the source document.
        foreach (Section srcSection in srcDoc.Sections)
        {
            // Loop through all block level nodes (paragraphs and tables) in the body of the section.
            foreach (Node srcNode in srcSection.Body)
            {
                // Do not insert node if it is a last empty paragarph in the section.
                Paragraph para = srcNode as Paragraph;
                if ((para != null) && para.IsEndOfSection && !para.HasChildNodes)
                    break;
                // This creates a clone of the node, suitable for insertion into the destination document.
                Node newNode = importer.ImportNode(srcNode, true);
                // Insert new node after the reference node.
                dstStory.InsertAfter(newNode, insertAfterNode);
                insertAfterNode = newNode;
            }
        }
    }
    public InsertDoc(Document doc)
    {
        m_doc = doc;
    }
}

Hi
Thanks for your request. In your code you remove paragraph that contains match text. I think that you should split this paragraph to two paragraphs.
Please see the following code.

public ReplaceAction InsertDocEvaluator(object sender, ReplaceEvaluatorArgs e)
{
    DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document);
    builder.MoveTo(e.MatchNode);
    // Insert Paragraph break
    builder.InsertBreak(BreakType.ParagraphBreak);
    // Remove previouse paragraph if it is empty
    Paragraph para = (Paragraph)builder.CurrentParagraph.PreviousSibling;
    // para.ParagraphFormat.Style.Font.Color = Color.YellowGreen;
    // Insert document after the paragraph, containing match text.
    InsertDocument(para, m_doc);
    // Remove the paragraph with the match text.
    Paragraph parentParagraph = (Paragraph)e.MatchNode.ParentNode;
    e.MatchNode.Remove();
    if (!parentParagraph.HasChildNodes)
        parentParagraph.Remove();
    if (!para.HasChildNodes)
        para.Remove();
    return (ReplaceAction.Skip);
}

I hope this could help you.
Best regards.

Hi Alexey,
Thanks for your quick answer, the solution you’ve sent me it is close to the desired, but the only problem is that creates a line break for every inserted Text.
See example below:
Als Beilage erhalten Sie die uns freundlicherweise zur Verfügung gestellten Unterlagen zurück.
Für Ihre weitere berufliche Laufbahn wünschen wir Ihnen viel Erfolg [%PauschalSpesen%], Sie an einer Stelle bei unserer
This is another Text [%FreistellungMitArbeitsverbot%], [%RechtsUnterschrift%] Freundliche Grüsse Freundliche Grüsse Freundliche Grüsse Freundliche Grüsse.
Result:
Als Beilage erhalten Sie die uns freundlicherweise zur Verfügung gestellten Unterlagen zurück.
Für Ihre weitere berufliche Laufbahn wünschen wir Ihnen viel Erfolg
Ihre Pauschalspesen sind von Sozialversicherungen
, Sie an einer Stelle bei unserer
This is another Text
unter Nichteinhaltung der vertraglichen Kündigungsfrist
, die Nichteinhaltung der vertraglichen Kündigungsfrist zu akzeptieren.
Freundliche Grüsse Freundliche Grüsse Freundliche Grüsse Freundliche Grüsse.
I preciate your help.
Kind regards
Josue Medrano

Hi
Thanks for your request. I think that you can find solution in the following thread.
https://forum.aspose.com/t/114203
Best regards.

Hi Alexey,
Thanks for your answer, could you please provideme this snippet as C# code VB is not very usefull for me.
Besides how can apply this example for me, I don’t used bookmark as you saw my example.
I was trying to use book mark, but i realized that if I have the bookmak in between the paragrapth then i doen’t work. Sample:
InsertDocument(builder.Document.Range.Bookmarks[“MyBookmark”].BookmarkStart.ParentNode, m_doc);
Konkret könnten [“MyBookmark”] beispielsweise die unerlaubte Kundgabe, die Verwertung
If the paragraph is used alone for a break line it works.
I appreciate your help and the effort you are given in this issue
Regards
josue

Hi
Thanks for your request. I translate code in C# and modify your code. Please try using the attached code.
Best regards.