public override VisitorAction VisitSectionStart(Section section)
        {
            Section previousSection = (Section)section.PreviousSibling;

            // If there is a previous section, attempt to copy any linked header footers.
            // Otherwise, they will not appear in an extracted document if the previous section is missing.
            if (previousSection != null)
            {
                HeaderFooterCollection previousHeaderFooters = previousSection.HeadersFooters;
                if (!section.PageSetup.RestartPageNumbering)
                {
                    section.PageSetup.RestartPageNumbering = true;
                    section.PageSetup.PageStartingNumber   = previousSection.PageSetup.PageStartingNumber +
                                                             pageNumberFinder.PageSpan(previousSection);
                }

                foreach (HeaderFooter previousHeaderFooter in previousHeaderFooters)
                {
                    if (section.HeadersFooters[previousHeaderFooter.HeaderFooterType] == null)
                    {
                        HeaderFooter newHeaderFooter =
                            (HeaderFooter)previousHeaderFooters[previousHeaderFooter.HeaderFooterType].Clone(true);
                        section.HeadersFooters.Add(newHeaderFooter);
                    }
                }
            }

            return(ContinueIfCompositeAcrossPageElseSkip(section));
        }
Пример #2
0
        static void Main(string[] args)
        {
            string filePath = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) + @"\data\" + "Working with Headers.doc";
            // Check for license and apply if exists
            string licenseFile = AppDomain.CurrentDomain.BaseDirectory + "Aspose.Words.lic";

            if (File.Exists(licenseFile))
            {
                // Apply Aspose.Words API License
                Aspose.Words.License license = new Aspose.Words.License();
                // Place license file in Bin/Debug/ Folder
                license.SetLicense("Aspose.Words.lic");
            }


            Document wordDocument          = new Document(filePath);
            HeaderFooterCollection headers = wordDocument.FirstSection.HeadersFooters;

            foreach (HeaderFooter header in headers)
            {
                if (header.HeaderFooterType == HeaderFooterType.HeaderFirst || header.HeaderFooterType == HeaderFooterType.HeaderPrimary || header.HeaderFooterType == HeaderFooterType.HeaderEven)
                {
                    Console.WriteLine(header.GetText());
                }
            }
        }
Пример #3
0
        public void ReplaceText()
        {
            //ExStart
            //ExFor:Document.FirstSection
            //ExFor:Section.HeadersFooters
            //ExFor:HeaderFooterCollection.Item(HeaderFooterType)
            //ExFor:HeaderFooter
            //ExFor:Range.Replace(String, String, FindReplaceOptions)
            //ExSummary:Shows how to replace text in the document footer.
            // Open the template document, containing obsolete copyright information in the footer.
            Document doc = new Document(MyDir + "HeaderFooter.ReplaceText.doc");

            HeaderFooterCollection headersFooters = doc.FirstSection.HeadersFooters;
            HeaderFooter           footer         = headersFooters[HeaderFooterType.FooterPrimary];

            FindReplaceOptions options = new FindReplaceOptions
            {
                MatchCase          = false,
                FindWholeWordsOnly = false
            };

            footer.Range.Replace("(C) 2006 Aspose Pty Ltd.", "Copyright (C) 2011 by Aspose Pty Ltd.", options);

            doc.Save(MyDir + @"\Artifacts\HeaderFooter.ReplaceText.doc");
            //ExEnd

            // Verify that the appropriate changes were made to the output document.
            doc = new Document(MyDir + @"\Artifacts\HeaderFooter.ReplaceText.doc");
            Assert.IsTrue(doc.Range.Text.Contains("Copyright (C) 2011 by Aspose Pty Ltd."));
        }
