Advice needed on .net and Aspose Integration

I want to read a word doc and find out words like this , and replace that it wd some other text and save the word doc,. Is there any mechanism to do this?

Hello
Thanks for your inquiry. You can use the Range.Replace method to achieve this. In your case you would want to use the method overload that takes simple strings and replace them, for example:

Document doc = new Document("C:\\Temp\\in.docx");
doc.getRange().replace("", "Andrey", false, false);
doc.save("C:\\Temp\\out.docx");

Best regards,

Also could you please attach your input document with logos and output PDF here for testing? I will investigate the issue and provide you more information.
Best regards,

I am getting thhis error:
Error 1 ‘Aspose.Words.Document’ does not contain a definition for ‘getRange’ and no extension method ‘getRange’ accepting a first argument of type ‘Aspose.Words.Document’ could be found (are you missing a using directive or an assembly reference?)

Sorry, I posted the code for Java. Please try using this code in NET.

Document doc = new Document("C:\\Temp\\in.docx");
doc.Range.Replace("", "Andrey", false, false);
doc.Save("C:\\Temp\\out.docx");

Best regards,

yes, that worked for me…
Here, I have to replace the text of a document like ‘Doc A’ with the letteres of other document like ‘Doc B’
Lets Say, In Doc B i have fields like,…
And in Doc A, I will get like this
Nikhil (like some xml format(value key format))
All I have to do dynamically need to replace the in Doc B with Nikhil in Doc A.
Can u pls help me?

Hello
Thanks for your inquiry. I think more easier and elegant way to achieve what you need is using MailMegre feature. Please see the following simple code and attached documents:

Document doc = new Document("C:\\Temp\\B.docx");
DataSet ds = new DataSet();
ds.ReadXml("C:\\Temp\\A.xml");
doc.MailMerge.Execute(ds.Tables[0]);
doc.Save("C:\\Temp\\out.docx");

Best regards,

Hi ,
I am not sure, in this document they have applied mail merge feature or not.
My document is like this:

Our ref:
Your ref:
Dear
Re:
We write to confirm the following information:
blah blah blah blah
** If you see thsi doc, you can see that they just used the <> to mark the words not the mail merge feature, but the doc u have sent is done using the mail merge thats why u are able to replace it … in this kind of situation… what u will suggest?

Hello
Thanks for your request. In your case, you can use code like the following to replace text placeholders with merge fields at run-time:

Document doc = new Document("C:\\Temp\\B.docx");
Regex regex = new Regex(@"\<(?\S+)\>", RegexOptions.IgnoreCase);
// Find and replace placeholders
doc.Range.Replace(regex, new ReplacePlaceholders(), false);
DataSet ds = new DataSet();
ds.ReadXml("C:\\Temp\\A.xml");
doc.MailMerge.Execute(ds.Tables[0]);
doc.Save("C:\\Temp\\out.docx");
private class ReplacePlaceholders: IReplacingCallback
{
    public ReplaceAction Replacing(ReplacingArgs args)
    {
        // Get name of placeholder.
        string name = args.Match.Groups["name"].Value;
        // If name is empty string, then do nothing.
        if (string.IsNullOrEmpty(name))
            return ReplaceAction.Skip;
        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = args.MatchNode;
        // The first (and may be the only) run can contain text before the match,
        // in this case it is necessary to split the run.
        if (args.MatchOffset> 0)
            currentNode = SplitRun((Run) currentNode, args.MatchOffset);
        // Create DocumentBuilder object, which will help us to insert filds.
        DocumentBuilder builder = new DocumentBuilder((Document) args.MatchNode.Document);
        // Move builder cursor to the current node.
        builder.MoveTo(currentNode);
        // Insert mergefield.
        builder.InsertField(string.Format("MERGEFIELD {0}", name), string.Format("½{0}╗", name));
        // This array is used to store all nodes of the match for further removing.
        ArrayList runs = new ArrayList();
        // Find all runs that contain parts of the match string.
        int remainingLength = args.Match.Value.Length;
        while ((remainingLength> 0) &&
            (currentNode != null) &&
            (currentNode.GetText().Length <= remainingLength))
        {
            runs.Add(currentNode);
            remainingLength = remainingLength - currentNode.GetText().Length;
            // Select the next Run node.
            // Have to loop because there could be other nodes such as BookmarkStart etc.
            do {
                currentNode = currentNode.NextSibling;
            }
            while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));
        }
        // Split the last run that contains the match if there is any text left.
        if ((currentNode != null) && (remainingLength> 0))
        {
            SplitRun((Run) currentNode, remainingLength);
            runs.Add(currentNode);
        }
        // Now highlight all runs in the sequence.
        foreach(Run run in runs)
        run.Remove();
        // Signal to the replace engine to do nothing because we have already done all what we wanted.
        return ReplaceAction.Skip;
    }
}
///
/// Splits text of the specified run into two runs.
/// Inserts the new run just after the specified run.
///
private static Run SplitRun(Run run, int position)
{
    Run afterRun = (Run) run.Clone(true);
    afterRun.Text = run.Text.Substring(position);
    run.Text = run.Text.Substring(0, position);
    run.ParentNode.InsertAfter(afterRun, run);
    return afterRun;
}

I use placeholders like the following
Also, I attached my new input and output documents.
Hope this helps.
Best regards,

When i debugged the code you have provided , the control going to the class file you have sent only if i am keping your B.docx as the dstiantion file.If i am putting any other docx file other than B.docx(the one you have just sent), the control is not going to the replaceholder class file. Why this is happening?

Hello
Thank you for additional information. I think in this case you can try comparing two documents (my document and your document) and find out why the problem occurs. Then you can change my code according to your template.
Best regards,

Have u written any document specific code, in the above post? i am not able to read other document other than the doc B you ahve provided.
This is my Word Template look like:
Private and Confidential
Our ref:
Your ref:
Dear
Re:
has been employed by a service company owned by fgsfgfggf dgsgg as follows:
[Following paragraph repeated for each employment instance].
From to in the capacity of within the department of our office.
Since in the capacity of within the department of our office.
In accordance with our usual policy, this information is provided in the strictest confidence and without any financial or other liability, nor must this reference be disclosed either to the subject or a third party.
Should you have any queries in regard to this letter, please contact fgfgfg fgfgfgfg our HC Direct team will assist you
Yours sincerely
fgfdgdfgdfgg
I want to do replace the in this template. pls help me, i could have used the code you have given, but for my surprise its taking your document only B.docx.

Yes, I find out the difference, in your document and the reg expression you have wrote will not consider the word contain spaces in between them like < First Name> , it will always look for something like . So what would be the reg expression for handling my scenario? mine is like < First name> < Address 1> like this…
Pls Advice…

Hi
Thanks for your request. As I can see your template contains placeholder which names contain whitespaces. But the code suggested previously used a regular expression that expects names without whitespaces. So, to resolve the problem you should just change the regular expression.
Best regards,

Oh… Yes,… Can u pls help me with that Reg Expression which can accpet whitespaces like my pattern… i am searching… but getting no answer …

Also, here you can find a simple guide that explains how to construct regular expression:
http://www.zytrax.com/tech/web/regex.htm
Best regards,

Can u pls help me to create a RegEx pattern for the same? < First Name> < My Details Here>.
It would be a great Help.

I guess, Do I need to create the regex dynamically? to replace my given pattern all over in the document or template? please advice?

Hi,
Any Thoughts?

Hi,
Could you please able to help me with a regex string for find out that.
I am clueless.