Free Support Forum - aspose.com

Crop Pdf page

Hello, 
I'm trying to crop a page in a pdf file.
I'm using the following method to do it, but the saved document is wrong, the page cropped 
shows different parts of the page.
if (stream != null)
{

Aspose.Pdf.Document document = new Aspose.Pdf.Document(filePath);
Aspose.Pdf.Rectangle pageRect = new Aspose.Pdf.Rectangle(page.X, page.Y, page.Width, page.Height);
document.Pages[page.PageIndex].CropBox = pageRect;

Page newPage = document.Pages[page.PageIndex];

Resolution resolution = new Resolution(300);

JpegDevice jpegDevice = new JpegDevice(resolution, 100);

jpegDevice.Process(newPage, stream);

document.Save(outputStream);

}

I think the Rectangle parameters are wrong, can someone help me to pass the right parameters ?
Thank you in advanced

Hi there,


Thanks for your inquiry. Please check a similar query here. Hopefully it will help you to get your desired results. If issue persist then please share your source document and dimension for cropping the Pdf document.

Best Regards,

I was able to work around to crop image using Aspose.Pdf.Facades.PdfFileMend.

But now i’m having difficulty to set the right coordinates to my cropped page.

 public MemoryStream CropPage(string filePath, FilePage page)
{
MemoryStream retVal = new MemoryStream();
        <span style="color:blue;">this</span>.DeletePage(filePath, page.PageIndex);

        <span style="color:#2b91af;">MemoryStream</span> imageStream = <span style="color:#2b91af;">FileHelper</span>.DownloadFile(page.PageUrl);

        <span style="color:blue;">if</span> (!<span style="color:blue;">string</span>.IsNullOrEmpty(filePath))
        {
            Aspose.Pdf.<span style="color:#2b91af;">Document</span> pdfDocument = <span style="color:blue;">new</span> Aspose.Pdf.<span style="color:#2b91af;">Document</span>(filePath);
            <span style="color:#2b91af;">PageCollection</span> pageCollection = pdfDocument.Pages;
            <span style="color:#2b91af;">Page</span> pdfPage = pageCollection[1];
            <span style="color:#2b91af;">Page</span> newPage = pdfDocument.Pages.Insert(page.PageIndex);
            newPage.SetPageSize((<span style="color:blue;">double</span>)pdfPage.Rect.Width, (<span style="color:blue;">double</span>)pdfPage.Rect.Height);
            Aspose.Pdf.Facades.<span style="color:#2b91af;">PdfFileMend</span> fileMend = <span style="color:blue;">new</span> Aspose.Pdf.Facades.<span style="color:#2b91af;">PdfFileMend</span>(pdfDocument, retVal);
            <span style="color:blue;">using</span> (imageStream)
            {
                <span style="color:#2b91af;">MemoryStream</span> stream = imageStream;
                <span style="color:blue;">float</span> pdfPageHeightLLY = (<span style="color:blue;">float</span>)page.Height;
                <span style="color:blue;">float</span> pdfPageWidthLLX = (<span style="color:blue;">float</span>)0;
                <span style="color:blue;">float</span> pdfPageHeightURY = (<span style="color:blue;">float</span>)pdfPage.Rect.Height + (<span style="color:blue;">float</span>)page.Y;
                <span style="color:blue;">float</span> pdfPageWidthURX = (<span style="color:blue;">float</span>)page.X;
                <span style="color:blue;">if</span> (imageStream != <span style="color:blue;">null</span>)
                {
                    fileMend.AddImage(imageStream, page.PageIndex, pdfPageWidthLLX, pdfPageHeightLLY, pdfPageWidthURX, pdfPageHeightURY);
                }
                stream = <span style="color:blue;">null</span>;
            }
            pdfDocument.Save(filePath);
            fileMend.Close();

        }

        <span style="color:blue;">return</span> retVal;
    }</pre></div><div>I want the cropped image to be saved at (0,0)</div><div>Any idea that might help?</div><div>thanks in advanced</div>

I Found it!

My coordinate was wrong and I had other style that was changing the display I wanted

 float pdfPageHeightLLY = (float)pdfPage.Rect.Height - (float)page.Height;
float pdfPageWidthLLX = (float)0;
float pdfPageHeightURY = (float)pdfPage.Rect.Height;
float pdfPageWidthURX = (float)page.Width;


Hi there,


Thanks for your feedback. Its good to know you have managed to get your desired results.

Please keep using Aspose products and feel free to ask us for any help. We will be glad to extend our support.

Best Regards,