Header and footer examples

31st May 2016
admin-marketing

Switch code for this example

These examples were written in answer to a question on Stack Overflow regarding stamping content on an existing document in iText5. They have been ported to iText7



StampHeader1.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/24678640/itext-pdfdocument-page-size-inaccurate
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.kernel.color.Color;
  16. import com.itextpdf.kernel.font.PdfFontFactory;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfReader;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.layout.Document;
  21. import com.itextpdf.layout.element.Paragraph;
  22. import com.itextpdf.layout.property.TextAlignment;
  23. import com.itextpdf.layout.property.VerticalAlignment;
  24. import com.itextpdf.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. import java.io.File;
  30.  
  31.  
  32. @Category(SampleTest.class)
  33. public class StampHeader1 extends GenericTest {
  34.     public static final String DEST = "./target/test/resources/sandbox/stamper/stamp_header1.pdf";
  35.     public static final String SRC = "./src/test/resources/pdfs/Right.pdf";
  36.  
  37.     public static void main(String[] args) throws Exception {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new StampHeader1().manipulatePdf(DEST);
  41.     }
  42.  
  43.     @Override
  44.     protected void manipulatePdf(String dest) throws Exception {
  45.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  46.         Document doc = new Document(pdfDoc);
  47.         Paragraph header = new Paragraph("Copy")
  48.                 .setFont(PdfFontFactory.createFont(FontConstants.HELVETICA))
  49.                 .setFontSize(14)
  50.                 .setFontColor(Color.RED);
  51.         for (int i = 1; i
StampHeader2.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/24678640/itext-pdfdocument-page-size-inaccurate
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.kernel.font.PdfFontFactory;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfReader;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.Paragraph;
  21. import com.itextpdf.layout.property.TextAlignment;
  22. import com.itextpdf.layout.property.VerticalAlignment;
  23. import com.itextpdf.samples.GenericTest;
  24. import com.itextpdf.test.annotations.type.SampleTest;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28. import java.io.File;
  29.  
  30.  
  31. @Category(SampleTest.class)
  32. public class StampHeader2 extends GenericTest {
  33.     public static final String DEST = "./target/test/resources/sandbox/stamper/stamp_header2.pdf";
  34.     public static final String SRC = "./src/test/resources/pdfs/Wrong.pdf";
  35.  
  36.     public static void main(String[] args) throws Exception {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new StampHeader2().manipulatePdf(DEST);
  40.     }
  41.  
  42.     @Override
  43.     protected void manipulatePdf(String dest) throws Exception {
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  45.         Document doc = new Document(pdfDoc);
  46.  
  47.         Paragraph header = new Paragraph("Copy")
  48.                 .setFont(PdfFontFactory.createFont(FontConstants.HELVETICA))
  49.                 .setFontSize(14);
  50.         for (int i = 1; i
StampHeader3.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/24678640/itext-pdfdocument-page-size-inaccurate
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.kernel.font.PdfFontFactory;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfReader;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.Paragraph;
  21. import com.itextpdf.layout.property.TextAlignment;
  22. import com.itextpdf.layout.property.VerticalAlignment;
  23. import com.itextpdf.samples.GenericTest;
  24. import com.itextpdf.test.annotations.type.SampleTest;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28. import java.io.File;
  29.  
  30.  
  31. @Category(SampleTest.class)
  32. public class StampHeader3 extends GenericTest {
  33.     public static final String DEST = "./target/test/resources/sandbox/stamper/stamp_header3.pdf";
  34.     public static final String SRC = "./src/test/resources/pdfs/Wrong.pdf";
  35.  
  36.     public static void main(String[] args) throws Exception {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new StampHeader3().manipulatePdf(DEST);
  40.     }
  41.  
  42.     @Override
  43.     protected void manipulatePdf(String dest) throws Exception {
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  45.         Paragraph header = new Paragraph("Copy")
  46.                 .setFont(PdfFontFactory.createFont(FontConstants.HELVETICA))
  47.                 .setFontSize(6);
  48.         Document doc = new Document(pdfDoc);
  49.         float x, y;
  50.         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