Page 1 of 1

How to crop PDF page multiple times?

Posted: Thu May 27, 2021 2:17 pm
by jeanleno
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.

Re: How to crop PDF page multiple times?

Posted: Fri Jul 02, 2021 4:55 pm
by Hugo
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