Adding image, page corrupt?

I basically used the code from https://github.com/aspose-pdf/Aspose.PDF-for-.NET/blob/master/Examples/CSharp/AsposePDF/Images/AddImage.cs to add an image to a pdf file with little variations.

It works on some PDFs but on others (example attached here) I get an error in Acrobat when I open the result-file: “An error on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem”.

Original: test.pdf (265.6 KB)
Result: test_result.pdf (273.8 KB)

The original pdf-file was created in Microsoft Word, so I think this is an error in Aspose as I don’t know what I possibly could have done wrong.

@HARIAT
I added Aspose.Pdf 23.8 library for the .Net 6 project and the resulting file opens for me.
I am attaching the code and the drawing file used for verification / reproduction, as well as the result.

// ExStart:AddImage
string dataDir = myDir;

// Open document
Document pdfDocument = new Document(dataDir + "test.pdf");

// Set coordinates
int lowerLeftX = 100;
int lowerLeftY = 100;
int upperRightX = 200;
int upperRightY = 200;

// Get the page where image needs to be added
Page page = pdfDocument.Pages[1];
// Load image into stream
FileStream imageStream = new FileStream(dataDir + "aspose-logo.jpg", FileMode.Open);
// Add image to Images collection of Page Resources
page.Resources.Images.Add(imageStream);
// Using GSave operator: this operator saves current graphics state
page.Contents.Add(new Aspose.Pdf.Operators.GSave());
// Create Rectangle and Matrix objects
Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
Matrix matrix = new 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 Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
XImage ximage = page.Resources.Images[page.Resources.Images.Count];
// Using Do operator: this operator draws image
page.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));
// Using GRestore operator: this operator restores graphics state
page.Contents.Add(new Aspose.Pdf.Operators.GRestore());
dataDir = dataDir + "AddImage_out.pdf";
// Save updated document
pdfDocument.Save(dataDir);
// ExEnd:AddImage
Console.WriteLine("\nImage added successfully.\nFile saved at " + dataDir);

AddImage_out.pdf (280.0 KB)
aspose-logo.jpg (7.1 KB)

Does the resulting file open for you?

Hello Sergei,

somewhere in the process I must have deleted the line

page.Contents.Add(new Aspose.Pdf.Operators.GSave());

With that back in it works fine.
Thank you for you help!

@HARIAT
was happy to help