How to crop PDF page multiple times?

Discussions about PDF management.
Post Reply
jeanleno
Posts: 5
Joined: Wed Oct 26, 2016 1:03 pm

How to crop PDF page multiple times?

Post by jeanleno » Thu May 27, 2021 2:17 pm

Hi.

I'm trying to crop a PDF page using this code, but after 3 crops GdViewer don't show any image and sometimes shows me an OutOfMemory error message.

Code: Select all

private void executeCrop(object parameter)
        {
            if (_gdViewer.MouseMode == ViewerMouseMode.MouseModeAreaSelection)
            {
                double left = 0;
                double top = 0;
                double width = 0;
                double height = 0;
                using (GdPictureImaging _image = new GdPictureImaging())
                {
                    int tempImage = _gdPicturePDF.RenderPageToGdPictureImage(TIFF_DPI, false);

                    _gdViewer.DisplayFromGdPictureImage(tempImage);
                    _gdViewer.GetRectCoordinatesOnDocumentPixel(ref left, ref top, ref width, ref height);

                    if (width > 0 && height > 0)
                    { 
                         if (_image.Crop(tempImage, (int)left, (int)top, (int)width, (int)height) != GdPictureStatus.OK)
                         {
                             MessageBox.Show("Error: " + _image.GetStat());
                             return;
                         }

                         replacePDFPage((float)width, (float)height, tempImage);

                         _gdViewer.ClearRect();
                    }

                    _image.ReleaseGdPictureImage(tempImage);
                }
            }
        }

        private void replacePDFPage(float width, float height, int tempImage)
        {
            int pageNo = _gdPicturePDF.GetCurrentPage();
            //remove the original page from the pdf file
            _gdPicturePDF.DeletePage(pageNo);
            //insert a new, blank page
            GdPictureStatus status;
            if (_gdPicturePDF.GetPageCount() == 0)
            {
                if (_gdPicturePDF.NewPDF() == GdPictureStatus.OK)
                {
                    status = _gdPicturePDF.NewPage(width, height);
                }
            }
            else
            {
                status = _gdPicturePDF.InsertPage(width, height, pageNo);
            }
            //add the cropped image as a resource to the pdf
            string imageResx = _gdPicturePDF.AddImageFromGdPictureImage(tempImage, false, false);
            //draw it on the page
            if (_gdPicturePDF.GetStat() == GdPictureStatus.OK)
            {
                _gdPicturePDF.DrawImage(imageResx, 0, 0, width, height);
            }

            _image.ReleaseGdPictureImage(tempImage);
            _gdViewer.DisplayFromGdPicturePDF(_gdPicturePDF);
        }
What I'm doing wrong? I tested using the latest version 14.1.116.

Hugo
Posts: 227
Joined: Tue Dec 18, 2018 10:09 am

Re: How to crop PDF page multiple times?

Post by Hugo » Fri Jul 02, 2021 4:55 pm

Hi Jeanleno,

Thank you for your question.

Would you be able to sen me the image you are using for this scenario please?
If this document is confidential feel free to contact us and send it here: https://orpalis.zendesk.com/hc/en-us/requests/new
I'll then run your code on your file to see if I am able to reproduce your issue.

Regards,
Hugo Cudd
Technical Support

Post Reply

Who is online

Users browsing this forum: Amazon [Bot] and 1 guest