2

The example given in the itext site is working very well.

Page events for headers and footers

However, I am creating pdf from list of html strings so I cannot determine when to set document.newPage() If i don't write document.newPage(), my PdfPgeEventHelper class doesn't update variable on each end of page, instead it gets last updated value.

Main method to create pdf:

public static void createPdf(ArrayList<String> htmlStrings, FooterTable footerEvt, String destinationPath)
        throws IOException, DocumentException {
    Document document = new Document(PageSize.A4);
    document.setMargins(68, 85, 75, 85);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(destinationPath));
    if (footerEvt != null)
        writer.setPageEvent(footerEvt);
    document.open();

    CSSResolver cssResolver = new StyleAttrCSSResolver();
    CssFile cssFile = XMLWorkerHelper
            .getCSS(new ByteArrayInputStream(readCSS("resources/content.min.css").getBytes()));
    cssResolver.addCss(cssFile);

    XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
    fontProvider.register("resources/ARIAL.TTF");

    CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

    PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

    XMLWorker worker = new XMLWorker(css, true);
    XMLParser p = new XMLParser(worker);
    int i = 0;
    for (String htmlfile : htmlStrings) {
        i++;
        footerEvt.setTitleIndex("" + i);//or FooterTable.setTitleIndex("" + i);
        ByteArrayInputStream stream = new ByteArrayInputStream(htmlfile.getBytes("UTF-8"));
        p.parse(stream, Charset.forName("UTF-8"));
    }
    document.close();
}

FooterTable class which extends PdfPageEventHelper

public static void setTitleIndex(String _titleIndex) {
    titleIndex = _titleIndex;
}
public void onEndPage(PdfWriter writer, Document document) {
    pageNumber = writer.getPageNumber();
    generateFooter().writeSelectedRows(0, -1, document.left(), document.bottom() - getTableHeight(footerTable) + 25,
            writer.getDirectContent());
}
public PdfPTable generateFooter() {
    try {
        BaseFont baseFont = BaseFont.createFont("resources/Arial.ttf", BaseFont.IDENTITY_H, true);
        footerTable = new PdfPTable(1);
        footerTable.setTotalWidth(440);
        footerTable.setLockedWidth(true);

        Font pageNumberFont = new Font(baseFont, 9, Font.BOLD);
        Paragraph pageNumberP = new Paragraph(titleIndex+"-"+ pageNumber, pageNumberFont);
        PdfPCell pageNumberCell = new PdfPCell(pageNumberP);
        pageNumberCell.setBorder(0);
        pageNumberCell.setPaddingTop(20);
        footerTable.addCell(pageNumberCell);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return footerTable;
}

For example if there are 10 htmlstrings, in footer every titleIndex is written as 11. But pagenumbers are written correctly.

EXAMPLE OUTPUT:

First page's footer First page's footer

Second page's footer Second page's footer

and it continues like that 11-3, 11-4, 11-5 ...

As you can see the variable is not updating each time. It should be 1-1, 2-2, 3-3, 4-4 ...

NOTE: Using iText 5.5.11

ayZagen
  • 487
  • 7
  • 15
  • 1
    I don't think it is possible to do it without newPage in your example. onEndPage invoked by newPage,I guess. – trd3v3lop Apr 26 '17 at 07:17
  • @trd3v3lop but how pageNumbers are written correctly. I change the pageNumber variable also in the onEndPage – ayZagen Apr 26 '17 at 07:19
  • The `newPage` with the page event happens also when iText automatically creates a new page. – mkl Apr 26 '17 at 08:14
  • @mkl so why not my footer is updated ? if i write `document.newPage()` at the end of the for loop it gets updated – ayZagen Apr 26 '17 at 08:46
  • First of all, the page events obviously are triggered, otherwise you wouldn't get any footers at all. It appears as if either the `p.parse` does not push the parsed elements to the document synchronously (so you would update the title index before the content you assume sent actually is forwarded to the writer) or your remaining code has an error. From the code pieces you shared I cannot tell. – mkl Apr 26 '17 at 10:55
  • @mkl added full codes. – ayZagen Apr 26 '17 at 12:21
  • I'm taking a look – mkl Apr 26 '17 at 13:34
  • I just ran your code using iText 5.5.11 (except for the files and methods you did not provide) and got page numbers 1-1, 2-2, 2-3,3-4, 3-5, 3-6. Thus, either there is something special in your inputs (html strings or css) or your versions. – mkl Apr 26 '17 at 14:09
  • @mkl stange.. does all of your htmls contains tag ? – ayZagen Apr 26 '17 at 14:11
  • None. I used merely 1000 word Lorem Ipsums whose paragraphs were in `

    `...`

    `.
    – mkl Apr 26 '17 at 14:30
  • @ayZagen Have you checked whether you have some opening tags without matching closing tags? – mkl Apr 27 '17 at 14:35
  • @mkl yeah there are no errors at html – ayZagen Apr 28 '17 at 17:34

0 Answers0