C#, Microsoft Windows

Create pdf report from word template in C#

1. Create a word template from an existent word document or a new one.
2. Add fields in the word template, prefer type keyword.
3. When you have created your templates you can use the class below!

Don’t forget to add a reference to the Microsoft Word Object Library from the COM tab.

    /*
     *  Reporter Class using word template
     *  Copyright (C) 2011 Reporter Class KARPOUZAS GEORGE
     *
     *  This program is free software: you can redistribute it and/or modify
     *  it under the terms of the GNU General Public License as published by
     *  the Free Software Foundation, either version 3 of the License, or
     *  (at your option) any later version.
     *
     *  This program is distributed in the hope that it will be useful,
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *  GNU General Public License for more details.
     *
     *  You should have received a copy of the GNU General Public License
     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     */
    class Reporter
    {
        private String _template;
        private object oMissing;
        private Object oFalse;
        private Microsoft.Office.Interop.Word._Application wordApp;
        private Microsoft.Office.Interop.Word._Document wordDoc;
 
        public Reporter()
        {
            this._template = "";
            this.oMissing = Missing.Value;
            this.oFalse = false;
            this.wordApp = null;
            this.wordDoc = null;
        }
 
        public void setTemplate(String template)
        {
                    if (File.Exists(template))
                    {
                        this.wordApp = new Microsoft.Office.Interop.Word.Application();
                        this.wordApp.Visible = false;
                        this._template = template;
                        object oTemplate = this._template;
                        this.wordDoc = wordApp.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
                    }
                    break;
        }
 
        public void print(Dictionary<String, String> keyvalues, String savepath)
        {
            this._create(keyvalues, savepath);
        }
 
        public void close()
        {
            this.wordDoc.Close(ref oFalse, ref oMissing, ref oMissing);
            this.wordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
        }
 
        private void _create(Dictionary<String, String> keyvalues, String saveas)
        {
            if (wordDoc != null && wordApp!=null)
            {
                foreach (Microsoft.Office.Interop.Word.Field field in this.wordDoc.Fields)
                {
                    Microsoft.Office.Interop.Word.Range fieldcode = field.Code;
                    String fieldText = fieldcode.Text;
 
                    if (fieldText.StartsWith(" KEYWORDS"))
                    {
                        Int32 endMerge = fieldText.IndexOf("\");
                        Int32 fieldNameLength = fieldText.Length - endMerge;
                        String fieldName = fieldText.Substring(11, endMerge - 11);
 
                        fieldName = fieldName.Trim();
                        if (keyvalues.Keys.Contains(fieldName))
                        {
                            field.Select();
                            String value = keyvalues[fieldName];
                            this.wordApp.Selection.TypeText(value==""?" ":value);
                        }
                        else
                        {
                            field.Select();
                            this.wordApp.Selection.TypeText(" ");
                        }
                    }
                }
                object fileFormat = WdSaveFormat.wdFormatPDF;
                Object oSaveAsFile = (Object)saveas;
                this.wordDoc.SaveAs(ref oSaveAsFile, ref fileFormat, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing);
            }
        }
    }

ex.

Reporter reporter = new Reporter();
reporter.setTemplate("path_to_template");
Dictionary<String,String> keywords_values = new Dictionary<String,String>();
keywords_values.Add("keyword1","value1");
reporter.print(keywords_values, "filename.pdf");
reporter.close();