With previous versions of Aspose.Pdf, we used this code to add a logo image to each page in a PDF:
string sImageUrl = "decisionheaderlogo.png";
if (bAddHeader && !string.IsNullOrEmpty(sImageUrl) && File.Exists(sImageUrl))
{
Document pdfDoc = new Document(sTargetFileFullPath);
ImageStamp imageStamp = new ImageStamp(sImageUrl);
imageStamp.Height = 50;
imageStamp.Width = 100;
//set properties of the stamp
imageStamp.TopMargin = 10;
imageStamp.RightMargin = 10;
imageStamp.HorizontalAlignment = HorizontalAlignment.Right;
imageStamp.VerticalAlignment = VerticalAlignment.Top;
//add header on all pages
foreach (Page opage in pdfDoc.Pages)
opage.AddStamp(imageStamp);
//save updated document
pdfDoc.Save(sTargetFileFullPath);
}
In Aspose.Pdf version 17.x (I tried 17.6 and 17.7) this no longer works. The image stamp does not appear in the output PDF and an exception is seen in the debugger each time AddStamp is called:
Exception thrown: 'System.Runtime.InteropServices.ExternalException' in System.Drawing.dll
After some hours of experimentation, I found that the ImageStamp feature still works if the image is first converted to bmp:
using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(sImageUrl))
using (MemoryStream ms = new MemoryStream())
{
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
Document pdfDoc = new Document(sTargetFileFullPath);
ImageStamp imageStamp = new ImageStamp(ms);
imageStamp.Background = false;
imageStamp.Height = 50;
imageStamp.Width = 100;
//set properties of the stamp
imageStamp.TopMargin = 10;
imageStamp.RightMargin = 10;
imageStamp.HorizontalAlignment = HorizontalAlignment.Right;
imageStamp.VerticalAlignment = VerticalAlignment.Top;
//add header on all pages
foreach (Page opage in pdfDoc.Pages)
opage.AddStamp(imageStamp);
//save updated document
pdfDoc.Save(sTargetFileFullPath);
}
Although this is a good workaround, I see the issue as a new bug in Aspose.Pdf 17.x and I hope it will be fixed in a future version.
Note that the input file is a PDF document generated by Aspose.Words in PDF/A-1a format (our customers generally insist on PDF/A compliance). A colleague found that the problem with the png image stamp does not occur if we do not use PDF/A but the default Pdf15 compliance. When the error occurs, the PDF/A compliance is also lost in the output PDF file.