3

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

Pops
  • 30,199
  • 37
  • 136
  • 151
Jalal Sordo
  • 1,605
  • 3
  • 41
  • 68
  • Please look my answer in this post [how-to-extract-header-data-from-docx-in-java-using-poi-3-8](http://stackoverflow.com/questions/19393505/how-to-extract-header-data-from-docx-in-java-using-poi-3-8) – Liquidpie Feb 23 '14 at 13:03

3 Answers3

3

Since your document is in .docx format, you'll need to use the XWPF component API of the POI project. You may find the org.apache.poi.xwpf.usermodel.XWPFHeader class useful (Javadoc), but I've never used it myself.

I couldn't find a good reference for doing this with XWPF, but the following instructions describe accessing headers with HWPF, the analagous interface for older Word documents (AKA .doc docs):

To get at the headers and footers of a Word document, first create a org.apache.poi.hwpf.HWPFDocument. Next, you need to create a org.apache.poi.hwpf.usermodel.HeaderStores, passing it your HWPFDocument. Finally, the HeaderStores gives you access to the headers and footers, including first / even / odd page ones if defined in your document. Additionally, HeaderStores provides a method for removing any macros in the text, which is helpful as many headers and footers do end up with macros in them.

The page those instructions are from implies that header support was never that good in HWPF, let alone XWPF. For more bad news, this other Apache page makes it sound like XWPF development has all but stalled. It's possible that what you want to do is planned but not supported yet.

Pops
  • 30,199
  • 37
  • 136
  • 151
  • The page you linked to only talks about HWPF development not being very active at the moment - most of the effort seems to be going into XWPF as that's where most people are interested – Gagravarr Feb 25 '12 at 10:34
  • If you know anything about XWPF commits, then you know more than I do. However, that page does say "none of the [XWPF] committers are actively adding new features." @Gagravarr – Pops Feb 25 '12 at 23:33
2

Check out Writing Microsoft Word Documents in Java With Apache POI

I never worked with Word file before, but done so with POI library for excel stuff, they are quite easy to follow (they model the row, column, sheet etc for excel) so I am assuming they will be equally easy to do for Word files.

And do quick read on their guide Apache POI - HWPF - Java API to Handle Microsoft Word Files

TS-
  • 4,311
  • 8
  • 40
  • 52
  • I was assuming the same when I started my project, I had so much success with spreadsheets why not use it for Word too? It has been an awful experience, definitely wouldn't recommend it unless you have a plain word doc. Every year when I have to update the Word docs the headers win every single time. – Chris.Stover Dec 01 '16 at 20:32
1

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.

Gagravarr
  • 47,320
  • 10
  • 111
  • 156
  • 1
    can you provide some code examples that allows you to edit the header using the mentioned classes of POI? – Jalal Sordo Feb 28 '12 at 11:06
  • Try looking at the unit tests, there are lots of examples given in there as part of testing that the feature works correctly – Gagravarr Mar 02 '12 at 17:55
  • Additionally, the header works just like any other document part, so once you've learnt enough XWPF to be able to add / change paragraphs and tables, it's exactly the same process for the header – Gagravarr Mar 02 '12 at 17:55
  • @Gagravarr Have you ever successfully changed a table cell with XWPF? – Kent Bull Feb 17 '15 at 23:09