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.

Wed Oct 22, 2014 7:02 pm

This is a great tool.

I'm trying to find a solution where I can parse the following text and replace with a new one.

old:
Dated this 03 day of September, 2014

new:
Dated this 22 day of October, 2014


How can I achieve this functionality?

Thank you!!!

JCasber
 
Posts: 3
Joined: Wed Oct 22, 2014 6:59 pm

Thu Oct 23, 2014 1:53 am

Dear JCasber,

Thanks for your inquiry.

For your requirement, please refer to the code below:
Code: Select all
Document doc = new Document();
doc.LoadFromFile("Test.docx", FileFormat.Docx2010);
doc.Replace("Dated this 03 day of September, 2014", "Dated this 22 day of October, 2014", true, true);
doc.SaveToFile("Result.docx", FileFormat.Docx2010);

Best Regards,
Burning
E-iceblue Support Team
Best Regards,
Burning
E-iceblue Support Team
User avatar

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

Thu Oct 23, 2014 3:24 am

Thank you. In the example the dates are not same.

Here are couple of samples..

Doc1:
This document is prepared for Name1. Dated this 02 day of August, 2014.


This document is prepared for Name 2. Dated this 12 day of September, 2014


In both above cases, I was using MS Word with Range find, to find from "Dated to 2014" and was replacing with current date and year like this... But using word had some issues then I found your tool. If I can figure out on how to do this that would be great.

In both the cases, i want to get an output like this.
Dated this 22 day of October, 2014.

The day and month wont be same. Is there a way I can achieve this functionality..

JCasber
 
Posts: 3
Joined: Wed Oct 22, 2014 6:59 pm

Thu Oct 23, 2014 6:30 am

Dear JCasber,

Thanks for your reply.

For your requirement, please refer to the code below:
Code: Select all
Document doc = new Document();
doc.LoadFromFile("Test.docx", FileFormat.Docx2010);
string pattern = @"Dated.*";
Regex regex = new Regex(pattern);
TextSelection[] selections = doc.FindAllPattern(regex);
string time = DateTime.Now.ToString("yyyy-MMMM-dd", CultureInfo.CreateSpecificCulture("en-US"));
string[] timeString = time.Split('-');
for (int i = 0; i < selections.Count(); i++)
{
    string temp = string.Format("Dated this {0} day of {1}, {2}", timeString[2], timeString[1], timeString[0]);
    doc.Replace(selections[i].SelectedText, temp, true, true);
}
doc.SaveToFile("Result.docx", FileFormat.Docx2010);

Best Regards,
Burning
E-iceblue Support Team
Best Regards,
Burning
E-iceblue Support Team
User avatar

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

Thu Oct 23, 2014 12:14 pm

Awesome...

thank you very much for your quick and responsive support....

JCasber
 
Posts: 3
Joined: Wed Oct 22, 2014 6:59 pm

Fri Oct 24, 2014 1:01 am

Dear JCasber,

Please feel free to contact us if you have any problem.

Best Regards,
Burning
E-iceblue Support Team
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