/// <summary> /// Called when a HeaderFooter node is encountered in the document. /// </summary> public override VisitorAction VisitHeaderFooterStart(Aspose.Words.HeaderFooter headerFooter) { // Returning this value from a visitor method causes visiting of this // node to stop and move on to visiting the next sibling node. // The net effect in this example is that the text of headers and footers // is not included in the resulting output. return(VisitorAction.SkipThisNode); }
private static void InsertWatermarkIntoHeader(Aspose.Words.Paragraph watermarkPara, Aspose.Words.Section sect, HeaderFooterType headerType) { Aspose.Words.HeaderFooter header = sect.HeadersFooters[headerType]; if (header == null) { header = new Aspose.Words.HeaderFooter(sect.Document, headerType); sect.HeadersFooters.Add(header); } header.AppendChild(watermarkPara.Clone(true)); }
public void BodyNodeType() { //ExStart //ExFor:Body.NodeType //ExFor:HeaderFooter.NodeType //ExFor:Document.FirstSection //ExSummary:Shows how you can enumerate through children of a composite node and detect types of the children nodes. // Open a document. Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + "Section.BodyNodeType.doc"); // Get the first section in the document. Aspose.Words.Section section = doc.FirstSection; // A Section is a composite node and therefore can contain child nodes. // Section can contain only Body and HeaderFooter nodes. foreach (Aspose.Words.Node node in section) { // Every node has the NodeType property. switch (node.NodeType) { case NodeType.Body: { // If the node type is Body, we can cast the node to the Body class. Body body = (Body)node; // Write the content of the main story of the section to the console. Console.WriteLine("*** Body ***"); Console.WriteLine(body.GetText()); break; } case NodeType.HeaderFooter: { // If the node type is HeaderFooter, we can cast the node to the HeaderFooter class. Aspose.Words.HeaderFooter headerFooter = (Aspose.Words.HeaderFooter)node; // Write the content of the header footer to the console. Console.WriteLine("*** HeaderFooter ***"); Console.WriteLine(headerFooter.HeaderFooterType); Console.WriteLine(headerFooter.GetText()); break; } default: { // Other types of nodes never occur inside a Section node. throw new Exception("Unexpected node type in a section."); } } } //ExEnd }
public void CreateFooter() { //ExStart //ExFor:HeaderFooter //ExFor:HeaderFooter.#ctor(DocumentBase, HeaderFooterType) //ExFor:HeaderFooterCollection //ExFor:Story.AppendParagraph //ExSummary:Creates a footer using the document object model and inserts it into a section. Aspose.Words.Document doc = new Aspose.Words.Document(); Aspose.Words.HeaderFooter footer = new Aspose.Words.HeaderFooter(doc, HeaderFooterType.FooterPrimary); doc.FirstSection.HeadersFooters.Add(footer); // Add a paragraph with text to the footer. footer.AppendParagraph("TEST FOOTER"); doc.Save(ExDir + "HeaderFooter.CreateFooter Out.doc"); //ExEnd doc = new Aspose.Words.Document(ExDir + "HeaderFooter.CreateFooter Out.doc"); Assert.True(doc.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary].Range.Text.Contains("TEST FOOTER")); }
public void CreateFooter() { //ExStart //ExFor:HeaderFooter //ExFor:HeaderFooter.#ctor(DocumentBase, HeaderFooterType) //ExFor:HeaderFooterCollection //ExFor:Story.AppendParagraph //ExSummary:Creates a footer using the document object model and inserts it into a section. Aspose.Words.Document doc = new Aspose.Words.Document(); Aspose.Words.HeaderFooter footer = new Aspose.Words.HeaderFooter(doc, HeaderFooterType.FooterPrimary); doc.FirstSection.HeadersFooters.Add(footer); // Add a paragraph with text to the footer. footer.AppendParagraph("TEST FOOTER"); doc.Save(MyDir + "HeaderFooter.CreateFooter Out.doc"); //ExEnd doc = new Aspose.Words.Document(MyDir + "HeaderFooter.CreateFooter Out.doc"); Assert.True(doc.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary].Range.Text.Contains("TEST FOOTER")); }
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.")); }
//ExStart //ExId:HeaderFooterPrimer //ExSummary:Maybe a bit complicated example, but demonstrates many things that can be done with headers/footers. public void Primer() { Aspose.Words.Document doc = new Aspose.Words.Document(); DocumentBuilder builder = new DocumentBuilder(doc); Aspose.Words.Section currentSection = builder.CurrentSection; Aspose.Words.PageSetup pageSetup = currentSection.PageSetup; // Specify if we want headers/footers of the first page to be different from other pages. // You can also use PageSetup.OddAndEvenPagesHeaderFooter property to specify // different headers/footers for odd and even pages. pageSetup.DifferentFirstPageHeaderFooter = true; // --- Create header for the first page. --- pageSetup.HeaderDistance = 20; builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst); builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; // Set font properties for header text. builder.Font.Name = "Arial"; builder.Font.Bold = true; builder.Font.Size = 14; // Specify header title for the first page. builder.Write("Aspose.Words Header/Footer Creation Primer - Title Page."); // --- Create header for pages other than first. --- pageSetup.HeaderDistance = 20; builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary); // Insert absolutely positioned image into the top/left corner of the header. // Distance from the top/left edges of the page is set to 10 points. string imageFileName = MyDir + "Aspose.Words.gif"; builder.InsertImage(imageFileName, RelativeHorizontalPosition.Page, 10, RelativeVerticalPosition.Page, 10, 50, 50, WrapType.Through); builder.ParagraphFormat.Alignment = ParagraphAlignment.Right; // Specify another header title for other pages. builder.Write("Aspose.Words Header/Footer Creation Primer."); // --- Create footer for pages other than first. --- builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary); // We use table with two cells to make one part of the text on the line (with page numbering) // to be aligned left, and the other part of the text (with copyright) to be aligned right. builder.StartTable(); // Clear table borders. builder.CellFormat.ClearFormatting(); builder.InsertCell(); // Set first cell to 1/3 of the page width. builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(100 / 3); // Insert page numbering text here. // It uses PAGE and NUMPAGES fields to auto calculate current page number and total number of pages. builder.Write("Page "); builder.InsertField("PAGE", ""); builder.Write(" of "); builder.InsertField("NUMPAGES", ""); // Align this text to the left. builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left; builder.InsertCell(); // Set the second cell to 2/3 of the page width. builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(100 * 2 / 3); builder.Write("(C) 2001 Aspose Pty Ltd. All rights reserved."); // Align this text to the right. builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Right; builder.EndRow(); builder.EndTable(); builder.MoveToDocumentEnd(); // Make page break to create a second page on which the primary headers/footers will be seen. builder.InsertBreak(BreakType.PageBreak); // Make section break to create a third page with different page orientation. builder.InsertBreak(BreakType.SectionBreakNewPage); // Get the new section and its page setup. currentSection = builder.CurrentSection; pageSetup = currentSection.PageSetup; // Set page orientation of the new section to landscape. pageSetup.Orientation = Orientation.Landscape; // This section does not need different first page header/footer. // We need only one title page in the document and the header/footer for this page // has already been defined in the previous section pageSetup.DifferentFirstPageHeaderFooter = false; // This section displays headers/footers from the previous section by default. // Call currentSection.HeadersFooters.LinkToPrevious(false) to cancel this. // Page width is different for the new section and therefore we need to set // a different cell widths for a footer table. currentSection.HeadersFooters.LinkToPrevious(false); // If we want to use the already existing header/footer set for this section // but with some minor modifications then it may be expedient to copy headers/footers // from the previous section and apply the necessary modifications where we want them. CopyHeadersFootersFromPreviousSection(currentSection); // Find the footer that we want to change. Aspose.Words.HeaderFooter primaryFooter = currentSection.HeadersFooters[HeaderFooterType.FooterPrimary]; Row row = primaryFooter.Tables[0].FirstRow; row.FirstCell.CellFormat.PreferredWidth = PreferredWidth.FromPercent(100 / 3); row.LastCell.CellFormat.PreferredWidth = PreferredWidth.FromPercent(100 * 2 / 3); // Save the resulting document. doc.Save(MyDir + "HeaderFooter.Primer Out.doc"); }