Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
ODF .NET
>
Tutorial
> Get header and footer text
The following example shows you how to get text from header and footer.
C# example
using System; using System.Collections.Generic; using Independentsoft.Office.Odf; using Independentsoft.Office.Odf.Styles; namespace Sample { class Program { static void Main(string[] args) { TextDocument doc = new TextDocument("c:\\test\\input.odt"); IList
masterPages = doc.CommonStyles.MasterStyles.MasterPages; foreach (MasterPage masterPage in masterPages) { Header header = masterPage.Header; Footer footer = masterPage.Footer; if (header != null) { IList
headerContent = header.Content; foreach (IHeaderFooterContent headerContentElement in headerContent) { if (headerContentElement is Paragraph) { Paragraph paragraph = (Paragraph)headerContentElement; for (int j = 0; j < paragraph.Content.Count; j++) { if (paragraph.Content[j] is AttributedText) { AttributedText attributedText = (AttributedText)paragraph.Content[j]; for (int a = 0; a < attributedText.Content.Count; a++) { if (attributedText.Content[a] is Text) { Text text = (Text)attributedText.Content[a]; Console.WriteLine(text.Value); } } } else if (paragraph.Content[j] is Text) { Text text = (Text)paragraph.Content[j]; Console.WriteLine(text.Value); } } } } } if (footer != null) { IList
footerContent = footer.Content; foreach (IHeaderFooterContent footerContentElement in footerContent) { if (footerContentElement is Paragraph) { Paragraph paragraph = (Paragraph)footerContentElement; for (int j = 0; j < paragraph.Content.Count; j++) { if (paragraph.Content[j] is AttributedText) { AttributedText attributedText = (AttributedText)paragraph.Content[j]; for (int a = 0; a < attributedText.Content.Count; a++) { if (attributedText.Content[a] is Text) { Text text = (Text)attributedText.Content[a]; Console.WriteLine(text.Value); } } } else if (paragraph.Content[j] is Text) { Text text = (Text)paragraph.Content[j]; Console.WriteLine(text.Value); } } } } } } Console.WriteLine("Pess any key to close."); Console.Read(); } } }
VB example
Imports System Imports System.Collections.Generic Imports Independentsoft.Office.Odf Imports Independentsoft.Office.Odf.Styles Module Module1 Sub Main(ByVal args() As String) Dim doc As New TextDocument("c:\\test\\input.odt") Dim masterPages As IList(Of MasterPage) = doc.CommonStyles.MasterStyles.MasterPages For Each masterPage As MasterPage In masterPages Dim header As Header = masterPage.Header Dim footer As Footer = masterPage.Footer If header IsNot Nothing Then Dim headerContent As IList(Of IHeaderFooterContent) = header.Content For Each headerContentElement As IHeaderFooterContent In headerContent If TypeOf headerContentElement Is Paragraph Then Dim paragraph As Paragraph = DirectCast(headerContentElement, Paragraph) For j As Integer = 0 To paragraph.Content.Count - 1 If TypeOf paragraph.Content(j) Is AttributedText Then Dim attributedText As AttributedText = DirectCast(paragraph.Content(j), AttributedText) For a As Integer = 0 To attributedText.Content.Count - 1 If TypeOf attributedText.Content(a) Is Text Then Dim text As Text = DirectCast(attributedText.Content(a), Text) Console.WriteLine(text.Value) End If Next ElseIf TypeOf paragraph.Content(j) Is Text Then Dim text As Text = DirectCast(paragraph.Content(j), Text) Console.WriteLine(text.Value) End If Next End If Next End If If footer IsNot Nothing Then Dim footerContent As IList(Of IHeaderFooterContent) = footer.Content For Each footerContentElement As IHeaderFooterContent In footerContent If TypeOf footerContentElement Is Paragraph Then Dim paragraph As Paragraph = DirectCast(footerContentElement, Paragraph) For j As Integer = 0 To paragraph.Content.Count - 1 If TypeOf paragraph.Content(j) Is AttributedText Then Dim attributedText As AttributedText = DirectCast(paragraph.Content(j), AttributedText) For a As Integer = 0 To attributedText.Content.Count - 1 If TypeOf attributedText.Content(a) Is Text Then Dim text As Text = DirectCast(attributedText.Content(a), Text) Console.WriteLine(text.Value) End If Next ElseIf TypeOf paragraph.Content(j) Is Text Then Dim text As Text = DirectCast(paragraph.Content(j), Text) Console.WriteLine(text.Value) End If Next End If Next End If Next Console.WriteLine("Pess any key to close.") Console.Read() End Sub End Module
Need help? Ask our developers:
Name*
Email*
Message*