Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon Jun 02, 2014 5:46 pm

I am taking a look at the Spire.Doc component and ran into a problem merging fields in the header and footer of a document created with word 2013.

It works great for fields in the body, but doesn't seem to process the header or footer.

I have attached the code to this post.

Thanks,
-Logan

Code: Select all
    class Program
    {
        static void Main(string[] args)
        {
                    var bytes = File.ReadAllBytes(path);
                    var fillDict = new Dictionary<string, string>{
                                                                {"Date",DateTime.Now.ToShortDateString()},
                                                                {"Time",DateTime.Now.ToShortTimeString()},
                                                                {"FreeText","This is my cool text"},
                                                                {"test","This is my test text"},
                                                                {"test1","test 1 text"},
                                                                {"test2","TEST 2 TEXT"},
                                                                {"HeaderField", "header txt"},
                                                                {"TestFooter", "Some footer text"}                                                               
                                                };

                    var spire = new Spire();
                    var filledBytes = spire.Merge(fillDict, bytes);

                    File.WriteAllBytes(savePath, filledBytes);
                    System.Diagnostics.Process.Start(savePath);
        }
    }

    public class Spire
    {
        public byte[] Merge(Dictionary<string, string> dict, byte[] wordFile)
        {
            byte[] rVal = null;
            using (MemoryStream generatedDocument = new MemoryStream())
            {
                if (wordFile != null)
                {
                    new MemoryStream(wordFile).CopyTo(generatedDocument);
                }
                Document document = new Document();
                document.LoadFromStream(generatedDocument, FileFormat.Auto);

                string[] filedNames = dict.Keys.ToArray();

                string[] filedValues = dict.Values.ToArray();

                document.MailMerge.Execute(filedNames, filedValues);


                //Save doc file.
                document.SaveToStream(generatedDocument, FileFormat.Docx2013);
                generatedDocument.Seek(0, SeekOrigin.Begin);
                rVal = generatedDocument.ReadAllBytes();
            }
            return rVal;
        }
    }

clmckinley
 
Posts: 1
Joined: Mon Jun 02, 2014 5:12 pm

Tue Jun 03, 2014 6:26 am

Hello,

Thanks for your feedback.
We have tested with Spire.Doc5.1.12, it works fine in the header or footer, I have attached the generated document, the below is our test code, you can check it.
Code: Select all
Document doc = new Document();
doc.LoadFromFile(@"..\..\Template.docx");
var fillDict = new Dictionary<string, string>{
                                                                {"Date",DateTime.Now.ToShortDateString()},
                                                                {"Time",DateTime.Now.ToShortTimeString()},
                                                                {"FreeText","This is my cool text"},
                                                                {"test","This is my test text"},
                                                                {"test1","test 1 text"},
                                                                {"test2","TEST 2 TEXT"},
                                                                {"HeaderField", "header txt"},
                                                                {"TestFooter", "Some footer text"}                                                               
                                                };
string[] filedNames = fillDict.Keys.ToArray();
string[] filedValues = fillDict.Values.ToArray();
doc.MailMerge.Execute(filedNames, filedValues);
doc.SaveToFile(@"..\..\result.docx",FileFormat.Docx);

Thanks,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Fri Jun 06, 2014 9:44 am

Hello,

Has the issue been resolved? Could you please give us some feedback if convenience?

If there are any questions, welcome to get it back to us.

Thanks,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Return to Spire.Doc