Header and footer examples

8th November 2015
admin-marketing

iText 5 is no longer being developed, except for security fixes. Click the following link to see an updated example for iText 7.

These examples were written in answer to a question on Stack Overflow regarding stamping content on an existing document.

StampHeader1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/24678640/itext-pdfdocument-page-size-inaccurate
 */
package sandbox.stamper;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
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;
 
/**
 *
 * @author iText
 */
@WrapToTest
public class StampHeader1 {
    
    public static final String SRC = "resources/pdfs/Right.pdf";
    public static final String DEST = "results/stamper/stamped_header1.pdf";
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new StampHeader1().manipulatePdf(SRC, DEST);
    }
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        Phrase header = new Phrase("Copy", new Font(FontFamily.HELVETICA, 14));
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            float x = reader.getPageSize(i).getWidth() / 2;
            float y = reader.getPageSize(i).getTop(20);
            ColumnText.showTextAligned(
                stamper.getOverContent(i), Element.ALIGN_CENTER,
                header, x, y, 0);
        }
        stamper.close();
        reader.close();
    }
}
StampHeader2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/24678640/itext-pdfdocument-page-size-inaccurate
 */
package sandbox.stamper;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
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;
 
/**
 *
 * @author iText
 */
@WrapToTest
public class StampHeader2 {
    
    public static final String SRC = "resources/pdfs/Wrong.pdf";
    public static final String DEST = "results/stamper/stamped_header2.pdf";
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new StampHeader2().manipulatePdf(SRC, DEST);
    }
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        stamper.setRotateContents(false);
        Phrase header = new Phrase("Copy", new Font(FontFamily.HELVETICA, 14));
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            float x = reader.getPageSize(i).getWidth() / 2;
            float y = reader.getPageSize(i).getTop(20);
            ColumnText.showTextAligned(
                stamper.getOverContent(i), Element.ALIGN_CENTER,
                header, x, y, 0);
        }
        stamper.close();
        reader.close();
    }
}
StampHeader3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/24678640/itext-pdfdocument-page-size-inaccurate
 */
package sandbox.stamper;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
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;
 
/**
 *
 * @author iText
 */
@WrapToTest
public class StampHeader3 {
    
    public static final String SRC = "resources/pdfs/Wrong.pdf";
    public static final String DEST = "results/stamper/stamped_header3.pdf";
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new StampHeader3().manipulatePdf(SRC, DEST);
    }
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        Phrase header = new Phrase("Copy", new Font(FontFamily.HELVETICA, 14));
        float x, y;
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            System.out.println(reader.getPageRotation(i));
            if (reader.getPageRotation(i) % 180 == 0) {
                x = reader.getPageSize(i).getWidth() / 2;
                y = reader.getPageSize(i).getTop(20);
            }
            else {
                System.out.println("rotated");
                x = reader.getPageSize(i).getHeight() / 2;
                y = reader.getPageSize(i).getRight(20);
            }
            ColumnText.showTextAligned(
                stamper.getOverContent(i), Element.ALIGN_CENTER,
                header, x, y, 0);
        }
        stamper.close();
        reader.close();
    }
}


Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now