We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to convert .png file to .pdf and add an image on a fixed X and Y location?

Hello All,

How to convert .png file to .pdf and add an image on a fixed X and Y location?
Please help

Thanks & Regards

Hi,


Thanks for contacting support.

In order to convert an image file to PDF format where you can add image on specific location, please try using the following code snippet.

[C#]

//open document<o:p></o:p>

Document pdfDocument = new Document();

//set coordinates

int lowerLeftX = 100;

int lowerLeftY = 100;

int upperRightX = 200;

int upperRightY = 200;

pdfDocument.Pages.Add();

//get the page where image needs to be added

Page page = pdfDocument.Pages[1];

//load image into stream

FileStream imageStream = new FileStream("c:/pdftest/SampleDocument1.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);

Aspose.Pdf.DOM.Matrix matrix = new Aspose.Pdf.DOM.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());

//save updated document

pdfDocument.Save("c:/pdftest/Image_output.pdf");