Free Support Forum - aspose.com

Add image with frame in PDF using Aspose.PDF for .NET

I’m adding image using this code(and a square, but they are not one object):

        const double PDF_PAGE_HEIGHT = 842; // aspose.pdf rectangle for image coordinates starts from left LOWEST corer. Const for convertation
        double lowerLeftX = annotation.Box.X;
        double lowerLeftY = PDF_PAGE_HEIGHT - annotation.Box.Height - annotation.Box.Y;
        double upperRightX = annotation.Box.X + annotation.Box.Width;
        double upperRightY = PDF_PAGE_HEIGHT - annotation.Box.Y;


        string imageLocalPath = ImageAnnotationHelper.CheckImagePath(annotation.ImagePath);
        ImageStamp imageStamp = new ImageStamp(imageLocalPath);
        imageStamp.Background = false;
        imageStamp.XIndent = annotation.Box.X;
        imageStamp.YIndent = PDF_PAGE_HEIGHT - annotation.Box.Height - annotation.Box.Y;
        imageStamp.Height = annotation.Box.Height;
        imageStamp.Width = annotation.Box.Width;
        //imageStamp.Rotate = Rotation.on270; // Now Aspose.PDF supports 4 rotations (0,90,180,270)
        imageStamp.Opacity = annotation.Opacity??1;
        _pdfDocument.Pages[annotation.PageNumber.Value].AddStamp(imageStamp);

        Page page = _pdfDocument.Pages[annotation.PageNumber.Value];
        Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
        XImage ximage = page.Resources.Images[page.Resources.Images.Count];
        SquareAnnotation squareAnnotation = new SquareAnnotation(_pdfDocument.Pages[annotation.PageNumber.Value], rectangle);
        squareAnnotation.Name = ximage.Name;
        squareAnnotation.Border = new Border(squareAnnotation);
        SetPenStyle(squareAnnotation, annotation);
        squareAnnotation.Border.Width = 1;// frame off - 0
        squareAnnotation.Color = GetAnnotationColor(annotation.PenColor);

It’s not the best way out of the situation. After such adding I have what i want, but after user can manipulate or delete frame or picture separately from each other. Here also pdf result with what I have, and word result what I want(created using aspose.words):results.zip (1.2 MB)

If you want, I can send you code that uses Aspose.Words for this result

@chub

Thanks for contacting support.

Yes, please share the code snippet with which you were able to create expected output. Also, please share your sample image file along with respective source PDF document. We will test the scenario in our environment and address it accordingly.

I Already Sent you PDF(in zip file) and code

@chub

We have received the files and observed them. As per our understandings, you need to place image over PDF and want users to be able to delete it? Would you kindly clarify if we understand it correctly and in case not, please share an expected PDF with a bit more details about your requirements. We will further proceed to assist you accordingly.

1 Like

Thanks for all, already found way out of this problem.

@chub

It is good to know that you were able to achieve your requirements. Please keep using our API and in case you still face any issue, please feel free to let us know.