Unconfigured Ad Widget

Collapse

Announcement

Collapse
No announcement yet.

How to add Page X of Y to pdf

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Guest's Avatar
    Guest replied
    To get the "Page X of Y", you would have to do 2 passes over the file because you won't know the total page count until you have created all the pages, unless you could determine this programmatically from the data.

    Bruno Lowagie has made a piece of code available from the latest release of his book "iText in Action, Second Edition by Bruno Lowagie". The online code sample could be found here http://itextpdf.com/examples/iia.php?id=118

    The key part of this code is
    Code:
     // SECOND PASS, ADD THE HEADER
     
            // Create a reader
            PdfReader reader = new PdfReader(baos.toByteArray());
            // Create a stamper
            PdfStamper stamper
                = new PdfStamper(reader, new FileOutputStream(RESULT));
            // Loop over the pages and add a header to each page
            int n = reader.getNumberOfPages();
            for (int i = 1; i <= n; i++) {
                getHeaderTable(i, n).writeSelectedRows(
                        0, -1, 34, 803, stamper.getOverContent(i));
            }
            // Close the stamper
            stamper.close();
        }
     
        /**
         * Create a header table with page X of Y
         * @param x the page number
         * @param y the total number of pages
         * @return a table that can be used as header
         */
        public static PdfPTable getHeaderTable(int x, int y) {
            PdfPTable table = new PdfPTable(2);
            table.setTotalWidth(527);
            table.setLockedWidth(true);
            table.getDefaultCell().setFixedHeight(20);
            table.getDefaultCell().setBorder(Rectangle.BOTTOM);
            table.addCell("FOOBAR FILMFESTIVAL");
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(String.format("Page %d of %d", x, y));
            return table;
        }
    You could see he is using a Reader and a Stamper with reader.getNumberOfPages to determine the number of pages.

    Bruno is the creator of iText. I would recommend that you pick up a copy of this book. It is a great resource for taking full advantage of iText.

    Note: The second edition of his book is for the latest and greatest version of iText. If you are using the version that is used in my AIR book then you might want to get Edition 1, which matches what is currently in the AIR book.

    Leave a comment:


  • dbcumbie
    started a topic How to add Page X of Y to pdf

    How to add Page X of Y to pdf

    I have created a pdf that is 3 pages and was wondering if there is a way to get the page # on each page. Some of the pages are created with the NewPage method, others I may not know when the text goes to the next page. Any help with this would be greatly appreciated.
Working...
X