-1

I use a Office Word file (template) and in this file there is repetitive default text and photo that I have to replace it by another photo and text

How can I define specific zone in the template and then find those zones in C# to replace them ?

3 Answers3

0

I think the best way is to find out how to manipulate the word xml structure to include the data you want.

For template filling and altering you can use the XML SDK from Microsoft

You can also follow this manual approach here without using the SDK. Manual approach. You will add a custom XML Ressource that includes your changes/ressources for the template.

If you don`t need to be that flexible you can use the standard content control / picture content control in Word and replace them afterwards in C# - it depends how flexible you want to be in replacing elements.. You can find a good and complete example of using picture content control here: Picture content control handling

Community
  • 1
  • 1
Marc Wittmann
  • 2,286
  • 2
  • 28
  • 41
  • Hi Marc, thanks for your help. I just discover that we can add special control in a Word file and specify for each of them a specific name. Do you think if it's a good idéea to dot that and after search theses controle by their names in C# app ? – Dervis Findik Aug 13 '15 at 10:42
  • Do you mean the content control/picture content control? Depends how flexible you want to be in replacing elements.. if this control enable you to do what you want I would try that solution first! You can find a good and complete example of using picture content control here: http://www.codeproject.com/Articles/29938/Adding-An-Image-to-a-Word-Document-Programmaticall I added this option to my answer for future reference – Marc Wittmann Aug 13 '15 at 14:46
  • Yes, the content control (rich text, picture, ...). I think it's a good way the specify zone in the Word document and after find it and add a value. But what do you mean by flexible ? – Dervis Findik Aug 13 '15 at 22:30
  • with content control you can only alter the control you set and replace the object in that control.. with above solution you can manipulate the whole xml structure which is very powerful... just thought mentioning it because you wanted "another approach" (according to your question) – Marc Wittmann Aug 14 '15 at 14:49
0

Ok, finally I try this approch ; use a Word file with Content Control and use a XML file to bind data to them

For that I use the following code :

string outFile = @"D:\template_created.docx";
string docPath = @"D:\template.docx";
string xmlPath = @"D:\template.xml";

File.Copy(docPath, outFile);

using (WordprocessingDocument doc = WordprocessingDocument.Open(outFile, true))
{
    MainDocumentPart mdp = doc.MainDocumentPart;
    if (mdp.CustomXmlParts != null)
    {
        mdp.DeleteParts<CustomXmlPart>(mdp.CustomXmlParts);
    }
    CustomXmlPart cxp = mdp.AddCustomXmlPart(CustomXmlPartType.CustomXml);

    FileStream fs = null;
    try
    {
        fs = new FileStream(xmlPath, FileMode.Open);

        cxp.FeedData(fs);
        mdp.Document.Save();
    }
    finally
    {
        if (fs != null)
        {
            fs.Dispose();
        }
    }
}

When I run the app, it created the custom XML file and append it to my Word file. When I open the Word file, there is no error, but all the Content Control are not filled

0

My final approach was to use Content Control in my Word document with a unique id. Then I can find those id's with C# and replace the content.