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 http://www.aspose.com/docs/display/pdfnet/Convert+PDF+pages+to+JPEG+images, 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 http://www.aspose.com/docs/display/pdfnet/Add+Image+in+existing+PDF+file 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\\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
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 http://www.aspose.com/docs/display/pdfnet/Convert+PDF+pages+to+JPEG+images, 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 http://www.aspose.com/docs/display/pdfnet/Add+Image+in+existing+PDF+file 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\
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