Free Support Forum - aspose.com

Problem Merging document into another ignores the position of the original placeholders

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

I have a problem merging documents into another.

The problem is that for some reasons during the replacement it ignores the position of the original placeholders.

I attach here a small project and templates where the problem can be reproduced.

See following example:

result:

PlaceHolderOrder1 in RollePlaceHolderOrder4 in BGPlaceHolderOrder3 in RangPlaceHolderOrder2 in SalaerPlaceHolderOrder5 in Vertragsdauer

NOTE: According the order in place holder should be this order (please see template):

[Rolle_BS64 - PlaceHolderOrder1 in Rolle]

[Salaer_BS64 - PlaceHolderOrder2 in Salaer]

[Rang_BS64 - PlaceHolderOrder3 in Rang]

[BG_BS64 - PlaceHolderOrder4 in BG]

[Vertragsdauer_BS64 - PlaceHolderOrder5 in Vertragsdauer ]

This problem is very significant for us; I'll appreciate very much your help in this issue.

Thanks a regards

Josue Medrano

UBS AG
Global Wealth Management & Business Banking
Information Technology - Custom Development
HR & EDUCATION SOLUTIONS
Tel: +41 61 288'2762
Email:
josue.medrano-cruz@ubs.com

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

Thanks for your request. e.MatchNode is Run that can contain other text than MatchValue. Please try using the following code:

public ReplaceAction InsertDocEvaluator(object sender, ReplaceEvaluatorArgs e)

{

//Get MatchNode

Run run1 = (Run)e.MatchNode;

//Create Run

Run run2 = new Run(e.MatchNode.Document, string.Empty);

//Get index of match value

int index = run1.Text.IndexOf(e.Match.Value);

if (index > 0)

{

//Split text in match node

run2.Text = string.IsNullOrEmpty(run1.Text.Substring(0, index)) ? " " : run1.Text.Substring(0, index);

run1.Text = string.IsNullOrEmpty(run1.Text.Substring(index + e.Match.Value.Length)) ? " " : run1.Text.Substring(index + e.Match.Value.Length);

}

else

{

run2.Text = " ";

run1.Text = string.IsNullOrEmpty(run1.Text.Substring(e.Match.Value.Length)) ? " " : run1.Text.Substring(e.Match.Value.Length);

}

run1.ParentParagraph.InsertBefore(run2, run1);

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

//Move to run1

builder.MoveTo(run1);

string mNodeText = "BM" + (new Random()).Next(1000, 100000);

builder.StartBookmark(mNodeText);

builder.EndBookmark(mNodeText);

try

{

InsertDocumentAtBookamrk(mNodeText, builder.Document, m_doc);

}

catch(Exception)

{

}

return (ReplaceAction.Skip);

}

I hope this could help you.

Best regards.

Hi Alexey
Thanks for your quick answer.
I've tried the code you gave me, but no luck. the same problem persists.
I attach the project again, this time exactly as the one I am trying, except I removed the the license .
Regards
Josue

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

Thanks for your request. Please try using the attached code and let me know if this works.

Best regards.

Hi Alexey,

It works!, thanks a lot for your support. I appreciate this very much. I'll do some other test with other escenarios before deploy this solution.

Again, thanks a lot.

Regards, Josue

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

I was doing some other testes, the solution you delivered works really good, however, I found a little bug, It is related to the styles, after the splitter runs it reset the appropriates styles, as you could see, when the placeholder is replaced with a document it does not matter the style, but when this will be replaces with a text after, the splitter has reset the style.

I tried to fix by using the style from run1 in run2, but it did no work.

I send you my little application once again, I hope you can help me out to find a solution.

I appreciate you help,

Kind regards,

Josue

- Before Splitter

[%PlaceHolder_A%]

[%PlaceHolder_B%]

[%PlaceHolder_C%]

- After Splitter

[%PlaceHolder_A%]

[%PlaceHolder_B%]

[%PlaceHolder_C%]

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

Thanks for your request. Just try using The following line of code

Run run2 = (Run)run1.Clone(true);

Instead

Run run2 = new Run(e.MatchNode.Document);

Best regards.

Hi Alexey,

It woks like charm! thanks a lot for your help.

Best regards,

Josue

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

