Cut and paste content in a page

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

31st May 2016
iText PDF

Switch code for this example

CutAndPaste.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.
  10.  * http://stackoverflow.com/questions/28368317/itext-or-itextsharp-move-text-in-an-existing-pdf
  11.  */
  12. package com.itextpdf.samples.sandbox.merge;
  13.  
  14. import com.itextpdf.kernel.geom.PageSize;
  15. import com.itextpdf.kernel.geom.Rectangle;
  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.kernel.pdf.canvas.PdfCanvas;
  20. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import org.junit.experimental.categories.Category;
  25.  
  26. import java.io.File;
  27. import java.io.IOException;
  28.  
  29. @Category(SampleTest.class)
  30. public class CutAndPaste extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/merge/page229_cut_paste.pdf";
  32.     public static final String SRC = "./src/test/resources/pdfs/page229.pdf";
  33.  
  34.     public static void main(String[] args) throws IOException {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new CutAndPaste().manipulatePdf(DEST);
  38.     }
  39.  
  40.     public void manipulatePdf(String dest) throws IOException {
  41.         Rectangle toMove = new Rectangle(100, 500, 100, 100);
  42.  
  43.         PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
  44.         Rectangle pageSize = srcDoc.getFirstPage().getPageSize();
  45.  
  46.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  47.         pdfDoc.setDefaultPageSize(new PageSize(pageSize));
  48.         pdfDoc.addNewPage();
  49.  
  50.         PdfFormXObject t_canvas1 = new PdfFormXObject(pageSize);
  51.         PdfCanvas canvas1 = new PdfCanvas(t_canvas1, pdfDoc);
  52.  
  53.         PdfFormXObject pageXObject = srcDoc.getFirstPage().copyAsFormXObject(pdfDoc);
  54.  
  55.         canvas1.rectangle(0, 0, pageSize.getWidth(), pageSize.getHeight());
  56.         canvas1.rectangle(toMove.getLeft(), toMove.getBottom(),
  57.                 toMove.getWidth(), toMove.getHeight());
  58.         canvas1.eoClip();
  59.         canvas1.newPath();
  60.         canvas1.addXObject(pageXObject, 0, 0);
  61.  
  62.         PdfFormXObject t_canvas2 = new PdfFormXObject(pageSize);
  63.         PdfCanvas canvas2 = new PdfCanvas(t_canvas2, pdfDoc);
  64.         canvas2.rectangle(toMove.getLeft(), toMove.getBottom(),
  65.                 toMove.getWidth(), toMove.getHeight());
  66.         canvas2.clip();
  67.         canvas2.newPath();
  68.         canvas2.addXObject(pageXObject, 0, 0);
  69.  
  70.         PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());
  71.         canvas.addXObject(t_canvas1, 0, 0);
  72.         canvas.addXObject(t_canvas2, -20, -2);
  73.  
  74.         srcDoc.close();
  75.         pdfDoc.close();
  76.     }
  77. }
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