Cut and paste content in a page

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

30th October 2015
admin-marketing

Switch code for this example

CutAndPaste.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to
  3.  * http://stackoverflow.com/questions/28368317/itext-or-itextsharp-move-text-in-an-existing-pdf
  4.  */
  5. package sandbox.merge;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Rectangle;
  10. import com.itextpdf.text.pdf.PdfContentByte;
  11. import com.itextpdf.text.pdf.PdfImportedPage;
  12. import com.itextpdf.text.pdf.PdfReader;
  13. import com.itextpdf.text.pdf.PdfTemplate;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15. import java.io.File;
  16.  
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class CutAndPaste {
  23.  
  24.     /** The original PDF file. */
  25.     public static final String SRC
  26.         = "resources/pdfs/page229.pdf";
  27.  
  28.     /** The resulting PDF file. */
  29.     public static final String DEST
  30.         = "results/merge/page229_cut-paste.pdf";
  31.    
  32.     /**
  33.      * Manipulates a PDF file src with the file dest as result
  34.      * @param src the original PDF
  35.      * @param dest the resulting PDF
  36.      * @throws IOException
  37.      * @throws DocumentException
  38.      */
  39.     public void manipulatePdf(String src, String dest)
  40.         throws IOException, DocumentException {
  41.         // Creating a reader
  42.         PdfReader reader = new PdfReader(src);
  43.         // step 1
  44.         Rectangle pageSize = reader.getPageSize(1);
  45.         Rectangle toMove = new Rectangle(100, 500, 200, 600);
  46.         Document document = new Document(pageSize);
  47.         // step 2
  48.         PdfWriter writer
  49.             = PdfWriter.getInstance(document, new FileOutputStream(dest));
  50.         // step 3
  51.         document.open();
  52.         // step 4
  53.         PdfImportedPage page = writer.getImportedPage(reader, 1);
  54.         PdfContentByte cb = writer.getDirectContent();
  55.         PdfTemplate template1 = cb.createTemplate(pageSize.getWidth(), pageSize.getHeight());
  56.         template1.rectangle(0, 0, pageSize.getWidth(), pageSize.getHeight());
  57.         template1.rectangle(toMove.getLeft(), toMove.getBottom(),
  58.                 toMove.getWidth(), toMove.getHeight());
  59.         template1.eoClip();
  60.         template1.newPath();
  61.         template1.addTemplate(page, 0, 0);
  62.         PdfTemplate template2 = cb.createTemplate(pageSize.getWidth(), pageSize.getHeight());
  63.         template2.rectangle(toMove.getLeft(), toMove.getBottom(),
  64.                 toMove.getWidth(), toMove.getHeight());
  65.         template2.clip();
  66.         template2.newPath();
  67.         template2.addTemplate(page, 0, 0);
  68.         cb.addTemplate(template1, 0, 0);
  69.         cb.addTemplate(template2, -20, -2);
  70.         // step 4
  71.         document.close();
  72.         reader.close();
  73.     }
  74.    
  75.     /**
  76.      * Main method.
  77.      * @param    args    no arguments needed
  78.      * @throws DocumentException
  79.      * @throws IOException
  80.      */
  81.     public static void main(String[] args)
  82.         throws IOException, DocumentException {
  83.         File file = new File(DEST);
  84.         file.getParentFile().mkdirs();
  85.         new CutAndPaste().manipulatePdf(SRC, DEST);
  86.     }
  87. }
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