Skip to main content
Skip table of contents

Cut and paste content in a page

Example in answer to the question Click How to move the content inside a rectangle inside an existing PDF?


cutandpaste

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.merge;

import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class CutAndPaste {
    public static final String DEST = "./target/sandbox/merge/page229_cut_paste.pdf";

    public static final String SRC = "./src/main/resources/pdfs/page229.pdf";

    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new CutAndPaste().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws IOException {
        PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
        Rectangle pageSize = srcDoc.getFirstPage().getPageSize();
        PdfDocument resultPdfDoc = new PdfDocument(new PdfWriter(dest));
        resultPdfDoc.setDefaultPageSize(new PageSize(pageSize));
        resultPdfDoc.addNewPage();

        PdfFormXObject pageXObject = srcDoc.getFirstPage().copyAsFormXObject(resultPdfDoc);
        Rectangle toMove = new Rectangle(100, 500, 100, 100);

        // Create a formXObject of a page content, in which the area to move is cut.
        PdfFormXObject formXObject1 = new PdfFormXObject(pageSize);
        PdfCanvas canvas1 = new PdfCanvas(formXObject1, resultPdfDoc);
        canvas1.rectangle(0, 0, pageSize.getWidth(), pageSize.getHeight());
        canvas1.rectangle(toMove);

        // This method uses the even-odd rule to determine which regions lie inside the clipping path.
        canvas1.eoClip();
        canvas1.endPath();
        canvas1.addXObjectAt(pageXObject, 0, 0);

        // Create a formXObject of the area to move.
        PdfFormXObject formXObject2 = new PdfFormXObject(pageSize);
        PdfCanvas canvas2 = new PdfCanvas(formXObject2, resultPdfDoc);
        canvas2.rectangle(toMove);

        // This method uses the nonzero winding rule to determine which regions lie inside the clipping path.
        canvas2.clip();
        canvas2.endPath();
        canvas2.addXObjectAt(pageXObject, 0, 0);

        PdfCanvas canvas = new PdfCanvas(resultPdfDoc.getFirstPage());
        canvas.addXObjectAt(formXObject1, 0, 0);

        // Add the area to move content, shifted 10 points to the left and 2 points to the bottom.
        canvas.addXObjectAt(formXObject2, -20, -2);

        srcDoc.close();
        resultPdfDoc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;

namespace iText.Samples.Sandbox.Merge
{
    public class CutAndPaste
    {
        public static readonly String DEST = "results/sandbox/merge/page229_cut_paste.pdf";

        public static readonly String SRC = "../../../resources/pdfs/page229.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new CutAndPaste().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
            Rectangle pageSize = srcDoc.GetFirstPage().GetPageSize();
            PdfDocument resultPdfDoc = new PdfDocument(new PdfWriter(dest));
            resultPdfDoc.SetDefaultPageSize(new PageSize(pageSize));
            resultPdfDoc.AddNewPage();

            PdfFormXObject pageXObject = srcDoc.GetFirstPage().CopyAsFormXObject(resultPdfDoc);
            Rectangle toMove = new Rectangle(100, 500, 100, 100);

            // Create a formXObject of a page content, in which the area to move is cut.
            PdfFormXObject formXObject1 = new PdfFormXObject(pageSize);
            PdfCanvas canvas1 = new PdfCanvas(formXObject1, resultPdfDoc);
            canvas1.Rectangle(0, 0, pageSize.GetWidth(), pageSize.GetHeight());
            canvas1.Rectangle(toMove);

            // This method uses the even-odd rule to determine which regions lie inside the clipping path.
            canvas1.EoClip();
            canvas1.EndPath();
            canvas1.AddXObjectAt(pageXObject, 0, 0);

            // Create a formXObject of the area to move.
            PdfFormXObject formXObject2 = new PdfFormXObject(pageSize);
            PdfCanvas canvas2 = new PdfCanvas(formXObject2, resultPdfDoc);
            canvas2.Rectangle(toMove);

            // This method uses the nonzero winding rule to determine which regions lie inside the clipping path.
            canvas2.Clip();
            canvas2.EndPath();
            canvas2.AddXObjectAt(pageXObject, 0, 0);

            PdfCanvas canvas = new PdfCanvas(resultPdfDoc.GetFirstPage());
            canvas.AddXObjectAt(formXObject1, 0, 0);

            // Add the area to move content, shifted 10 points to the left and 2 points to the bottom.
            canvas.AddXObjectAt(formXObject2, -20, -2);

            srcDoc.Close();
            resultPdfDoc.Close();
        }
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.