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.