MS word header/footer update

Does Aspose provide APIs for header & footer update in MS word docs.

Hi there,

Thanks for your interest in Aspose. I’m moving your request to Aspose.Words, there one of my colleagues will guide you properly.

Best Regards,

Hi,

Thanks for your inquiry. Yes, Aspose.Words provide API for header and footer of MS Word document. Please use Section.HeadersFooters property to get the collection of header/footer in a section of document. HeaderFooter class represents a container for the header or footer text of a section. HeaderFooter can contain Paragraph and Table child nodes.

HeaderFooter is a section-level node and can only be a child of Section. There can only be one HeaderFooter or each HeaderFooterType in a Section. Please read following documentation links for your kind reference.
https://docs.aspose.com/words/net/working-with-headers-and-footers/
https://docs.aspose.com/words/net/working-with-columns-and-rows/

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Thanks for your reply.
I am looking to update header of a MS word document (.doc) and modify it’s custom properties programatically. I downloaded trial version of aspose. world.java just to see if aspose can meet our needs.
I tried to use the APIs you suggested. However, still unable to modify the header. Request if you can share some sample. I don’t want to remove the existing header and add a new one. All i need is to update some attributes of an existing header.
I am attaching a sample doc file whose header i need to update. For example: in the attached document’s header, the value of “Rev” is “R”. I want to change the value of “Rev” to R2. Is it possible with Aspose?
Here’s the sample code i tried so far:
---------------

package test;
import com.aspose.words.*;
import java.io.File;
import java.text.MessageFormat;
import java.util.Iterator;
import org.apache.poi.hwpf.usermodel.HeaderStories;
/**
 * Manipulates a document programmatically. Inserts formatted text, paragraphs,
 * images, tables and other content into a Word document.
 */
public class HeaderDemo_Aspose
{
    public static void main(String args[]) throws Exception{
        Document doc = new Document("C:\\Test\\Sample2.doc");
        DocumentBuilder builder = new DocumentBuilder(doc);
        Section currentSection = builder.getCurrentSection();
        HeaderFooter primaryHeader = currentSection.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
        Row row = primaryHeader.getTables().get(0).getFirstRow();

        // Get properties
        // System.out.println("2. Buil-in Properties");
        for (int i = 0; i < doc.getBuiltInDocumentProperties().getCount(); i++)
        {
            DocumentProperty prop = doc.getBuiltInDocumentProperties().get(i);
            // System.out.println(MessageFormat.format("{0}({1}) : {2}", prop.getName(), prop.getType(), prop.getValue()));
        }
        // System.out.println("3. Custom Properties");
        for (int i = 0; i < doc.getCustomDocumentProperties().getCount(); i++)
        {
            DocumentProperty prop = doc.getCustomDocumentProperties().get(i);
            // System.out.println(MessageFormat.format("{0}({1}) : {2}", prop.getName(), prop.getType(), prop.getValue()));
        }
        // End of code to get properties
        NodeCollection ncollection = primaryHeader.getChildNodes();
        int ncount = ncollection.getCount();
        Node node = ncollection.get(0);
        TableCollection tcollection = primaryHeader.getTables();
        tcollection.getCount();
        for(int i=0; i<tcollection.getCount();i++){
            RowCollection rcol = tcollection.get(i).getRows();
            for(int j=0; j<rcol.getCount();j++){
                CellCollection cellCol = rcol.get(j).getCells();
                Iterator itr = cellCol.iterator();
                int i2 = 0;
                while(itr.hasNext()){
                    System.out.println("No: " + i2 + " = " + cellCol.get(i2).getText());
                    i2 ++;
                    itr.next();
                }
            }
        }

        // Save the resulting document.
        // doc.save("C:\\Test\\Sample2.doc");

    }
}

---------------
appreciate your help.
Thanks

Hi,

Thanks for your inquiry. In your case, the value “R” of “Rev” is inside table’s cell. You need to modify the contents of table’s cell. Please use the following code snippet to achieve your requirements. Hope this helps you. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Document doc = new Document(MyDir + "Sample2.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
Section currentSection = builder.getCurrentSection();
HeaderFooter primaryHeader = currentSection.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
Table table = (Table)primaryHeader.getChild(NodeType.TABLE, 0, true);
table.getRows().get(5).getCells().get(3).getFirstParagraph().getRuns().clear();
table.getRows().get(5).getCells().get(3).getFirstParagraph().appendChild(new Run(doc, "R2"));
doc.save(MyDir + "out.docx");

Thanks… that works
Some Qs:
a) Would aspose world support performing the similar operations (modifying doc properties, modifying header/footer, text etc) for doc, docx, xls, xlsx, ppt and pptx formats as well? or do we need aspose total?
b) We have a document title written just below the header section (in the body of the document). This also needs to be updated as per the value in “title” property of the document. I believe that’s also doable. appreciate if you can give some pointers on that as well.
Thanks very much…

Hi Puneet,

Thanks for your inquiry.

puneet112-1:
a) Would aspose world support performing the similar operations (modifying doc properties, modifying header/footer, text etc) for doc, docx, xls, xlsx, ppt and pptx formats as well? or do we need aspose total?

Please note that Aspose.Words for .NET is a class library that enables your applications to perform a great range of document processing tasks. Aspose.Words supports DOC, DOCX, RTF, HTML, OpenDocument, PDF, XPS, EPUB and other formats. With Aspose.Words you can generate, modify, convert, render and print documents without utilizing Microsoft Word®. For more information, please go through the documentation below:
https://docs.aspose.com/words/net/product-overview/

You need to use Aspose.Total to work with doc, docx, xls, xlsx, ppt and pptx formats.

puneet112-1:
b) We have a document title written just below the header section (in the body of the document). This also needs to be updated as per the value in “title” property of the document. I believe that’s also doable. appreciate if you can give some pointers on that as well.

Document.BuiltInDocumentProperties property returns a collection that represents all the built-in document properties of the document. You can use BuiltInDocumentProperties.Title property to gets or sets the title of the document. Please read following documentation link for your kind reference.
https://docs.aspose.com/words/net/work-with-document-properties/