2

I'm generating a word document using Apache POI XWPF. this document includes a footer with Page number but when I convert this word document into PDF, the page number is not displayed:

XWPFRun run = paragraph.createRun();  
XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);

paragraph = footer.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);

run = paragraph.createRun();  
run.setFontSize(8);
run.setItalic(true);
run.setColor(header_color);
run.setText("Page ");
//->Also tried with this function but not displayed when PDF is generated paragraph.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT");
run.getCTR().addNewPgNum();

and the part of my code converting into PDF:

PdfOptions options = PdfOptions.create();
OutputStream outWord = new FileOutputStream(new File(out_pdf));
PdfConverter.getInstance().convert(document, outWord, options);

I tried different ways to insert the page number (correctly inserted in word format) but each time I'm converting the document into PDF I'm losing it...

tiamat
  • 879
  • 2
  • 12
  • 35
  • What version of the OpenSages converter are you using? And if not the latest, what happens when you upgrade? – Gagravarr Mar 30 '20 at 14:33
  • I'm using the latest, version 2.0.2 (Opensagres POI Xwpf Converter PDF) – tiamat Mar 30 '20 at 15:35
  • 1
    Page numbers only can be calculated when the `Word` document is rendered. `Apache poi` does not rendering the document. So the document needs to be opened using `Word` and the re-saved. Only while opening in `Word` the page numbers are calculated and the corresponding fields are filled. – Axel Richter Mar 30 '20 at 16:31
  • Indeed....I'm trying another solution by opening the word document once it is saved and integrate a for loop per page and increase an int value to be inserted in the footer to get this page numer... – tiamat Mar 31 '20 at 06:37

0 Answers0