Adding page numbers to an existing PDF

Tags: page x of yColumnTextPdfStamper

In this example, we use ColumnText to add page numbers to an existing PDF document.

Files: 
package sandbox.stamper;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class StampPageXofY {
 
    public static final String SRC = "resources/pdfs/nameddestinations.pdf";
    public static final String DEST = "results/stamper/pagenumbers.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new StampPageXofY().manipulatePdf(SRC, DEST);
    }
 
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        int n = reader.getNumberOfPages();
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        PdfContentByte pagecontent;
        for (int i = 0; i < n; ) {
            pagecontent = stamper.getOverContent(++i);
            ColumnText.showTextAligned(pagecontent, Element.ALIGN_RIGHT,
                    new Phrase(String.format("page %s of %s", i, n)), 559, 806, 0);
        }
        stamper.close();
        reader.close();
    }
}
File nameRaw URLUpdated
StampPageXofY.javaStampPageXofY.java2015-10-30 6:32 pm
Resources: 
File nameRaw URLUpdated
nameddestinations.pdfnameddestinations.pdf2015-10-25 2:06 am
Results: 
File nameRaw URLUpdated
cmp_pagenumbers.pdfcmp_pagenumbers.pdf2015-10-30 6:34 pm