Hello there,
I need to insert an image of (PNG or JPEG) at specific coordinates of a page in the PDF file.
The web application gets the file from the document library and renders it into individual PNG files using this method Convert PDF documents using C# API|Aspose.PDF for .NET, like that:
// Open PDF file
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(inputFilePath);
// Measure it
int pageCount = pdfDocument.Pages.Count;
try
{
for (int pageNumber = 1; pageNumber
{
string pageName = String.Format(“Page{0}.png”, pageNumber);
string pagePath = Path.Combine(dataDirectory, pageName);
using (FileStream imageStream = new FileStream(pagePath, FileMode.Create))
{
//create PNG device with specified attributes
//Width, Height, Resolution, Quality
//Quality [0-100], 100 is Maximum
//create Resolution object
Resolution resolution = new Resolution(100);
PngDevice pngDevice = new PngDevice(new PageSize(PageSize.A4.Width, PageSize.A4.Height), resolution);
//convert a particular page and save the image to stream
pngDevice.Process(pdfDocument.Pages[pageNumber], imageStream);
//close stream
imageStream.Close();
}
}
}
catch (IndexOutOfRangeException ex)
{
if (ex.Source == “Aspose.Pdf”)
{
// At most 4 elements (for any collection) can be viewed in evaluation mode.
// Eval mode in Aspose throws it
Logger.Instance.Log(Logger.SERVICE_TRACE_SOURCE, System.Diagnostics.TraceEventType.Error, String.Format(
“DocumentManager.convertPDFFileToPageImages: Aspose.Pdf in evaluation mode”));
}
else
{
throw;
}
}
User reviews the pages in the web browser, and clicks where they want the image to be inserted.
Then, I use method described here https://docs.aspose.com/pdf/net/working-with-images/ to add an image at the specified location, like that
// For each image selected
foreach (ImageLocation imageLocation in imageLocations)
{
// Add images to the right pages
Page page = pdfDocument.Pages[imageLocation.PageNumber];
using (FileStream imageStream = new FileStream(imageLocation.FilePath, FileMode.Open))
{
page.Resources.Images.Add(imageStream);
}
XImage xImage = page.Resources.Images[page.Resources.Images.Count];
Logger.Instance.Log(Logger.SERVICE_TRACE_SOURCE, System.Diagnostics.TraceEventType.Verbose, String.Format(
“DocumentManager.InsertImagesIntoFileInStorage: Added ‘{0}’ file to ‘{1}’ page as XImage.Name=‘{2}’, width=‘{3}’, height=‘{4}’'”,
imageLocation.FilePath,
imageLocation.PageNumber,
xImage.Name,
xImage.Width,
xImage.Height));
// Insert images into right location
//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(imageLocation.XPosition, imageLocation.YPosition, imageLocation.XPosition + xImage.Width, imageLocation.YPosition + xImage.Height);
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));
//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());
}
I am having difficulty in placing the image at exact location I specified and having it be the same size I specified.
Attached is the document. It already contained a 100x100 PNG with yellow background and “2” upside down in the bottom-right corner, at the bottom left of the first page.
The user inserted 100x100 PNG with orange background and “1” proper way up in the upper left corner at x=74 and y=176 coordinates as measured from bottom left of the page, just above the original image and aligned to it’s left side
Added ‘D:\Projects<snip>\Documents\21\2_Stamp1.png’ file to ‘1’ page as XImage.Name=‘Im1’, width=‘100’, height=‘100’
Aspose.Pdf.Rectangle LLX=‘74’, LLY=‘176’, URX=‘174’, URY=‘276’, Width=‘100’, Height=‘100’
Aspose.Pdf.DOM.Matrix A=‘100’, B=‘0’, C=‘0’, D=‘100’, E=‘74’, F=‘176’
The original image measures 1.04" in size (as it was when I inserted it.
The second image is displayed visually offset from where I want it to. It also measures 1.78" in size, and 1.04/1.78 happens to be pretty close to 0.75% which makes me think some sort of scaling going on
Question: How do I insert the image at the right location and scale in the page?
I am using Aspose.PDF 9.1.0.0.
Thank you
Hi Daniel,
Thanks for your inquiry. Please note that the basic measuring unit in Aspose.Pdf for .NET is point, where 1 inch = 72 points and 1 cm = 1/2.54 inch = 0.3937 inch = 28.3 points. It seems the scaling is due to difference in coordinates. Please convert size into points as per above measurements, hopefully it will resolve the issue. If issue persist then please share your source PDF document and image, so we will try the scenario at our end and will provide you more information accordingly.
We are sorry for the inconvenience caused.
Best Regards,
Hello Tilal,
This explains a lot. How do I go about converting sizes of pixels into points? What do I divide the pixels by? Is there a helper method in Aspose somewhere to do this conversion?
Thank you,
Daniel
Hi Daniel,
Thanks for your feedback. The conversion from point to pixel depends on an image’s DPI (dots per inch) property. For example, if an image’s DPI is 96 (96 pixels for each inch), and it is 100 points high, its height in pixels is (100 / 72) * 96 = 133.3. The general formula is: pixels = ( points / 72 ) * DPI. Hopefully it will help you to convert pixel to point.
Best Regards,