I am trying to add the same image to multiple pages and I am having a problem. I followed the code that allows me to add a single image to a single page and it works. I followed the reference " Add Image to Existing PDF File" on this link here.
The problem I am having is that I have a single image that I will need to add on multiple pages and it will not work. I’ve tried reading the section, " Add Reference of a single Image multiple times in a PDF Document" but this makes no sense to me.
Is there any more coding examples that would show how to add a single image to multiple pages?
@ryan.coleman
Thank you for contacting support.
You may use below code snippet to add reference of single image to all pages of a PDF document, with XImageCollection.Add(XImage)
method.
using (Aspose.Pdf.Document document = new Document(dataDir + "lorem.input.pdf"))
{
using (var imageStream = File.Open(dataDir + "Test.PNG", FileMode.Open))
{
XImage image = null;
foreach (Page page in document.Pages)
{
Aspose.Pdf.Rectangle imageRectangle = page.CropBox;
WatermarkAnnotation annotation = new WatermarkAnnotation(page, page.Rect);
string name;
if (image == null)
{
name = page.Resources.Images.Add(imageStream);
image = page.Resources.Images[name];
}
else
{
name = page.Resources.Images.Add(image);
}
page.Contents.Add(new Operators.GSave());
page.Contents.Add(new Operators.ConcatenateMatrix(new Aspose.Pdf.Matrix(imageRectangle.Width, 0, 0, imageRectangle.Height, 0, 0)));
page.Contents.Add(new Operators.Do(name));
page.Contents.Add(new Operators.GRestore());
page.Annotations.Add(annotation);
}
}
document.Save(dataDir + "output_19.5.pdf");
}
We hope this will be helpful. Please feel free to contact us if you need any further assistance.
While using this coding I am getting the error below when trying to save the document. It doesn’t matter if I am saving it to a stream or to a pdf file.
“Number of placed objects is not correct”
Any idea on how to fix this?
Here is the coding for the page portion that I’m using.
I am pulling the page number from another variable and then loading that into the Page object. At the end, I am doing all of the Contents.Add operators.
Page page = existingPDFDoc.Pages[signature.PageNumber];
string name;
if (image == null) {
name = page.Resources.Images.Add(imageStream);
image = page.Resources.Images[name];
} else {
name = page.Resources.Images.Add(image);
}
signature.AdjustedRectangle = new Rectangle(
(signature.BaseRectangle.LLX),
(signature.BaseRectangle.LLY - 50),
(signature.BaseRectangle.URX + 75),
(signature.BaseRectangle.URY));
page.Contents.Add(new Aspose.Pdf.Operators.GSave());
Matrix matrix = new Matrix(new double[] { signature.AdjustedRectangle.URX -
signature.AdjustedRectangle.LLX, 0, 0, signature.AdjustedRectangle.URY -
signature.AdjustedRectangle.LLY, signature.AdjustedRectangle.LLX,
signature.AdjustedRectangle.LLY});
page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
page.Contents.Add(new Aspose.Pdf.Operators.Do(name));
page.Contents.Add(new Aspose.Pdf.Operators.GRestore());
Also, it is worth mentioning that this works when I am adding a single image to a single page. When I start adding to multiple pages, it produces that error.
@ryan.coleman
Would you please share your source files including the image and PDF document while mentioning the page numbers where you are trying to add the image, so that we may investigate accordingly and help you out.
I’ve shared the pdf file that I’m using and a png file that can be used for the image.
RyanColeman.png (31.4 KB)
Notary-Borrower.pdf (276.3 KB)
foreach (var pdfDoc in pdfDocs) {
using (Document existingPDFDoc = pdfDoc.Document) {
using (MemoryStream imageStream = new MemoryStream(imageBytearray)) {
XImage image = null;
foreach (var signature in pdfDoc.InnerPDFDoc.SignatureIfno) {
if (signature.EmbeddedText == "@@NOTSEAL@@") {
Page page = existingPDFDoc.Pages[signature.PageNumber];
string name;
if (image == null) {
name = page.Resources.Images.Add(imageStream);
image = page.Resources.Images[name];
} else {
name = page.Resources.Images.Add(image);
}
signature.AdjustedRectangle = new Rectangle(
(signature.BaseRectangle.LLX),
(signature.BaseRectangle.LLY - 50),
(signature.BaseRectangle.URX + 75),
(signature.BaseRectangle.URY));
page.Contents.Add(new Aspose.Pdf.Operators.GSave());
Matrix matrix = new Matrix(new double[] { signature.AdjustedRectangle.URX - signature.AdjustedRectangle.LLX, 0, 0, signature.AdjustedRectangle.URY - signature.AdjustedRectangle.LLY, signature.AdjustedRectangle.LLX, signature.AdjustedRectangle.LLY });
page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
page.Contents.Add(new Aspose.Pdf.Operators.Do(name));
page.Contents.Add(new Aspose.Pdf.Operators.GRestore());
}
}
}
//existingPDFDoc.OptimizeResources();
pdfDoc.DocumentStream.Position = 0;
existingPDFDoc.Save(pdfDoc.DocumentStream);
//existingPDFDoc.Save(@"C:\TestFiles\TestNotaryPDF.pdf", SaveFormat.Pdf);
}
Do not worry about the Foreach line or the signature.EmbeddedText if statement as they are just helping me find what I’m looking for in my list of PDF Documents. You can see that I am Setting the Page object to whatever page I’m finding my tag on and doing the work there.
In the document that I am sharing the images are being placed on each page starting from page 2.
Also, I am only getting this error when I try to save the file. It doesn’t error out when adding any of the operators to the page.Contents section.
@ryan.coleman
Thank you for sharing requested data.
We are looking into details and will share our findings with you soon.