Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JODF
>
Tutorial
> Get header and footer text
The following example shows you how to get text from header and footer.
import java.util.List; import com.independentsoft.office.odf.AttributedText; import com.independentsoft.office.odf.Paragraph; import com.independentsoft.office.odf.Text; import com.independentsoft.office.odf.TextDocument; import com.independentsoft.office.odf.styles.Footer; import com.independentsoft.office.odf.styles.Header; import com.independentsoft.office.odf.styles.IHeaderFooterContent; import com.independentsoft.office.odf.styles.MasterPage; public class Example { public static void main(String[] args) { try { TextDocument doc = new TextDocument("c:\\test\\input.odt"); List
masterPages = doc.getCommonStyles().getMasterStyles().getMasterPages(); for (MasterPage masterPage : masterPages) { Header header = masterPage.getHeader(); Footer footer = masterPage.getFooter(); if (header != null) { List
headerContent = header.getContent(); for (IHeaderFooterContent headerContentElement : headerContent) { if (headerContentElement instanceof Paragraph) { Paragraph paragraph = (Paragraph)headerContentElement; for (int j = 0; j < paragraph.getContent().size(); j++) { if (paragraph.getContent().get(j) instanceof AttributedText) { AttributedText attributedText = (AttributedText)paragraph.getContent().get(j); for (int a = 0; a < attributedText.getContent().size(); a++) { if (attributedText.getContent().get(a) instanceof Text) { Text text = (Text)attributedText.getContent().get(a); System.out.println(text.getValue()); } } } else if (paragraph.getContent().get(j) instanceof Text) { Text text = (Text)paragraph.getContent().get(j); System.out.println(text.getValue()); } } } } } if (footer != null) { List
footerContent = footer.getContent(); for(IHeaderFooterContent footerContentElement : footerContent) { if (footerContentElement instanceof Paragraph) { Paragraph paragraph = (Paragraph)footerContentElement; for (int j = 0; j < paragraph.getContent().size(); j++) { if (paragraph.getContent().get(j) instanceof AttributedText) { AttributedText attributedText = (AttributedText)paragraph.getContent().get(j); for (int a = 0; a < attributedText.getContent().size(); a++) { if (attributedText.getContent().get(a) instanceof Text) { Text text = (Text)attributedText.getContent().get(a); System.out.println(text.getValue()); } } } else if (paragraph.getContent().get(j) instanceof Text) { Text text = (Text)paragraph.getContent().get(j); System.out.println(text.getValue()); } } } } } } } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Need help? Ask our developers:
Name*
Email*
Message*