0

My code sample is as follows


    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        addFooter(writer);
    }

    private void addFooter(PdfWriter writer){
        PdfPTable footer = new PdfPTable(2);
        try {
            // set defaults
            footer.setWidths(new int[]{2, 24});
            footer.setWidthPercentage(50);

            footer.setTotalWidth(527);
            footer.setLockedWidth(true);
            footer.getDefaultCell().setFixedHeight(30);
            footer.getDefaultCell().setBorder(Rectangle.TOP);
            footer.getDefaultCell().setBorderColor(BaseColor.RED);
            // here for the text Page 100 of, word of goes below in next line. 
            //It should be in same line.
            footer.addCell(new Phrase(String.format("Page %d of", Integer.parseInt(writer.getPageNumber()+"33")), new Font(Font.FontFamily.HELVETICA, 8)));

            // add placeholder for total page count
            PdfPCell totalPageCount = new PdfPCell(total);
            totalPageCount.setBorder(Rectangle.TOP);
            totalPageCount.setBorderColor(BaseColor.GREEN);
            footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
            footer.addCell(totalPageCount);

            // write page
            PdfContentByte canvas = writer.getDirectContent();
            canvas.beginMarkedContentSequence(PdfName.ARTIFACT);
            footer.writeSelectedRows(0, -1, 34, 20, canvas);
            canvas.endMarkedContentSequence();
        } catch(DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

}

This code is working fine in case of single digit page number. But when page increases to double digits then few texts get shifted to next line. For example if the text is Page 10 of 10, then word "of" goes in next line.

Shashi Shekhar
  • 177
  • 1
  • 4
  • 16
  • You have a very narrow first column. Make it wider to have it host more than single digit page numbers. – mkl Aug 20 '19 at 20:13
  • I tried that as well but it increases the unnecessary space even if change it to only one point more. It gives the output like Page 10 of 10. I mean after "Page 10 of", there is much more space. – Shashi Shekhar Aug 21 '19 at 06:59
  • If you right-align the left cell, the gap will be small, even if you enlarge its column. – mkl Aug 21 '19 at 09:26
  • Have you tried the proposal? – mkl Aug 26 '19 at 10:35
  • Yes I tried. But I didn't see any effect of that change. Can you please give some code example? – Shashi Shekhar Aug 27 '19 at 07:16
  • Does this answer your question? [PDF Page Numbering in Java & iText](https://stackoverflow.com/questions/11205777/pdf-page-numbering-in-java-itext) – Ravindra Gullapalli May 04 '21 at 13:21

1 Answers1

2

The first column of your footer table is very small. So small actually that larger page numbers will automatically cause a line wrap. Thus, give more space to the first column by replacing

footer.setWidths(new int[]{2, 24});

by e.g.

footer.setWidths(new int[]{2, 20});

In a comment you responded to the proposed widening of the first column

but it increases the unnecessary space even if change it to only one point more. It gives the output like Page 10 of 10. I mean after "Page 10 of", there is much more space.

To reduce the gap you can right align the contents of the first column, e.g. by adding

footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);

before the adding the "Page %d of" cell.

Thus,

private void addFooterImproved(PdfWriter writer) {
    PdfPTable footer = new PdfPTable(2);
    try {
        // set defaults
        footer.setWidths(new int[]{2, 20});
        footer.setWidthPercentage(50);

        footer.setTotalWidth(527);
        footer.setLockedWidth(true);
        footer.getDefaultCell().setFixedHeight(30);
        footer.getDefaultCell().setBorder(Rectangle.TOP);
        footer.getDefaultCell().setBorderColor(BaseColor.RED);
        footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
        footer.addCell(new Phrase(String.format("Page %d of", writer.getPageNumber()), new Font(Font.FontFamily.HELVETICA, 8)));

        // add placeholder for total page count
        PdfPCell totalPageCount = new PdfPCell(total);
        totalPageCount.setBorder(Rectangle.TOP);
        totalPageCount.setBorderColor(BaseColor.GREEN);
        footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
        footer.addCell(totalPageCount);

        // write page
        PdfContentByte canvas = writer.getDirectContent();
        canvas.beginMarkedContentSequence(PdfName.ARTIFACT);
        footer.writeSelectedRows(0, -1, 34, 20, canvas);
        canvas.endMarkedContentSequence();
    } catch(DocumentException de) {
        throw new ExceptionConverter(de);
    }
}

(CreateFooter helper method)

The result for a 100 page document:

page 1

...

page 100

mkl
  • 90,588
  • 15
  • 125
  • 265
  • Thanks @mkl. shifting to next line is not happening after this change, but in case of single page number like "Page 1 of 2" there is lot of space. – Shashi Shekhar Aug 28 '19 at 15:32
  • Yes. Essentially tables with fixed column ratios are not good at using space well. What you can do, of course, is first determine the page number, look whether it is a single-digit, double-digit, triple-digit, or whatever number, and use widths you found appropriate for that digit number, `{2, 24}` for single digit, `{2, 20}` for triple digits,... – mkl Aug 28 '19 at 15:41