We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Docx to pdf- Adding Merge Fields at run time

Hello there,

I am adding Merge Fields in my document at run-time.
All is working fine, except two issues.

For my case, some particular strings in the document are to be replaced by MergeFields at run-time.
So, I am iterating through all the runs in my document and if their text matches with my DataSource property, I am replacing them with the MergeField. Below is my code:

Document objDocument = new Document(dir + “TestRun.docx”);

private void SetMergeFieldsForParagraphs()
string customProperty = “”;
Paragraph objParagraph;
bool insertMergeField = false;
DocumentBuilder docBuilder = new DocumentBuilder(objDocument);
NodeCollection paragraphs = objDocument.GetChildNodes(NodeType.Paragraph, true);

for (int i = 0; i < paragraphs.Count; i++)
objParagraph = (Paragraph)paragraphs[i];

NodeCollection runs = objParagraph.GetChildNodes(NodeType.Run, true);
foreach (Node run in runs)
if (run.ToTxt().ToUpper().Contains(“PPROPERTYTITLE”))
customProperty = “PropertyTitle”;
insertMergeField = true;
else if (run.ToTxt().ToUpper() == “PPROPERTYACCOMMODATION”)
customProperty = “PropertyAccomodation”;
insertMergeField = true;
else if (run.ToTxt().ToUpper().Contains(“PASKINGPRICE”))
customProperty = “Price”;
insertMergeField = true;

if (insertMergeField == true)
docBuilder.InsertField(string.Format(“MERGEFIELD {0}”, customProperty), string.Format("«{0}»", customProperty));
insertMergeField = false;


PropertyMailMergeDataSource mmDataSource = new PropertyMailMergeDataSource(propertyList);
objDocument.SaveToPdf(dir + “TestRun.pdf”);

First Issue:
For the string “Price Guide PAskingPrice” in my source doc, I am getting only one Run.
I want to replace only PAskingPrice with MergeField, So need a way for these to be returned as two seperate Runs.
Is there a way I can achieve this?

Or If there is any other way to achieve above, It will be much appreciated.

Second Issue:
“PPROPERTYACCOMMODATION”, I am not able to find this string in any of the Runs while itearting.
It seems that after calling node.Remove() method, it doesn’t ietrate furthur because the parent node Runs is updated by inserting MergeField.

Both input and output document are attached.
Please guide.

Thank you!


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

Thanks for your request. Why do not you use code I provided in the following thread to replace text placeholders with mergefields?

The code I provided works fine even if few placeholders are in the same run.

Best regards,

Thanks Andrey, I could have happily used the method you have suggested in above thread,

But, the problem is my source documents are not having string to be replaced in a particular format that I can filter them with Regex. That’s why I went for iterating over paragraphs.

Please guide.


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

Thank you for additional information. I used placeholders like [placeholder] only to demonstrate the technique. Using regular expression, you can find any string. In your case you can loop through columns in your datatable, find column name in the document and replace it with the appropriate mergefield. Code will look like the following:

// Open document.

Document doc = new Document(@"Test055\in.doc");

// Define collection of placeholders.


// Find placeholders in the documetn and replace them with mergefields.

foreach (string name in names)


Regex regex = new Regex(name, RegexOptions.IgnoreCase);

// Find and replace placeholder.

doc.Range.Replace(regex, new ReplaceEvaluator(ReplaceEvaluator), false);


// Save output document


The only change you should do in the ReplaceEvaluator is the following:

// Get name of placeholder.

string name = e.Match.Value;

Best regards,

Thank you for your great help!