Invalid index: index should be in the range [1..n] where n equals to the operators count

Hi,

I am adding Image to a PDF layer in 2 different functionality for same PDF file but it throws Exception “Invalid index: index should be in the range [1…n] where n equals to the operators count”

Here is the sample code

public void CreateContent()
        {
            try
            {
                var stream = File.OpenRead("AsposeTotalNET.lic");
                var imagestream = File.OpenRead("img.png");

                Aspose.Pdf.License pdfLicense = new();
                pdfLicense.SetLicense(stream);


                Document document = new Document("sample.pdf");
                Page page = document.Pages[1];
                if (page.Layers == null || !page.Layers.Any())
                {
                    page.Layers = new List<Layer>();
                    //Issue 1 if the Layers list is not null still we need to create a new list else no image will render in page which is added to layer
                }

                page.Resources.Images.Add(imagestream);
                XImage ximage = page.Resources.Images[page.Resources.Images.Count];
                Layer imageLayer = page.Layers.FirstOrDefault(x => x.Name == "ImageLayer");
                if (imageLayer == null)
                {
                    imageLayer = new Layer(Guid.NewGuid().ToString(), "ImageLayer");
                    page.Layers.Add(imageLayer);
                }

                //Aspose Y starts from bottom to top. need to substract the height since to match aspose and spire. Aspose draws image upward where as spire draws downwords
                Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(100, 100, 100 + ximage.Width, 100 + ximage.Height);
                Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });
                imageLayer.Contents.Add(new Aspose.Pdf.Operators.GSave());
                imageLayer.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
                imageLayer.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));
                imageLayer.Contents.Add(new Aspose.Pdf.Operators.GRestore());
                page.Contents.Add(new Aspose.Pdf.Operators.GRestore());


                MemoryStream outStream = new MemoryStream();
                document.Save(outStream, SaveFormat.Pdf);
                outStream.Position = 0;
                //End of functionality 1



                document = new Document(outStream);
                page = document.Pages[2];
                if (page.Layers == null || !page.Layers.Any())
                {
                    page.Layers = new List<Layer>();
                }
                page.Resources.Images.Add(imagestream);
                ximage = page.Resources.Images[page.Resources.Images.Count];
                imageLayer = page.Layers.FirstOrDefault(x => x.Name == "ImageLayer");
                if (imageLayer == null)
                {
                    imageLayer = new Layer(Guid.NewGuid().ToString(), "ImageLayer");
                    page.Layers.Add(imageLayer);
                }

                //Aspose Y starts from bottom to top. need to substract the height since to match aspose and spire. Aspose draws image upward where as spire draws downwords
                rectangle = new Aspose.Pdf.Rectangle(100, 100, 100 + ximage.Width, 100 + ximage.Height);
                matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });
                imageLayer.Contents.Add(new Aspose.Pdf.Operators.GSave());
                imageLayer.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
                imageLayer.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));
                imageLayer.Contents.Add(new Aspose.Pdf.Operators.GRestore());
                page.Contents.Add(new Aspose.Pdf.Operators.GRestore());


                outStream = new MemoryStream();
                document.Save(outStream, SaveFormat.Pdf);
                //End of functionality 2nd
            }catch(Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
}

Any help much appreciated.

@cpaperless

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-56022

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

The issues you have found earlier (filed as PDFNET-56022) have been fixed in Aspose.PDF for .NET 23.12.

I would like to reopen this issue as it is not a right fix and I am easily able to reproduce with below code. (Consider using pdf with multiple pages)

public void CreateContent()
 {
     try
     {
         var stream = File.OpenRead("AsposeTotalNET.lic");
         var imagestream = File.OpenRead("img.png");

         Aspose.Pdf.License pdfLicense = new();
         pdfLicense.SetLicense(stream);


         Document document = new Document("sample.pdf");
         foreach (Page page in document.Pages)
         {
             if (page.Layers == null || !page.Layers.Any())
             {
                 page.Layers = new List<Layer>();
                 //Issue 1 if the Layers list is not null still we need to create a new list else no image will render in page which is added to layer
             }

             page.Resources.Images.Add(imagestream);
             XImage ximage = page.Resources.Images[page.Resources.Images.Count];
             Layer imageLayer = page.Layers.FirstOrDefault(x => x.Name == "ImageLayer");
             if (imageLayer == null)
             {
                 imageLayer = new Layer(Guid.NewGuid().ToString(), "ImageLayer");
                 page.Layers.Add(imageLayer);
             }

             //Aspose Y starts from bottom to top. need to substract the height since to match aspose and spire. Aspose draws image upward where as spire draws downwords
             Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(100, 100, 100 + ximage.Width, 100 + ximage.Height);
             Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });
             imageLayer.Contents.Add(new Aspose.Pdf.Operators.GSave());
             imageLayer.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
             imageLayer.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));
             imageLayer.Contents.Add(new Aspose.Pdf.Operators.GRestore());
             page.Contents.Add(new Aspose.Pdf.Operators.GRestore());
         }

         MemoryStream outStream = new MemoryStream();
         document.Save(outStream, SaveFormat.Pdf);
         outStream.Position = 0;
         //End of functionality 1



         document = new Document(outStream);
         foreach (Page page in document.Pages)
         {
             if (page.Layers == null || !page.Layers.Any())
             {
                 page.Layers = new List<Layer>();
             }
             page.Resources.Images.Add(imagestream);
             XImage ximage = page.Resources.Images[page.Resources.Images.Count];
             Layer imageLayer = page.Layers.FirstOrDefault(x => x.Name == "ImageLayer");
             if (imageLayer == null)
             {
                 imageLayer = new Layer(Guid.NewGuid().ToString(), "ImageLayer");
                 page.Layers.Add(imageLayer);
             }

             //Aspose Y starts from bottom to top. need to substract the height since to match aspose and spire. Aspose draws image upward where as spire draws downwords
             Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(200, 200, 200 + ximage.Width, 200 + ximage.Height);
             Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });
             imageLayer.Contents.Add(new Aspose.Pdf.Operators.GSave());
             imageLayer.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
             imageLayer.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));
             imageLayer.Contents.Add(new Aspose.Pdf.Operators.GRestore());
             page.Contents.Add(new Aspose.Pdf.Operators.GRestore());

         }
         outStream = new MemoryStream();
         document.Save(outStream, SaveFormat.Pdf);
         //End of functionality 2nd
     }catch(Exception ex)
     {
         System.Diagnostics.Debug.WriteLine(ex.Message);
     }

 }

@cpaperless

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-56555

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

The issues you have found earlier (filed as PDFNET-56555) have been fixed in Aspose.PDF for .NET 24.3.