Images on layers in multi-page document Aspose.PDF.Net

When modifying a multipage document, on each page I’d like to add an image and assign it to a layer. This works fine on a single page document and the last page of a multi-page document, but the images get written right to the page for other pages. What am I doing wrong? Image handling works fine below. TIA - Dave

            float dRotation = -270.0F;

            string sTempFileName2 = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".pdf");

            using (Document doc = new Document(sFileToProcess))
            {
                System.Drawing.Bitmap unrotatedBmp = new System.Drawing.Bitmap(QRCodeImageFunctions.GetQRCodeImage(sURLToEncode, (int)fWidth));

                foreach (Page page in doc.Pages)
                {
                    Bitmap bmp = RotateBitmap(unrotatedBmp, dRotation);

                    Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(fX, fY, fX + bmp.Width, fY + bmp.Height);

                    var imageStream = new MemoryStream();
                    bmp.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png);

                    imageStream.Position = 0;

                    page.Resources.Images.Add(imageStream);

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

                    page.Contents.Add(new Operator.GSave());

                    string sLayerId = GetARandomString(3, LOWER_CASE);

                    Layer layer = new Layer(sLayerId, sLayerId);

                    Matrix matrix = new Matrix(new double[] { Math.Abs(rectangle.URX - rectangle.LLX), 0, 0, Math.Abs(rectangle.URY - rectangle.LLY), rectangle.LLX, rectangle.LLY });

                    layer.Contents.Add(new Operator.GSave());

                    layer.Contents.Add(new Operator.ConcatenateMatrix(matrix));

                    layer.Contents.Add(new Operator.Do(ximage.Name));

                    layer.Contents.Add(new Operator.GRestore());

                    page.Layers = new List<Layer>();
                    page.Layers.Add(layer);

                    page.Contents.Add(new Operator.GRestore());
                } // foreach (Page page in doc.Pages)

                doc.Save(sTempFileName2, SaveFormat.Pdf);
            } // using doc

@DaveBrumbaugh

Thank you for contacting support.

We would like to request you to share a narrowed down sample application reproducing this issue because shared code snippet includes some undefined methods and variables. Please also share sample documents that work fine, as well as the documents which reproduce this problem. So that we may investigate further to help you out. Before sharing requested data, please ensure using Aspose.PDF for .NET 18.3 in your environment.

Thanks, Farhan. I suspect I am doing something wrong here. Image is applied to all pages, but is not included in layer except for last page. I think the layers I’m adding are actually empty. I did upgrade to 18.3. Sample code below will download demo files.

    public static void LayerDemo()
    {
        string sFileToProcess = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".pdf");
        string sQRCodeFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".png");

        // Get demo files
        using (System.Net.WebClient webClient = new System.Net.WebClient())
        {
            webClient.DownloadFile("https://pwstorageazure.blob.core.windows.net/public/Binder3.pdf", sFileToProcess);
            webClient.DownloadFile("https://pwstorageazure.blob.core.windows.net/public/QRCode.png", sQRCodeFile);
        }

        float fX = 50f;
        float fY = 50f;

        try
        {
            Aspose.Pdf.License license = new Aspose.Pdf.License();
            System.Reflection.Assembly thisExe = System.Reflection.Assembly.GetExecutingAssembly();

            try
            {
                using (var resourceStream = thisExe.GetManifestResourceStream("AsposePDFConsole.Aspose.Pdf.lic"))
                {
                    license.SetLicense(resourceStream);
                    license.Embedded = true;
                }
            }
            catch
            {
                Console.WriteLine("Error loading Aspose.PDF license");
                return;
            }

            string sOutputFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".pdf");

            using (Document doc = new Document(sFileToProcess))
            {
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(sQRCodeFile);

                int iPageIndex = 1;

                foreach (Page page in doc.Pages)
                {
                    Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(fX, fY, fX + bmp.Width, fY + bmp.Height);

                    var imageStream = new MemoryStream();
                    bmp.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png);

                    imageStream.Position = 0;

                    page.Resources.Images.Add(imageStream);

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

                    page.Contents.Add(new Operator.GSave());

                    Layer layer = new Layer(iPageIndex.ToString(), "Layer" + iPageIndex.ToString());

                    Matrix matrix = new Matrix(new double[] { Math.Abs(rectangle.URX - rectangle.LLX), 0, 0, Math.Abs(rectangle.URY - rectangle.LLY), rectangle.LLX, rectangle.LLY });

                    layer.Contents.Add(new Operator.GSave());

                    layer.Contents.Add(new Operator.ConcatenateMatrix(matrix));

                    layer.Contents.Add(new Operator.Do(ximage.Name));

                    layer.Contents.Add(new Operator.GRestore());

                    page.Layers = new List<Layer>();
                    page.Layers.Add(layer);

                    page.Contents.Add(new Operator.GRestore());

                    iPageIndex++;
                } // foreach (Page page in doc.Pages)

                doc.Save(sOutputFileName, SaveFormat.Pdf);

                Console.WriteLine("Wrote '{0}'", sOutputFileName);
                System.Diagnostics.Debug.WriteLine(string.Format("Wrote '{0}'", sOutputFileName));
            } // using doc
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: {0}\n{1}", ex.Message, ex.StackTrace);
        }
    }

@DaveBrumbaugh

We have worked with the data shared by you and have been able to reproduce the issue in our environment. A ticket with ID PDFNET-44415 has been logged in our issue management system for further investigation and resolution. The issue ID has been linked with this thread so that you will receive notification as soon as the issue is resolved.

We are sorry for the inconvenience.

I have same problem when adding layers to each page.

As a workarround I tried to save the file after adding a layer to each page and reopen it (adding layer, save, open). Crazy, since the newly added layer was not available after loading the file (it was available when the document was saved).

@HaraldGeisselhart72

Thank you for contacting support.

Would you please try using Aspose.PDF for .NET 19.1 in your environment. In case the issue persists, then share respective source and generated files along with the workaround so that we may investigate accordingly.