Пример #4
0
        public void ReplaceText()
        {
            //ExStart
            //ExFor:Document.FirstSection
            //ExFor:Section.HeadersFooters
            //ExFor:HeaderFooterCollection.Item(HeaderFooterType)
            //ExFor:HeaderFooter
            //ExFor:Range.Replace(String, String, FindReplaceOptions)
            //ExSummary:Shows how to replace text in a document's footer.
            Document doc = new Document(MyDir + "Footer.docx");

            HeaderFooterCollection headersFooters = doc.FirstSection.HeadersFooters;
            HeaderFooter           footer         = headersFooters[HeaderFooterType.FooterPrimary];

            FindReplaceOptions options = new FindReplaceOptions
            {
                MatchCase          = false,
                FindWholeWordsOnly = false
            };

            int currentYear = DateTime.Now.Year;

            footer.Range.Replace("(C) 2006 Aspose Pty Ltd.", $"Copyright (C) {currentYear} by Aspose Pty Ltd.", options);

            doc.Save(ArtifactsDir + "HeaderFooter.ReplaceText.docx");
            //ExEnd

            doc = new Document(ArtifactsDir + "HeaderFooter.ReplaceText.docx");

            Assert.IsTrue(doc.Range.Text.Contains($"Copyright (C) {currentYear} by Aspose Pty Ltd."));
        }
Пример #5
0
        static void Main(string[] args)
        {
            Document wordDocument          = new Document("data/Convert Word Doc to Other Formats.doc");
            HeaderFooterCollection footers = wordDocument.FirstSection.HeadersFooters;

            foreach (HeaderFooter footer in footers)
            {
                if (footer.HeaderFooterType == HeaderFooterType.FooterFirst || footer.HeaderFooterType == HeaderFooterType.FooterPrimary || footer.HeaderFooterType == HeaderFooterType.FooterEven)
                {
                    Console.WriteLine(footer.GetText());
                }
            }
        }
        public void ReplaceTextInFooter()
        {
            //ExStart:ReplaceTextInFooter
            Document doc = new Document(MyDir + "Footer.docx");

            HeaderFooterCollection headersFooters = doc.FirstSection.HeadersFooters;
            HeaderFooter           footer         = headersFooters[HeaderFooterType.FooterPrimary];

            FindReplaceOptions options = new FindReplaceOptions {
                MatchCase = false, FindWholeWordsOnly = false
            };

            footer.Range.Replace("(C) 2006 Aspose Pty Ltd.", "Copyright (C) 2020 by Aspose Pty Ltd.", options);

            doc.Save(ArtifactsDir + "FindAndReplace.ReplaceTextInFooter.docx");
            //ExEnd:ReplaceTextInFooter
        }
        private static void ReplaceTextInFooter(string dataDir)
        {
            // ExStart:ReplaceTextInFooter
            // Open the template document, containing obsolete copyright information in the footer.
            Document doc = new Document(dataDir + "HeaderFooter.ReplaceText.doc");

            HeaderFooterCollection headersFooters = doc.FirstSection.HeadersFooters;
            HeaderFooter           footer         = headersFooters[HeaderFooterType.FooterPrimary];

            FindReplaceOptions options = new FindReplaceOptions
            {
                MatchCase          = false,
                FindWholeWordsOnly = false
            };

            footer.Range.Replace("(C) 2006 Aspose Pty Ltd.", "Copyright (C) 2019 by Aspose Pty Ltd.", options);

            doc.Save(dataDir + "HeaderFooter.ReplaceText.doc");
            // ExEnd:ReplaceTextInFooter
        }
        private static void ReplaceTextInFooter(string dataDir)
        {
            // ExStart:ReplaceTextInFooter
            // Open the template document, containing obsolete copyright information in the footer.
            Document doc = new Document(dataDir + "HeaderFooter.ReplaceText.doc");

            // Access header of the Word document.
            HeaderFooterCollection headersFooters = doc.FirstSection.HeadersFooters;
            HeaderFooter           header         = headersFooters[HeaderFooterType.HeaderPrimary];

            // Set options.
            FindReplaceOptions options = new FindReplaceOptions
            {
                MatchCase          = false,
                FindWholeWordsOnly = false
            };

            // Replace text in the header of the Word document.
            header.Range.Replace("Aspose.Words", "Remove", options);

            // Save the Word document.
            doc.Save(dataDir + "HeaderReplace.docx");
            // ExEnd:ReplaceTextInFooter
        }
