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