Ejemplo n.º 1
0
        private HeaderFooter ChooseHeaderFooter(HeadersFooters hfs, PagePosition pagePos)
        {
            if (hfs == null)
            {
                return(null);
            }

            PageSetup pageSetup = this.currentSection.PageSetup;

            if (pagePos == PagePosition.First)
            {
                if (pageSetup.DifferentFirstPageHeaderFooter)
                {
                    return((HeaderFooter)hfs.GetValue("FirstPage", GV.ReadOnly));
                }
            }
            if (pagePos == PagePosition.Even || this.currentPage % 2 == 0)
            {
                if (pageSetup.OddAndEvenPagesHeaderFooter)
                {
                    return((HeaderFooter)hfs.GetValue("EvenPage", GV.ReadOnly));
                }
            }
            return((HeaderFooter)hfs.GetValue("Primary", GV.ReadOnly));
        }
Ejemplo n.º 2
0
        void FormatHeadersFooters()
        {
            HeadersFooters headers = (HeadersFooters)this.currentSection.GetValue("Headers", GV.ReadOnly);

            if (headers != null)
            {
                PagePosition         pagePos = CurrentPagePosition;
                HeaderFooterPosition hfp     = new HeaderFooterPosition(this.sectionNumber, pagePos);
                if (!this.formattedHeaders.ContainsKey(hfp))
                {
                    FormatHeader(hfp, ChooseHeaderFooter(headers, pagePos));
                }
            }

            HeadersFooters footers = (HeadersFooters)this.currentSection.GetValue("Footers", GV.ReadOnly);

            if (footers != null)
            {
                PagePosition         pagePos = CurrentPagePosition;
                HeaderFooterPosition hfp     = new HeaderFooterPosition(this.sectionNumber, pagePos);
                if (!this.formattedFooters.ContainsKey(hfp))
                {
                    FormatFooter(hfp, ChooseHeaderFooter(footers, pagePos));
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Given a range it will merge any properties of the object which match the names of the fields in the document
        /// </summary>
        /// <param name="currentRange">The range in the document you want the method to act on</param>
        /// <param name="client">The object you want to take the data from</param>
        /// <param name="properties">The properties of the object, if null will ascertain from the object</param>
        public static void UpdateFieldsFromRange(Range currentRange, object client, List <PropertyInfo> properties = null)
        {
            if (properties == null)
            {
                properties = client.GetType().GetProperties().ToList();
            }
            Fields fields = currentRange.Fields;

            foreach (Section section in currentRange.Sections)
            {
                HeadersFooters headers = section.Headers;
                foreach (HeaderFooter sectionHeader in headers)
                {
                    foreach (Field field in sectionHeader.Range.Fields)
                    {
                        UpdateMergeField(client, field, properties);
                    }
                }
                HeadersFooters footers = section.Footers;
                foreach (HeaderFooter sectionFooter in footers)
                {
                    foreach (Field field in sectionFooter.Range.Fields)
                    {
                        UpdateMergeField(client, field, properties);
                    }
                }
            }
            foreach (Field field in fields)
            {
                UpdateMergeField(client, field, properties);
            }
        }
 internal WordHeadersFootersCollection(HeadersFooters headersFooters)
 {
     foreach (HeaderFooter headerFooter in headersFooters)
     {
         this._headersFooters.Add(new WordHeaderFooter(headerFooter));
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Renders the specified headers footers.
        /// </summary>
        /// <param name="headersFooters">The headers footers.</param>
        public void Render(HeadersFooters headersFooters)
        {
            TextFrame frame = headersFooters.Primary.AddTextFrame();

            frame.RelativeHorizontal = RelativeHorizontal.Page;
            frame.RelativeVertical   = RelativeVertical.Page;
            frame.Left   = ShapePosition.Left;
            frame.Width  = Unit.FromCentimeter(21);
            frame.Height = Unit.FromCentimeter(29.8);
            frame.AddImage(_url);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Получить список нижних колонтитулов
        /// </summary>
        public static IEnumerable <HeaderFooter> ToEnumerable(this HeadersFooters headersFooters)
        {
            if (headersFooters == null)
            {
                yield break;
            }

            foreach (HeaderFooter headerFooters in headersFooters)
            {
                yield return(headerFooters);
            }
        }
Ejemplo n.º 7
0
        public static HeadersFooters GetDefaultHeaderFooter(FileInfo file)
        {
            Document document = new Document();

            document.LoadFromFile(file.FullName);

            Section section = document.Sections[0];

            HeadersFooters para_header_default = section.HeadersFooters;

            return(para_header_default);
        }
Ejemplo n.º 8
0
 // update fields in a header or footer
 private static void UpdateHeaderFooterFields(HeadersFooters list, Microsoft.Office.Interop.Word.Application word, String filename)
 {
     foreach (HeaderFooter item in list)
     {
         if (item.Exists && !item.LinkToPrevious)
         {
             var range = item.Range;
             UpdateFieldsInRange(range, word, filename);
             ReleaseCOMObject(range);
         }
         ReleaseCOMObject(item);
     }
 }
Ejemplo n.º 9
0
        /// <summary>
        /// A helper method that finds a specified string in the document (header, body and footer) and replaces it
        /// </summary>
        /// <param name="oldString">The exisitng text</param>
        /// <param name="newString">The replacement text</param>
        private void FindAndReplace(object oldString, object newString)
        {
            //options
            object matchCase         = false;
            object matchWholeWord    = true;
            object matchWildCards    = false;
            object matchSoundsLike   = false;
            object matchAllWordForms = false;
            object forward           = true;
            object format            = false;
            object matchKashida      = false;
            object matchDiacritics   = false;
            object matchAlefHamza    = false;
            object matchControl      = false;
            object read_only         = false;
            object visible           = true;
            object replace           = 2;
            object wrap = 1;

            //execute find and replace

            foreach (Section section in _wordDoc.Sections)
            {
                wordApp.Selection.Find.Execute(ref oldString, ref matchCase, ref matchWholeWord,
                                               ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref newString, ref replace,
                                               ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);

                HeadersFooters headers = section.Headers;

                foreach (HeaderFooter header in headers)
                {
                    Range headerRange = header.Range;
                    headerRange.Find.Execute(ref oldString, ref matchCase, ref matchWholeWord,
                                             ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref newString, ref replace,
                                             ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
                }

                HeadersFooters footers = section.Footers;

                foreach (HeaderFooter footer in footers)
                {
                    Range footerRange = footer.Range;
                    footerRange.Find.Execute(ref oldString, ref matchCase, ref matchWholeWord,
                                             ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref newString, ref replace,
                                             ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
                }
            }
        }
Ejemplo n.º 10
0
        private void SetHeadersFooters(HeadersFooters headersFooters, HeaderFooterReferenceType referenceType)
        {
            switch (referenceType.Type.Value)
            {
            case HeaderFooterValues.Default:
                CopyChildElements(headersFooters.Primary.Elements, referenceType.Id.Value);
                break;

            case HeaderFooterValues.First:
                CopyChildElements(headersFooters.FirstPage.Elements, referenceType.Id.Value);
                break;

            case HeaderFooterValues.Even:
                CopyChildElements(headersFooters.EvenPage.Elements, referenceType.Id.Value);
                break;
            }
        }
Ejemplo n.º 11
0
        public static void AddHeaderFooter(FileInfo file)
        {
            if (String.IsNullOrEmpty(conf.filepath_default_header_footer))
            {
                throw new Exception("Cant not find default headerfooter file");
            }
            //Console.WriteLine("Default file Header_Footer : " + conf.filepath_default_header_footer);

            Document doc = new Document();

            doc.LoadFromFile(file.FullName);

            Section section = doc.Sections[0];

            //remove header, footer from all page
            section.HeadersFooters.Header.ChildObjects.Clear();
            section.HeadersFooters.Footer.ChildObjects.Clear();


            HeadersFooters default_headerfooter = GetDefaultHeaderFooter(new FileInfo(conf.filepath_default_header_footer));

            //change header
            HeaderFooter current_header = section.HeadersFooters.Header;

            foreach (DocumentObject obj in default_headerfooter.Header.ChildObjects)
            {
                current_header.ChildObjects.Add(obj.Clone());
            }

            //change footer
            HeaderFooter current_footer = section.HeadersFooters.Footer;

            foreach (DocumentObject obj in default_headerfooter.Footer.ChildObjects)
            {
                current_footer.ChildObjects.Add(obj.Clone());
            }

            // change LeftIndent
            current_header.Paragraphs[0].Format.LeftIndent = -72;
            current_footer.Paragraphs[0].Format.LeftIndent = -71;

            section.PageSetup.HeaderDistance = 0;
            section.PageSetup.FooterDistance = 0;

            doc.SaveToFile(file.FullName, FileFormat.Docx);
        }
    static void Main(string[] args)
    {
        List <string> path = new List <string>(args);

        string filePathstr = string.Join(" ", path.ToArray());
        //System.Windows.Forms.MessageBox.Show("filepathstr: " + filePathstr);
        string folderPathstr = Path.GetDirectoryName(filePathstr);

        //System.Windows.Forms.MessageBox.Show("folderPathstr: " + folderPathstr);
        try
        {
            Application ap       = new Application();
            Document    document = ap.Documents.Open(filePathstr);
            document.Fields.Update();
            foreach (Section section in document.Sections)
            {
                document.Fields.Update();                 // update each section
                HeadersFooters headers = section.Headers; //Get all headers
                foreach (HeaderFooter header in headers)
                {
                    Fields fields = header.Range.Fields;
                    foreach (Field field in fields)
                    {
                        field.Update();      // update all fields in headers
                    }
                }
                HeadersFooters footers = section.Footers;      //Get all footers
                foreach (HeaderFooter footer in footers)
                {
                    Fields fields = footer.Range.Fields;
                    foreach (Field field in fields)
                    {
                        field.Update();      //update all fields in footers
                    }
                }
            }
            document.Save();
            document.Close();
        }
        catch (NullReferenceException)
        {
            System.Windows.Forms.MessageBox.Show("A valid file was not selected.");
        }
    }
Ejemplo n.º 13
0
        private void removeAndUnlinkHeadersAndFootersFromFinalSection(Document module)
        {
            module.Content.Characters.Last.Delete();
            HeadersFooters headers = module.Content.Sections.Last.Headers;
            HeadersFooters footers = module.Content.Sections.Last.Footers;

            headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious   = false;
            headers[WdHeaderFooterIndex.wdHeaderFooterEvenPages].LinkToPrevious = false;
            headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage].LinkToPrevious = false;
            footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious   = false;
            footers[WdHeaderFooterIndex.wdHeaderFooterEvenPages].LinkToPrevious = false;
            footers[WdHeaderFooterIndex.wdHeaderFooterFirstPage].LinkToPrevious = false;
            module.Content.Sections.Last.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Delete();
            module.Content.Sections.Last.Headers[WdHeaderFooterIndex.wdHeaderFooterEvenPages].Range.Delete();
            module.Content.Sections.Last.Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.Delete();
            module.Content.Sections.Last.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Delete();
            module.Content.Sections.Last.Footers[WdHeaderFooterIndex.wdHeaderFooterEvenPages].Range.Delete();
            module.Content.Sections.Last.Footers[WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.Delete();
            module.PrintPreview();
        }
Ejemplo n.º 14
0
 internal virtual void VisitHeadersFooters(HeadersFooters headersFooters) { }
Ejemplo n.º 15
0
        internal override void Dispose(bool disposing)
        {
            if (disposing)
            {
                // Free managed
                if (_headers != null)
                {
                    _headers.Dispose();
                    _headers = null;
                }

                if (_footers != null)
                {
                    _footers.Dispose();
                    _footers = null;
                }
            }

            base.Dispose(true);
        }
Ejemplo n.º 16
0
 internal override void VisitHeadersFooters(HeadersFooters headersFooters)
 {
 }
Ejemplo n.º 17
0
 public virtual void VisitHeadersFooters(HeadersFooters headersFooters)
 {
 }
Ejemplo n.º 18
0
 internal override void VisitHeadersFooters(HeadersFooters headersFooters)
 {
 }
Ejemplo n.º 19
0
 public override void VisitHeadersFooters(HeadersFooters headersFooters)
 {
 }
Ejemplo n.º 20
0
 internal virtual void VisitHeadersFooters(HeadersFooters headersFooters)
 {
 }
Ejemplo n.º 21
0
        private void UpdateWordProperties_Click(object sender, EventArgs e)
        {
            Word.Application oWord;
            Word._Document   oDoc;
            object           oMissing = Missing.Value;
            object           oDocCustomProps;
            //object fileName=filename;
            object fileName;
            string strIndex, strValue;
            string filename;

            // Storing list of all docx files in targetfilenames array WITH extension
            targetfilenames = System.IO.Directory.GetFiles(wordfolder + "\\", "*.docx").Select(path => System.IO.Path.GetFileName(path)).ToArray();

            // Looping through every docx document in wordfolder
            for (int i = 0; i < targetfilenames.Length; i++)
            {
                filename = wordfolder + "\\" + targetfilenames[i]; //getting filename
                fileName = filename;                               // Passing full filename into the object fileName

                //Create an instance of Microsoft Word and make it visible.
                oWord         = new Word.Application();
                oWord.Visible = true;

                //Open the word document and get the CustomDocumentProperties collection.
                oDoc            = oWord.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                oDocCustomProps = oDoc.CustomDocumentProperties;
                Type typeDocCustomProps = oDocCustomProps.GetType();

                //Get the MT_TSP_ProjectCode property
                strIndex = "MT_TSP_ProjectCode";

                object oDocMT_TSP_ProjectCodeProp = typeDocCustomProps.InvokeMember("Item",
                                                                                    BindingFlags.Default |
                                                                                    BindingFlags.GetProperty,
                                                                                    null, oDocCustomProps,
                                                                                    new object[] { strIndex });
                Type typeDocMT_TSP_ProjectCodeProp = oDocMT_TSP_ProjectCodeProp.GetType();
                strValue = typeDocMT_TSP_ProjectCodeProp.InvokeMember("Value",
                                                                      BindingFlags.Default |
                                                                      BindingFlags.GetProperty,
                                                                      null, oDocMT_TSP_ProjectCodeProp,
                                                                      new object[] { }).ToString();
                // MessageBox.Show("The MT_TSP_ProjectCode is: " + strValue, "MT_TSP_ProjectCode");

                // Set the MT_TSP_ProjectCode strindex to the text set in Project Code

                typeDocMT_TSP_ProjectCodeProp.InvokeMember("Item",
                                                           BindingFlags.Default |
                                                           BindingFlags.SetProperty,
                                                           null, oDocCustomProps,
                                                           new object[] { strIndex, textBox1.Text });


                //Get the MT_TSP_ProjectName property
                strIndex = "MT_TSP_ProjectName";

                object oDocMT_TSP_ProjectNameProp = typeDocCustomProps.InvokeMember("Item",
                                                                                    BindingFlags.Default |
                                                                                    BindingFlags.GetProperty,
                                                                                    null, oDocCustomProps,
                                                                                    new object[] { strIndex });
                Type typeDocMT_TSP_ProjectNameProp = oDocMT_TSP_ProjectNameProp.GetType();
                strValue = typeDocMT_TSP_ProjectCodeProp.InvokeMember("Value",
                                                                      BindingFlags.Default |
                                                                      BindingFlags.GetProperty,
                                                                      null, oDocMT_TSP_ProjectNameProp,
                                                                      new object[] { }).ToString();

                // Set the MT_TSP_ProjectName strindex to the text set in Project Name

                typeDocMT_TSP_ProjectCodeProp.InvokeMember("Item",
                                                           BindingFlags.Default |
                                                           BindingFlags.SetProperty,
                                                           null, oDocCustomProps,
                                                           new object[] { strIndex, textBox2.Text });

                //Get the MT_TSP_Projectlocation property
                strIndex = "MT_TSP_Projectlocation";

                object oDocMT_TSP_ProjectlocationProp = typeDocCustomProps.InvokeMember("Item",
                                                                                        BindingFlags.Default |
                                                                                        BindingFlags.GetProperty,
                                                                                        null, oDocCustomProps,
                                                                                        new object[] { strIndex });
                Type typeDocMT_TSP_ProjectlocationProp = oDocMT_TSP_ProjectlocationProp.GetType();
                strValue = typeDocMT_TSP_ProjectlocationProp.InvokeMember("Value",
                                                                          BindingFlags.Default |
                                                                          BindingFlags.GetProperty,
                                                                          null, oDocMT_TSP_ProjectlocationProp,
                                                                          new object[] { }).ToString();

                // Set the MT_TSP_Projectlocation strindex to the text set in Project Location

                typeDocMT_TSP_ProjectlocationProp.InvokeMember("Item",
                                                               BindingFlags.Default |
                                                               BindingFlags.SetProperty,
                                                               null, oDocCustomProps,
                                                               new object[] { strIndex, textBox3.Text });

                //Get the MT_TSP_Date property
                strIndex = "MT_TSP_Date";

                object oDocMT_TSP_DateProp = typeDocCustomProps.InvokeMember("Item",
                                                                             BindingFlags.Default |
                                                                             BindingFlags.GetProperty,
                                                                             null, oDocCustomProps,
                                                                             new object[] { strIndex });
                Type typeDocMT_TSP_DateProp = oDocMT_TSP_DateProp.GetType();
                strValue = typeDocMT_TSP_DateProp.InvokeMember("Value",
                                                               BindingFlags.Default |
                                                               BindingFlags.GetProperty,
                                                               null, oDocMT_TSP_DateProp,
                                                               new object[] { }).ToString();

                // Set the MT_TSP_Date strindex to the text set in Project Date

                typeDocMT_TSP_DateProp.InvokeMember("Item",
                                                    BindingFlags.Default |
                                                    BindingFlags.SetProperty,
                                                    null, oDocCustomProps,
                                                    new object[] { strIndex, dateTimePicker1.Text });

                // Update all fields

                // Update main body
                oDoc.Fields.Update();


                // Loop through all sections
                foreach (Section section in oDoc.Sections)
                {
                    //Get all Headers
                    HeadersFooters headers = section.Headers;

                    // Update fields in header
                    foreach (HeaderFooter header in headers)
                    {
                        Fields fields = header.Range.Fields;
                        foreach (Field field in fields)
                        {
                            field.Update();
                        }
                    }

                    //Get all footers
                    HeadersFooters footers = section.Footers;

                    // Update fields in footer
                    foreach (HeaderFooter footer in footers)
                    {
                        Fields fields = footer.Range.Fields;
                        foreach (Field field in fields)
                        {
                            field.Update();
                        }
                    }
                }

                // Update table of contents
                oDoc.TablesOfContents[1].Update();

                // Save and close word document

                oDoc.Save();
                oDoc.Close();

                // Quitting the work application so WINWORD.EXE does not persist in memory

                oWord.Quit();

                // Releasing COM Objects (Normal.dotm was being in use if this was not done)

                GC.Collect();
                GC.WaitForPendingFinalizers();
                Marshal.FinalReleaseComObject(oDoc);
            }
        }