Header and footer examples

This is a code example of iText PDF, discover more.

8th November 2015
admin-marketing

Switch code for this example

StampHeader1.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/24678640/itext-pdfdocument-page-size-inaccurate
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Element;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.Font.FontFamily;
  11. import com.itextpdf.text.Phrase;
  12. import com.itextpdf.text.pdf.ColumnText;
  13. import com.itextpdf.text.pdf.PdfReader;
  14. import com.itextpdf.text.pdf.PdfStamper;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. /**
  22.  *
  23.  * @author iText
  24.  */
  25. @WrapToTest
  26. public class StampHeader1 {
  27.    
  28.     public static final String SRC = "resources/pdfs/Right.pdf";
  29.     public static final String DEST = "results/stamper/stamped_header1.pdf";
  30.     public static void main(String[] args) throws IOException, DocumentException {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new StampHeader1().manipulatePdf(SRC, DEST);
  34.     }
  35.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  36.         PdfReader reader = new PdfReader(src);
  37.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  38.         Phrase header = new Phrase("Copy", new Font(FontFamily.HELVETICA, 14));
  39.         for (int i = 1; i
StampHeader2.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/24678640/itext-pdfdocument-page-size-inaccurate
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Element;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.Font.FontFamily;
  11. import com.itextpdf.text.Phrase;
  12. import com.itextpdf.text.pdf.ColumnText;
  13. import com.itextpdf.text.pdf.PdfReader;
  14. import com.itextpdf.text.pdf.PdfStamper;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. /**
  22.  *
  23.  * @author iText
  24.  */
  25. @WrapToTest
  26. public class StampHeader2 {
  27.    
  28.     public static final String SRC = "resources/pdfs/Wrong.pdf";
  29.     public static final String DEST = "results/stamper/stamped_header2.pdf";
  30.     public static void main(String[] args) throws IOException, DocumentException {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new StampHeader2().manipulatePdf(SRC, DEST);
  34.     }
  35.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  36.         PdfReader reader = new PdfReader(src);
  37.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  38.         stamper.setRotateContents(false);
  39.         Phrase header = new Phrase("Copy", new Font(FontFamily.HELVETICA, 14));
  40.         for (int i = 1; i
StampHeader3.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/24678640/itext-pdfdocument-page-size-inaccurate
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Element;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.Font.FontFamily;
  11. import com.itextpdf.text.Phrase;
  12. import com.itextpdf.text.pdf.ColumnText;
  13. import com.itextpdf.text.pdf.PdfReader;
  14. import com.itextpdf.text.pdf.PdfStamper;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. /**
  22.  *
  23.  * @author iText
  24.  */
  25. @WrapToTest
  26. public class StampHeader3 {
  27.    
  28.     public static final String SRC = "resources/pdfs/Wrong.pdf";
  29.     public static final String DEST = "results/stamper/stamped_header3.pdf";
  30.     public static void main(String[] args) throws IOException, DocumentException {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new StampHeader3().manipulatePdf(SRC, DEST);
  34.     }
  35.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  36.         PdfReader reader = new PdfReader(src);
  37.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  38.         Phrase header = new Phrase("Copy", new Font(FontFamily.HELVETICA, 14));
  39.         float x, y;
  40.         for (int i = 1; i
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