Hello!I’m new to Aspose.I need to get the page number information of the footer of the document and further check if the page number is missing . Here is my code. But it can not get footer (page information) correctly.Could you please help me with my code? Thank you very much!
public static void getFooter(InputStream inputStream) {
Document doc = null;
try {
doc = new Document(inputStream);
for (Section section : doc.getSections()) {
// There are three kinds of footer and header types.
// 1 - The "First" header/footer, which only appears on the first page of a section.
HeaderFooter footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST);
if (footer != null) {
System.out.println(footer.getFirstParagraph().getText());
}
// 2 - The "Primary" header/footer, which appears on odd pages.
footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
if (footer != null) {
System.out.println(footer.getFirstParagraph().getText());
}
// 3 - The "Even" header/footer, which appears on even pages.
footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN);
if (footer != null) {
System.out.println(footer.getFirstParagraph().getText());
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
The output is strange.
@Zhimayou
To retrieve footer information, including page numbers, from a document using Aspose.Words, you can utilize the HeaderFooter
class. Your current implementation is on the right track, but let’s ensure that you are correctly accessing the footer content.
Here’s a refined version of your code that checks for the presence of page numbers in the footer:
public static void getFooter(InputStream inputStream) {
Document doc = null;
try {
doc = new Document(inputStream);
for (Section section : doc.getSections()) {
// Access different types of footers
HeaderFooter footerFirst = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST);
HeaderFooter footerPrimary = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
HeaderFooter footerEven = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN);
// Check and print the text of each footer type
if (footerFirst != null) {
System.out.println("First Footer: " + footerFirst.getText().trim());
} else {
System.out.println("First Footer is missing.");
}
if (footerPrimary != null) {
System.out.println("Primary Footer: " + footerPrimary.getText().trim());
} else {
System.out.println("Primary Footer is missing.");
}
if (footerEven != null) {
System.out.println("Even Footer: " + footerEven.getText().trim());
} else {
System.out.println("Even Footer is missing.");
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Key Points:
- Footer Types: The code checks for three types of footers: first, primary, and even. Each footer type may contain different content based on the section’s configuration.
- Null Checks: It includes checks to determine if each footer type is present and prints a message if it is missing.
- Trimming Text: The
getText()
method is used to retrieve the footer text, and trim()
is applied to remove any leading or trailing whitespace.
This approach should help you accurately retrieve and verify the footer content, including page numbers, in your document. If you continue to experience issues, ensure that the document you are processing actually contains the expected footer information.
Feel free to ask if you have any further questions or need additional assistance!
But what is obtained is not the page number of the document.
@Zhimayou Do you mean the value of PAGE
field in the document’s header/footer? As you may know, MS Word documents are flow by their nature and there is no “page” concept. The consumer applications reflows the document’s content into pages on the fly. The PAGE
field in the document’s header/footer is different for each page and it’s value is generated on the fly.
In the document object model header/footer is applied to section, that can span several pages. So there is no way to get the actual value of PAGE
field from the document’s header/footer.