Northwind next

Thank I think that the first suggest will resolve my problem by using mail merge and Image:FieldName merge field (for exapmle 12a23.gif).
I can store the image file name to the field in the data source .
But how can we specify mail merge and Image ?

Sorry I didn’t understand this question.

If you are happy to store image file names in the database, then just have a text field in the database and store image file name there.

If you can store full image file name (including path), for example “C:\MyDb\Images\123.gif”, then you don’t need to have any extra code. Just insert a merge field into the document and type “Image:MyImage” as the merge field name. The “Image:” prefix will specify to Aspose.Word that the field contains the image file name or image bytes so Aspose.Word will fetch the value from MyImage field.

If you cannot store full image file name in the database field (for example you can only store “123.gif”), then you need to write a mail merge event handler that will compose the full file name and help Aspose.Word to locate the image. Let me know if you need this and I will post an example here.

Thank you,
I want to only store image name (as a text ) without path for example 123.gif.
I think this is better.
Then I want you to post me example.
Regards

I will send you an archive that contains the code, document and data. I post here for other customers’ reference.

Here is the code:

/// <summary> 
/// This example shows how to mail merge images into a document when 
/// only image file name, for example "MyProduct123.gif" is stored in the database. 
/// </summary>
public void MailMergeImageFileName()
{
    //This example loads Product table from an XML file.
    DataSet ds = new DataSet();
    ds.ReadXml(gBasePath + "MailMergeImageFileName.xml");
    //Open the report template document.
    Document doc = new Document(gBasePath + "MailMergeImageFileName.doc");
    //Attach an event handler that will be called for the Image:ProductImage merge field.
    doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(HandleMergeImageField);
    //Invoke mail merge. Note we are using merge regions because we want the table in the document to grow.
    doc.MailMerge.ExecuteWithRegions(ds.Tables["Product"]);
    doc.Save(gBasePath + "MailMergeImageFileName Out.doc");
}

/// <summary>
/// This is an event handler that gets called for every record for any Image:xxx merge field in the document.
/// </summary>
private void HandleMergeImageField(object sender, MergeImageFieldEventArgs e)
{
    if (e.FieldName == "ProductImage")
    {
        //FieldValue contains just the value from the database - it's the short image file name.
        string fileName = (string)e.FieldValue;
        //Our task is to create a complete image file name with its path and let the mail merge
        //engine continue, it will load the image and insert it into the document.
        e.ImageFileName = gBasePath + fileName;
    }
}
private const string gBasePath = @"C:\Program Files\Aspose\Aspose.Word\Examples\MailMergeImageFileName\";

Here is the data file I use:

<Products>
 <Product>
  <ProductCode>123</ProductCode>
  <ProductImage>Product123.jpg</ProductImage>
 </Product>
 <Product>
  <ProductCode>1011</ProductCode>
  <ProductImage>Product1011.jpg</ProductImage>
 </Product>
</Products>

Here is how the template report looks like:

Product Code
Photo <<TableStart:Product>> <<ProductCode>>  <<Image:ProductImage>> <<TableEnd:Product>>

Sorry cannot send because don’t have your email, but I hope it should be all clear from the above post.

Thanks you it works

Hello,
Is it possible after generating my document, to join contents of another word documents.
I want to joind annexes in my document.
For example, after mail merging my build documents , that contains 42 lines.
I add the content of other precreated documents doc1.doc (11 lines ) and doc2.doc (24 lines ).

So at the end,build.doc will contains 42+11+24 lines.

Is possible to do this pragrammaticaly ?

Thanks,

You can use this function to add content of one document to another. If you have only one section in the document, you can simplify the function by removing the loop.

/// 
/// Moves all sections from one document to another.
/// 
private void AppendDoc(Document dstDoc, Document srcDoc)
{
    while (srcDoc.Sections.Count > 0)
    {
        Section section = srcDoc.Sections[0];
        // The section must first be removed before it can be inserted into a document.
        srcDoc.Sections.RemoveAt(0);
        dstDoc.Sections.Add(section);
    }
}