We have found another problem in regarding the snippetsnippt code you delivered me to solve the problem regaring the topic.

In the document there are some place holders which will be replaced with plain text, now these have problem, after the splitter runs the space between words is removed and pleaced at the beginning, so when the replacement takes afect the result is that the word stick together and there is a gap at the bigining.

Sorry to bother you, I thought everything was ok, till i found this.

see sample:

===Original Text===

UBS AG in (nachstehend UBS)

und [%Anrede%] [%Vorname%] [%Name%] (nachstehend Arbeitnehmer)

vereinbaren:

[%VertragDauer2%] [%VertragDauer3%]

===Result after the splitter===

UBS AG in (nachstehend UBS)

und [%Anrede%][%Vorname%][%Name%] (nachstehend Arbeitnehmer)

vereinbaren:

[%VertragDauer2%][%VertragDauer3%]

Notice: that the space which separes each word of the other is removed a passed to the begining of the word.

I attach again the little solution for you to reproduce the error.

Dieses Schreiben ist eine Ergänzung zum bestehenden Arbeitsvertrag vom [Datum] und ändert diesen in folgenden Punkten ab:

PlaceHolderOrder1 in RollePlaceHolderOrder2 in SalaerPlaceHolderOrder3 in RangPlaceHolderOrder4 in BGPlaceHolderOrder5 in Vertragsdauer

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

Thanks for your request. Please try using the following regex:

Regex splitRegex = new Regex(" *?\\[%(?.*?)%\\]");

Hope this helps.

Best regards.

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

Thanks for your quick answer, as you see this solve a problem but leads another,

The placeholders for plain text are ok, the spaces remain, this is just perfect, but this removes all spaces in a row when documents are merged.

OK

und [%Anrede%] [%Vorname%] [%Name%]

vereinbaren:

[%VertragDauer2%] [%VertragDauer3%]

NOK

PlaceHolderOrder2 in SalaerPlaceHolderOrder3 in Rang

In template is so:

[%Test1%] [%Test2%]

[%Test1%] [%Test2%]

So result should have the gap between place holders.

Many thanks for your help.

Regards

Josue

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

Thanks for your request. This could be fixed by inserting bookmark at the end of run. You can do this using the following code (this code is used in SplitRunsReplaceEvaluator):

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

builder.MoveTo(run2);

//Insert bookmarks into the document

builder.StartBookmark(e.Match.Groups["name"].Value);

if (run2.NextSibling != null)

builder.MoveTo(run2.NextSibling);

else

builder.MoveTo(run2.ParentParagraph);

builder.EndBookmark(e.Match.Groups["name"].Value);

Also you should use the following line in the InsertDocumentAtBookamrk method.

//Move cursor to bookmark and insert paragraph break

builder.MoveToBookmark(bookmarkName, false, true);

Hope this helps.

Best regards.

Hi Alexey,

Once again, thanks a lot.

It seems to work ok. I have implemented this in my application, I'll let the team to do some other tests.

Kind regards

Josue

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

Hi Alexey

I have another problem with merging document, the problem is that actually somehow the lines breaks from the merged document are being removed:

in following example the blue text comes from a merged document which has two line breaks at the end, after is follow by another merged document (green)

I attach a small solution which reproduces the error.

I'll appreciate very much your help is this matter.

Sample:

NoOK

This is a test after the "end." a line follows with two line break at end. This paragraph ends with two line breaks at the end. This is a normal merge document not line break at end.

It should be like this:

This is a test after the "end." a line follows with two line break at end. This paragraph ends with two line breaks at the end.

This is a normal merge document not line break at end.

------------------------------------------------

Best Regards

Josue:

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

Thanks for your request. The InsertDocumentAtBookamrk method removes empty paragraphs from the end of document. You can just comment the following code snippets in the InsertDocumentAtBookamrk method.

//Remove empty paragraphs from the end of document

while((!srcDoc.LastSection.Body.LastParagraph.HasChildNodes))

{

srcDoc.LastSection.Body.LastParagraph.Remove();

}

And the following

if(((para != null)) && para.IsEndOfSection && (!para.HasChildNodes))

{

break;

}

Best regards.

Hi Alexey,

I have applied these changes, and It works fine, so far that was the only issue I had.

Thanks a lot for your support.

Best regards.
Josue