Add image to pdf document at top left most corner

Hi,

I am using Aspose.pdf.Document to create pdf document as it support DOM model.

I am using below code to add image to pdf document but I am unable to place where I need to place it.

I would like to place it in first page, top left corner.

// Set coordinates
int lowerLeftX = 10;
int lowerLeftY = 10;
int upperRightX = 50;
int upperRightY = 50;

// Get the page where image needs to be added
Page page = pdfDocument.Pages.Add();
// Load image into stream
FileStream imageStream = new FileStream(“HealthFirstImage.jpg”, FileMode.Open);
// Add image to Images collection of Page Resources
page.Resources.Images.Add(imageStream);
// Using GSave operator: this operator saves current graphics state
page.Contents.Add(new Operator.GSave());
// Create Rectangle and Matrix objects
Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });

// Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
page.Contents.Add(new Operator.ConcatenateMatrix(matrix));
XImage ximage = page.Resources.Images[page.Resources.Images.Count];
// Using Do operator: this operator draws image
page.Contents.Add(new Operator.Do(ximage.Name));
// Using GRestore operator: this operator restores graphics state
page.Contents.Add(new Operator.GRestore());

Hi Alan,

Thanks for contacting support.

The Image position is calculated from bottom left of page, so in order to render it on top-left position, please get page Height information to place image accordingly. Please try using following code snippet to accomplish your requirement and for your reference, I have also attached the output generated over my end.

[C#]

Document pdfDocument = new Document();

// Set coordinates
int lowerLeftX = 10;
int lowerLeftY = 10;
int upperRightX = 50;
int upperRightY = 50;

// Get the page where the image needs to be added
Page page = pdfDocument.Pages.Add();

// Load image into stream
FileStream imageStream = new FileStream("c:/pdftest/logoSpin.png", FileMode.Open);

// Add the image to the Images collection of Page Resources
page.Resources.Images.Add(imageStream);

// Using GSave operator: this operator saves the current graphics state
page.Contents.Add(new Operator.GSave());

// Create Rectangle and Matrix objects
// Get page height information to place the image
Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, page.PageInfo.Height - lowerLeftY, upperRightX, page.PageInfo.Height - upperRightY);
Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });

// Using ConcatenateMatrix (concatenate matrix) operator: defines how the image must be placed
page.Contents.Add(new Operator.ConcatenateMatrix(matrix));

XImage ximage = page.Resources.Images[page.Resources.Images.Count];

// Using Do operator: this operator draws the image
page.Contents.Add(new Operator.Do(ximage.Name));

// Using GRestore operator: this operator restores the graphics state
page.Contents.Add(new Operator.GRestore());

// Save the PDF file
pdfDocument.Save("c:/pdftest/ConvertedFile.pdf");