Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Oct 30, 2019 12:29 pm

Hello Spire.
Please, how to draw a Image with defined Clip region in Spire.PDF ?

For example I want to draw image with a Rectangle Clip Region defined.
So at the output PDF will stay only (small) piece from inside of a image.

Thank you! :)

BR,
Vladimir

procamSpire
 
Posts: 22
Joined: Tue Sep 03, 2019 7:56 am

Thu Oct 31, 2019 8:31 am

Hi,

Thanks for your inquiry.
Please refer to the code below to draw an image on PDF page and save the PDF file at the same size of the image.

Code: Select all
            PdfDocument doc = new PdfDocument();
            Image image = Image.FromFile(@"……\picture.png");
            PdfImage pdfimage = PdfImage.FromImage(image);

            //Get the image size in point and create a new page with image size.
            PdfUnitConvertor uinit = new PdfUnitConvertor();
            SizeF pageSize = uinit.ConvertFromPixels(image.Size, PdfGraphicsUnit.Point);
            PdfPageBase page = doc.Pages.Add(pageSize, new PdfMargins(0f));

            //Draw the PDF image on the PDF page.
            page.Canvas.DrawImage(pdfimage, new PointF(0, 0));
            doc.SaveToFile("result.pdf");


If the code couldn't solve your issue, please offer us your input image and your desired result file for further investigation. You could upload them here or send us(support@e-iceblue.com) via email.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Mon Nov 04, 2019 9:22 am

Hi!
There is no clipping of image in you source sample - only drawing of complete image onto page.
I want to draw only part of image (this is clipping) described by rectangle.

Image

Best regards,
Vladimir

procamSpire
 
Posts: 22
Joined: Tue Sep 03, 2019 7:56 am

Mon Nov 04, 2019 10:22 am

Hi,

Thanks for your reply.
Sorry that Spire.PDF doesn't support to capture part of picture with a clip region then draw it on the PDF page.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Tue Nov 05, 2019 7:58 am

Hi Spire!
Do you have in plans for future to implement the clipping of images?
BR,

Vladimir

procamSpire
 
Posts: 22
Joined: Tue Sep 03, 2019 7:56 am

Tue Nov 05, 2019 10:01 am

Hi,

Thanks for your reply.
We will consider adding this new feature into our upgrade schedule in the future. Once there is any progress, we will inform you.
Sorry for the inconvenience caused.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Fri Nov 15, 2019 12:18 pm

Hi,

Hope you are doing well.
After further investigation, we found that you could draw a clip region of an image on Pdf page by calling the method "SetClip". Below is the code for your reference.

Code: Select all
            PdfDocument doc = new PdfDocument();
            Image image = Image.FromFile(@"……\picture.png");
            PdfImage pdfimage = PdfImage.FromImage(image);

            SizeF pageSize = new SizeF(PdfPageSize.A4);
            PdfPageBase page = doc.Pages.Add(pageSize, new PdfMargins(0f));

            //Set the clip region of the image.
            RectangleF re = new RectangleF(new PointF(100, 100), new SizeF(100, 100));
            page.Canvas.SetClip(re);

            //Draw the image on the Pdf page.
            page.Canvas.DrawImage(pdfimage, new PointF(100, 100));

            //Save to file.
            doc.SaveToFile("result.pdf",FileFormat.PDF);


Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Fri Dec 06, 2019 11:21 am

Hi,

Greetings from E-iceblue.
Have you tried the code I offered you last time? Does it solve your issue?
Could you please give us some feedback at your convenience?

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Return to Spire.PDF