Search code examples
javafilefile-ioapache-poi.doc

Updating a .docx file's page header using Apache POI


How can I update the page header of a .docx file using the Apache POI 3.7 API?


Solution

  • First up, call getHeaderFooterPolicy() on your XWPFDocument, which returns a HeaderFooterPolicy. From that, you can identify the appropriate header for your page (eg Default, First Page etc)

    Once you have the appropriate XWPFHeader that you want to change, then you can go about editing it as any other document part. You can fetch the tables, the paragraphs etc, then remove them, add new ones, change the text of them etc. It's all the same process then as editing the main document.