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;
}
}