Example #1
0
        private static void CreateConfig()
        {
            TemplateSettings settings = new TemplateSettings();
            Template template = new Template();
            template.Name = "www.jobhunt.in:Template(Bright)";
            template.StyleName = "Fancy";
            template.BottomMargin = 48;
            template.TopMargin = 48;
            template.LeftMargin = 48;
            template.RightMargin = 48;
            settings.Templates.Add(template);

            // Serialization
            XmlSerializer s = new XmlSerializer(typeof(TemplateSettings));
            TextWriter w = new StreamWriter(@"templatesettings.sample.xml");
            s.Serialize(w, settings);
            w.Close();
        }
Example #2
0
        public void ApplyTemplate(string srcHtml, Template template, string outputDoc)
        {
            object readOnly = true;
            object saveChanges = false;
            object oSource = srcHtml;
            object oTrue = true;
            object oFalse = false;

            try
            {
                if (createNew)
                {
                    app = new Application();
                    createNew = false;
                }

                // Source document open here
                app.Documents.Open(ref oSource, ref missing,
                     ref readOnly, ref missing, ref missing,
                     ref missing, ref missing, ref missing,
                     ref missing, ref missing, ref missing,
                     ref missing, ref missing, ref missing, ref missing,
                     ref missing);

                app.ActiveDocument.ApplyQuickStyleSet(template.StyleName);

                SetProperty(app.ActiveDocument, true, "Comments", template.Name);

                PageSetup pageSetup = app.ActiveDocument.PageSetup;
                pageSetup.BottomMargin = template.BottomMargin;
                pageSetup.TopMargin = template.TopMargin;
                pageSetup.LeftMargin = template.LeftMargin;
                pageSetup.RightMargin = template.RightMargin;

                object format = WdSaveFormat.wdFormatDocument;
                object oOutputDoc = outputDoc;

                if (template.EmbedFonts)
                    app.ActiveDocument.SaveSubsetFonts = false;

                object oEmbedFonts = template.EmbedFonts ? true : false;
                app.ActiveDocument.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView;
                app.ActiveDocument.SaveAs(ref oOutputDoc, ref format,
                        ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref oEmbedFonts,
                        ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing,
                        ref missing, ref missing);

                app.ActiveDocument.Close(ref oFalse, ref missing, ref missing);
            }
            catch
            {
                if (app != null)
                {
                    app.Quit(ref saveChanges, ref missing, ref missing);
                    Marshal.ReleaseComObject(app);
                    createNew = true;
                }
                throw;
            }
        }