Пример #9
0
        public void ReplaceText()
        {
            //ExStart
            //ExFor:Document.FirstSection
            //ExFor:Section.HeadersFooters
            //ExFor:HeaderFooterCollection.Item(HeaderFooterType)
            //ExFor:HeaderFooter
            //ExFor:Range.Replace(String, String, Boolean, Boolean)
            //ExSummary:Shows how to replace text in the document footer.
            // Open the template document, containing obsolete copyright information in the footer.
            Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + "HeaderFooter.ReplaceText.doc");

            HeaderFooterCollection headersFooters = doc.FirstSection.HeadersFooters;

            Aspose.Words.HeaderFooter footer = headersFooters[HeaderFooterType.FooterPrimary];
            footer.Range.Replace("(C) 2006 Aspose Pty Ltd.", "Copyright (C) 2011 by Aspose Pty Ltd.", false, false);

            doc.Save(MyDir + "HeaderFooter.ReplaceText Out.doc");
            //ExEnd

            // Verify that the appropriate changes were made to the output document.
            doc = new Aspose.Words.Document(MyDir + "HeaderFooter.ReplaceText Out.doc");
            Assert.IsTrue(doc.Range.Text.Contains("Copyright (C) 2011 by Aspose Pty Ltd."));
        }
        public void ReplacePlaceholderWithInserts(IEnumerable <XElement> inserts)
        {
            _inserts = inserts;

            // Replace the Sections first, then replace the headers and footers afterwards
            foreach (Section section in _doc.Sections)
            {
                Dictionary <HeaderFooterType, List <Node> > headerText = new Dictionary <HeaderFooterType, List <Node> >();
                // Remove the Headers from the section
                HeaderFooterCollection headerFooterCollection = section.HeadersFooters;
                foreach (HeaderFooter header in headerFooterCollection)
                {
                    List <Node> paraCol = new List <Node>();
                    foreach (Paragraph para in header.Paragraphs)
                    {
                        paraCol.Add(para.Clone(true));
                    }
                    headerText.Add(header.HeaderFooterType, paraCol);
                }

                section.ClearHeadersFooters();
                // Replace this section
                section.Range.Replace(new Regex(CompiledMarkupConstants._FIELD_PLACEHOLDER), this, true);
                // Add the Headers back for this section
                foreach (HeaderFooter header in section.HeadersFooters)
                {
                    if (headerText.ContainsKey(header.HeaderFooterType))
                    {
                        IEnumerable <Node> list = (IEnumerable <Node>)headerText[header.HeaderFooterType];
                        list.Reverse();
                        foreach (Node node in list)
                        {
                            header.AppendChild(node);
                        }
                    }
                }
            }
            foreach (Section section in _doc.Sections)
            {
                HeaderFooter header = null;


                // Process Header / Footers in Exact order for Visualfiles
                header = section.HeadersFooters[HeaderFooterType.HeaderPrimary];
                if (header != null)
                {
                    header.Range.Replace(new Regex(CompiledMarkupConstants._FIELD_PLACEHOLDER), this, true);
                    header = null;
                }
                header = section.HeadersFooters[HeaderFooterType.HeaderFirst];
                if (header != null)
                {
                    header.Range.Replace(new Regex(CompiledMarkupConstants._FIELD_PLACEHOLDER), this, true);
                    header = null;
                }

                header = section.HeadersFooters[HeaderFooterType.HeaderEven];
                if (header != null)
                {
                    header.Range.Replace(new Regex(CompiledMarkupConstants._FIELD_PLACEHOLDER), this, true);
                    header = null;
                }
                header = section.HeadersFooters[HeaderFooterType.FooterPrimary];
                if (header != null)
                {
                    header.Range.Replace(new Regex(CompiledMarkupConstants._FIELD_PLACEHOLDER), this, true);
                    header = null;
                }
                header = section.HeadersFooters[HeaderFooterType.FooterFirst];
                if (header != null)
                {
                    header.Range.Replace(new Regex(CompiledMarkupConstants._FIELD_PLACEHOLDER), this, true);
                    header = null;
                }
                header = section.HeadersFooters[HeaderFooterType.FooterEven];
                if (header != null)
                {
                    header.Range.Replace(new Regex(CompiledMarkupConstants._FIELD_PLACEHOLDER), this, true);
                    header = null;
                }
            }

            // Replace any Blank replacements to remove formatting around 'blank' tags
            foreach (Node node in _blankReplacements)
            {
                RemoveEmptyNode(node);
            }
        }