简体   繁体   中英

Cut rectangle from page and place to another page by using iText7

I want to copy rectangle sized location and put to another page. Eg I have 100 pages and i want to cut in 54 page rectangle which is in x , y and has width , height cordinates and put it into 3 page.
My method look like this:

public void CopyRect(string filenamePdf, 
                     Rectangle rect, 
                     int sourcePageNumber, 
                     int destinationPageNumber)
{
}
  • string filenamePdf is my source pdf
  • rectangle rect is my cordinates (x,y, width, height)
  • int sourcePageNumber is page from where i want to copy that rectangle with information
  • int destinantionPageNumber is page where i want to put that rectangle with information.

How should I describe this method? Thank you.

Here I found a solution:

 public void CopyRectCopy(string filenamePdf,
                          Rectangle rect,
                          int sourcePageNumber,
                          int destinationPageNumber)
    {
        PdfDocument srcDoc = new PdfDocument(new PdfReader(filenamePdf));
        PdfDocument resultPdfDoc = new PdfDocument(newPdfWriter(@"C:\Users\USER123"));
        int pageCounter = srcDoc.GetNumberOfPages();
        Rectangle toMove = rect;
        Rectangle pageSize = srcDoc.GetPage(sourcePageNumber).GetPageSize();
        PdfFormXObject pageXObject = srcDoc.GetPage(sourcePageNumber).CopyAsFormXObject(resultPdfDoc);


        PdfFormXObject formXObjectA = new PdfFormXObject(pageSize);
        for (int i = 1; i <= pageCounter; i++)
        {
            //deletes an rectangle from exact page.
            if (i == sourcePageNumber)
            {
                resultPdfDoc.AddNewPage();

                PdfCanvas canvasas = new PdfCanvas(formXObjectA, resultPdfDoc);
                canvasas.Rectangle(0, 0, 595.28, 841.89);
                canvasas.Rectangle(toMove);
                canvasas.EoClip();
                canvasas.EndPath();
                canvasas.AddXObject(pageXObject, 0, 0);
            }
            else
            {
                srcDoc.CopyPagesTo(i, i, resultPdfDoc);
            }

        }
        // 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.AddXObject(pageXObject, 0, 0);


        PdfCanvas canvas = new PdfCanvas(resultPdfDoc.GetPage(destinationPageNumber));
        PdfCanvas canvass = new PdfCanvas(resultPdfDoc.GetPage(sourcePageNumber));
        // Add the area to move content, shifted 10 points to the left and 2 points to the bottom.
        canvas.AddXObject(formXObject2, 0, -50);
        canvass.AddXObject(formXObjectA, 0, 0);

        srcDoc.Close();
        resultPdfDoc.Close();
    }

It cut a rectangle from one page and paste to another.

EDITED.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM