import com.sini.com.spire.doc.documents.HeaderFooterType;
HeaderFooter footer = section.getHeadersFooters().getFooter();
if (footer != null) {
// 获取页脚内容
for (int ff =0; ff < footer.getParagraphs().getCount() ; ff ++) {
Paragraph paragraph = (Paragraph)footer.getParagraphs().get(ff);
String content = paragraph .getText();
System.out.println("页脚&##### :" + content);
// for (int pp =0;pp< paragraph.getChildObjects().getCount();pp++) {
// String content = paragraph.getChildObjects().get(pp).toString();
// System.out.println("页脚&##### :" + content);
// }
}
如果在页脚里写文字 ,则一点问题都没有。
但是,页脚里只有页码的时候,则什么都取不到。
要怎么样才可以取到页脚里的页码呢?
【以下回答由 GPT 生成】
首先,你可以使用Spire.doc库的replacePageNumber()方法来替换页脚中的页码。这个方法会将页码替换为特定值,然后你就可以通过getText()方法来获取到这个值了。下面是修改后的代码示例:
import com.sini.com.spire.doc.*;
import com.sini.com.spire.doc.documents.*;
// 创建文档对象
Document document = new Document();
// 插入一个新的段落
Section section = document.addSection();
Paragraph paragraph = section.addParagraph();
// 在段落中添加一个页码
paragraph.appendField("页码", FieldType.Field_Page);
// 获取页脚
HeaderFooter footer = section.getHeadersFooters().getFooter();
if (footer != null) {
// 替换页脚中的页码
for (int ff = 0; ff < footer.getParagraphs().getCount(); ff++) {
Paragraph par = (Paragraph)footer.getParagraphs().get(ff);
if(par.getText().contains("Field_Page")){
par.replacePageNumber();
}
}
// 获取页脚内容
for (int ff = 0; ff < footer.getParagraphs().getCount(); ff++) {
Paragraph paragraph = (Paragraph)footer.getParagraphs().get(ff);
String content = paragraph.getText();
System.out.println("页脚内容:" + content);
}
}
这样,你就可以通过替换页脚中的页码来获取到页码的值了。如果你只关注页码而不关心其他内容,你可以将页码替换为固定的值后再进行获取。
不过需要注意的是,Spire.doc并不是一个免费的库,你可能需要购买它的许可证才能使用它的功能。