Andrew Duffy
Andrew Duffy

Reputation: 583

Aspose.pdf for .net image insertion going over text

I'm using aspose.pdf for .net and when i dynamically insert an image, the adjacent text on the pdf runs underneath the image.

I am replacing a placeholder (eg %checkboxImage%) with an actual image

enter image description here

I'm just using the code from their docs as per below

        private void AddImage(TextFragment textFragment, int imageWidth, int imageHeight, Stream stream)
        {
            var currentPage = textFragment.Page;

            var position = new Position(0, 0);
            position = textFragment.Position;
            position.YIndent = textFragment.Position.YIndent;
            textFragment.Text = "";
            if (position.XIndent != 0 && position.YIndent != 0)
            {
                // Set coordinates
                double lowerLeftX = position.XIndent;
                double lowerLeftY = position.YIndent;
                double upperRightX = position.XIndent + imageWidth;
                double upperRightY = position.YIndent + imageHeight;

                currentPage.Resources.Images.Add(stream);
                currentPage.Contents.Add(new GSave());
                var rectangle = new Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
                var matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY - 10 });
                currentPage.Contents.Add(new ConcatenateMatrix(matrix));
                XImage ximage = currentPage.Resources.Images[currentPage.Resources.Images.Count];
                currentPage.Contents.Add(new Do(ximage.Name));
                currentPage.Contents.Add(new GRestore());
            }
        }

I've tried things like setting a margin for the image, also tried having the placeholder and the adjacent text in a separate cells of a table.

The z-index of the image is 0 perhaps it should be something else? But what?

Thanks for any ideas

Upvotes: 0

Views: 1607

Answers (1)

Farhan Raza
Farhan Raza

Reputation: 392

You may simply add the image using ImageStamp class where you can control its position with XIndent and YIndent properties as per your requirements. Below is a sample code snippet for your kind reference, which may be modified or enhanced further.

// Open document
Document pdfDocument = new Document(dataDir+ "AddImageStamp.pdf");

// Create image stamp
ImageStamp imageStamp = new ImageStamp(dataDir + "aspose-logo.jpg");
imageStamp.Background = true;
imageStamp.XIndent = 100;
imageStamp.YIndent = 100;
imageStamp.Height = 300;
imageStamp.Width = 300;
imageStamp.Rotate = Rotation.on270;
imageStamp.Opacity = 0.5;
// Add stamp to particular page
pdfDocument.Pages[1].AddStamp(imageStamp);

dataDir = dataDir + "AddImageStamp_out.pdf";
// Save output document
pdfDocument.Save(dataDir);

If you still need any assistance then please share your source document and image file along with SSCCE code so that I may investigate and assist you accordingly.

PS: I work with Aspose as Developer Evangelist.

Upvotes: 1

Related Questions