Free Support Forum - aspose.com

Bookmarks are inserted in wrong place when chucks are copy/paste

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Hi,

I have a problem with the merging documents; In a template I have some Placeholders to be replaced by a merged documents, when the replacement goes through, the replacement is done exactly in the way it should be, but when I duplicate the same block by copy/paste, then the replacement is incorrect, If I copy/paste this block again but this time I modify manually the spaces between each other, then the replace is done correctly. This problem occurs since the bookmark is inserted in the incorrect place in the copy/paste chunk, but I have not Idea what's the reason of this.

See Sample bellow:


Could you please help me to find out that is wrong?
I attach a small application where the error can be reproduced.

Thanks in advance.
Best regards
Josue Medrano

Best regards

Josue Medrano

Chunk 1 Ok : Every placeholder manually was placed where is desired to appear

rdbUnbef : Template rdbUnbefZusatz_1 : Template

rdbUnbefZusatz_2: Template rdbUnbefZusatz_3: Template

Chunk 2 Nok : The placeholder where copied and pasted from previews chunk

rdbUnbef : TemplaterdbUnbefZusatz_1 : Template

rdbUnbefZusatz_2: TemplaterdbUnbefZusatz_3: Template

Chunk 3 Ok : The placeholders were copied and pasted from first chunk and then delete manually the separation and the separate again.

rdbUnbef : Template rdbUnbefZusatz_1 : Template

rdbUnbefZusatz_2: Template rdbUnbefZusatz_3: Template

Original File blocks:

[%rdbUnbef%] [%rdbUnbefZusatzA%]
[%rdbUnbefZusatzB%] [%rdbUnbefZusatzC%]

[%rdbUnbef%] [%rdbUnbefZusatzA%]
[%rdbUnbefZusatzB%] [%rdbUnbefZusatzC%]

[%rdbUnbef%] [%rdbUnbefZusatzA%]
[%rdbUnbefZusatzB%] [%rdbUnbefZusatzC%]

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks fro your inquiry. I see in your code that you don’t split runs during inserting document at placeholder. Please try using the following code:

public ReplaceAction InsertDocEvaluator(object sender, ReplaceEvaluatorArgs e)

{

//Get text from match node

string runText = ((Run)e.MatchNode).Text;

//Create document builder

DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document);

//clone match node to preserve formation of text.

Run run = (Run)e.MatchNode.Clone(false);

//split text of match node

run.Text = runText.Substring(runText.IndexOf(e.Match.Value) + e.Match.Value.Length);

((Run)e.MatchNode).Text = runText.Substring(0, runText.IndexOf(e.Match.Value));

//insert created run after match node

e.MatchNode.ParentNode.InsertAfter(run, e.MatchNode);

//get name of mergefield

//-string fieldName = e.Match.Value;

string bookMarkName = string.Format("{0}_{1}", e.Match.Groups["name"].Value, RandomAlphaNumerics(8));

//Insert a Bookmark at match place.

builder.MoveTo(run);

builder.StartBookmark(bookMarkName);

builder.EndBookmark(bookMarkName);

InsertDocumentAtBookamrk(bookMarkName, e.MatchNode.Document, m_margeddoc);

return ReplaceAction.Skip;

}

Hope this helps.

Best regards.

Thanks Alexey,

Thanks, that's really does the trick.

Kind regards,

Josue