We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to find the presence of a Header/Footer in document?

Hi,

I am creating a .doc file with my own header footer. I am using the below code for to insert my header footer.

Document doc=new Document(“Test.doc”);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.insertHtml(headerStr);
\ headerStr is my header html string with TABLE inside a TABLE
\



The next moment, i am trying to get the header text by using the below code.

builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
com.aspose.words.Node node = builder.getCurrentNode();
System.out.println("node "+node);
if(node != null) {
String headerStr = node.getText();
System.out.println(“headerStr :”+headerStr);
}

But builder.getCurrentNode(); returns null.

I basically need to find if a header is present in the document or not. Is there any work-around or any direct method to find the presence of a Header/Footer?

Waiting for your quick reply.

Thanks,
Chandrasekar.P



Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. I think that you can try using the following code.

//Open document

Document doc = new Document("in.doc");

//Check headers/footers in each section

for(int sectIndex=0; sectIndex<doc.getSections().getCount(); sectIndex++)

{

Section sect = doc.getSections().get(sectIndex);

//Check headers/footers

if(sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN)!=null)

{

String text = sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN).getText();

System.out.println("Footer even:");

System.out.println(text);

}

if(sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST)!=null)

{

String text = sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST).getText();

System.out.println("Footer first:");

System.out.println(text);

}

if(sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY)!=null)

{

String text = sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY).getText();

System.out.println("Footer primary:");

System.out.println(text);

}

if(sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_EVEN)!=null)

{

String text = sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_EVEN).getText();

System.out.println("Header even:");

System.out.println(text);

}

if(sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_FIRST)!=null)

{

String text = sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_FIRST).getText();

System.out.println("Header first:");

System.out.println(text);

}

if(sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY)!=null)

{

String text = sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY).getText();

System.out.println("Header primary:");

System.out.println(text);

}

}

Hope this helps.

Best regards.