Hi Alfredo,
Thanks for your inquiry. In your case, I suggest you please use the last parameter value as false in Range.Replace method. This will fix the issue which you are facing.
Document doc = new Document(MyDir + "DOSSIER.docx");
doc.Range.Replace("##EXHIBIT1##", "", false, false);
doc.Range.Replace("##EXHIBIT2##", "", false, false);
doc.Range.Replace("##EXHIBIT3##", "", false, false);
doc.Save(MyDir + "Out.docx");
doc.Save(MyDir + "Out.pdf");
If you can modify your template document, I suggest you please use mail merge feature to insert image. In that case, you need to implement IFieldMergingCallback interface.
Please specify a field name prefix like **Image:**MyFieldName in the document to be able to directly merge images during Mail Merge.
There are three properties available ImageFileName, ImageStream and Image to specify where the image must be taken from. Set only one of these properties. Please see the following highlighted code snippet.
Document doc = new Document(MyDir + "in.docx");
// Your code…
// Your code...
doc.MailMerge.FieldMergingCallback = new HandleMergeImageField();
doc.MailMerge.Execute(datatable); //Pass the datatable in Execute method
doc.Save(MyDir + "Out.docx");
private class HandleMergeImageField : IFieldMergingCallback
{
void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
{
// Do nothing.
}
///
/// This is called when mail merge engine encounters Image:XXX merge field in the document.
/// You have a chance to return an Image object, file name or a stream that contains the image.
///
void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
{
MemoryStream stream = (MemoryStream)e.FieldValue;
stream.Position = 0;
// Now the mail merge engine will retrieve the image from the stream.
e.ImageStream = stream;
}
}
If you want to pass image file name(path of image), please use the ImageFieldMergingArgs.ImageFileName property. This property sets the file name of the image that the mail merge engine must insert into the document.Please check the following code example.
Hope this helps you. Please let us know if you have any more queries.
Document doc = new Document(MyDir + "in.docx");
doc.MailMerge.FieldMergingCallback = new MailMergeInsertImage();
doc.MailMerge.Execute(
new string[] { "MyFieldName" },
new object[] { MyDir + "image.jpg" });
doc.Save(MyDir + "Out.docx");
public class MailMergeInsertImage : IFieldMergingCallback
{
void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
{
}
void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
{
e.ImageFileName = e.FieldValue.ToString();
}
}