Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Jan 29, 2015 5:38 pm

Hi,

i have the need to put one or more customer's data on a document with the given template:

Code: Select all
{CUSTOMERS_INFORMATION}

   {CUSTOMER_NAME}
   {CUSTOMER_DOCUMENT_ID}

   {CUSTOMER_SPOUSE_INFORMATION}

      {CUSTOMER_SPOUSE_NAME}
      {CUSTOMER_SPOUSE_DOCUMENT_ID}

   {/CUSTOMER_SPOUSE_INFORMATION}

{/CUSTOMERS_INFORMATION}


For this i need to do the following tasks:

1) Search for the first {CUSTOMERS_INFORMATION}(...){/CUSTOMERS_INFORMATION} block in the document;
This was done with the following code:
Code: Select all
string strRegexTagGroup =
            @"\{{(?<tagName>{0})\}}(?<tagContent>.+?)\{{/\k<tagName>\}}";

var regexTagGroup = new Regex(
               string.Format(strRegexTagGroup, "CUSTOMERS_INFORMATION"),
               RegexOptions.Singleline);

var selection = document.FindPattern(regexTagGroup);


2) Copy that block's content (without the {CUSTOMERS_INFORMATION} begin and end parts), in case there's more than one customer;

Question: This can be done with this line?
Code: Select all
var copy = selection.GetAsOneRange().Clone()


3) Replace the tags {CUSTOMER_NAME} and {CUSTOMER_DOCUMENT_ID} within that block with their respective data;

Question: The previous "selection" variable doesn't expose a Replace method. How can i do the replace on that specific text block?

4) Search for the {CUSTOMER_SPOUSE_INFORMATION}(...){/CUSTOMER_SPOUSE_INFORMATION} inside the "selection" block and replace the respective tags;

If the customer doesn't have spouse information, delete that particular block from the document;

Question: Again, the "selection" variable don't have a FindPattern method. How this can be achieved?

5) If there's more than one customer, insert the "copy" at the end of the block (just before {/CUSTOMERS_INFORMATION});
Repeat the replacing process on steps 3 and 4;

Question: How is the "copy" data be appended at the end of the "selection" block?

Any help is welcome.

Thanks in advance.

EDIT: I'm currently using the Free version of Spire.Doc

WinsysFernando
 
Posts: 3
Joined: Tue Jan 27, 2015 4:49 pm

Fri Jan 30, 2015 2:57 am

Hello,

Thanks for your inquiry.

Considering the complexity of the problems you posted, can you provide us a sample document and some sample data? It would be very helpful you can share us your project (sample project is also available).

Thank you.
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Fri Jan 30, 2015 5:52 pm

Thanks for the reply.

Attached is the test project i am working on.
The important part is on the ReplaceGroup method on the MergeController class. TODO markers were used to pinpoint the important spots.

Basically the current issues are:

1) Not being able to used FindPattern from a TextSelection;
2) Apparently not being able to keep formatting when "pasting" from a "copied" text;
3) FindPattern not being able to detect the tags when they are in different paragraphs;

I guess some of my approaches on other parts of said method need a better way to handle this process. How this code can be modified to fit my needs?

Again, thanks in advance.

EDIT: I changed to the full version of Spire.Doc in hope of finding some additional method that would help me.

WinsysFernando
 
Posts: 3
Joined: Tue Jan 27, 2015 4:49 pm

Mon Feb 02, 2015 3:45 am

Hello WinsysFernando,

Thanks for sharing your project.

From your code snippet and sample document, I can see that you you want to replace text inside the tags.

Actually, there is another simple way to meet your requirement: use mail merge to merge data in the template document. You can get more information about "mail merge" via link below:
https://support.office.microsoft.com/en ... n-US&ad=US

And Spire.Doc supports mail merge, there are some tutorials on our website which would help you:
http://www.e-iceblue.com/Tutorials/Spir ... Merge.html
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Mon Feb 02, 2015 3:57 pm

Unfortunately, i am unable to use the available Mail Merge feature.
The motive is because a legacy product of us(written in VB6, over 10 years old) use this exact same procedure and text structure to merge documents(this product uses Word Automation to execute this task), so the template file must be able to be modified in both sides without changes.

That's why those steps must be done manually on the document's text. Any help to accomplish this will be really appreciated.

WinsysFernando
 
Posts: 3
Joined: Tue Jan 27, 2015 4:49 pm

Tue Feb 03, 2015 9:21 am

Hello WinsysFernando,

1) Not being able to used FindPattern from a TextSelection
Sorry that TextSelection doesn't support to use FindPattern.

2)Apparently not being able to keep formatting when "pasting" from a "copied" text
I noticed that you used "Clone()" function to get the textrange, please remove it and just call "GetAsOneRange()".

3)FindPattern not being able to detect the tags when they are in different paragraphs
Try to use "FindAllPattern" function.

Besides, this problem is mainly about your application design, not the function aspects of Spire.Doc. Wish there is a better way to help you accomplish this application.
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Return to Spire.Doc