We are trying to add the same image on each page of a PDF document. We managed to do that, however the performance is very bad for huge files (200+ pages). The problem is that, even though we store a local reference to the image, the image get reprocessed when sent to Aspose library.
Please see following code snippet, it adds the image on the first page and on remaining pages, it adds the reference of the images instead of adding it for each page. It will improve the performance.
C#
Aspose.Pdf.Rectangle imageRectangle = new Aspose.Pdf.Rectangle(0, 0, 323, 156); using (Document document = new Document(dataDir + “16S_002415-000ACCT_V1.pdf”)) { using (var imageStream = File.Open(dataDir + “aspose-logo.png”, FileMode.Open))
{
XImage image = null; foreach (Page page in document.Pages)
{
Aspose.Pdf.Annotations.WatermarkAnnotation annotation = new Aspose.Pdf.Annotations.WatermarkAnnotation(page, imageRectangle);
XForm form = annotation.Appearance[“N”];
form.BBox = imageRectangle; string name; if (image == null)
{ //at first page we add new image name = form.Resources.Images.Add(imageStream);
image = form.Resources.Images[name];
} else
{ //at second and next page we add reference to the existing XImage
name = form.Resources.Images.Add(image);
}
Thank you very much for the code snippet. It helps a lot. However I forgot to mention we are using an older version (11.4) and the class hierarchy seems to be different and I can’t find the WatermarkAnnotation.
We currently find it risky to upgrade to your latest, so since then is it possible to find somewhere the older version class hierarchy or is the result going to be the same if we use a simple Annotation class?
The scenario has been tested with recent release of Aspose.Pdf for .NET 17.3.0 complete class reference is Aspose.Pdf.Annotations.WatermarkAnnotation and we always encourage our customers to try using latest release of API and I am afraid the WatermarkAnnotation class may not be available in older release version.
We decided to upgrade to latest Aspose and the above code works wonderfully and the performance is way better. Thank you for your help.
However we need a little more help in understanding how the image gets draw onto the page.
We have the following requirements:
We have to draw our image somewhere close to the upper-left corner of the page [the origine (0,0) is the lower-left corner of the page].
The PDF page format is a mix between landscape and portrait with different heights and widths
We’ve tried using the page.PageInfo.Height in order to calculate the position, but we get the same value for each page. We would like to know which variable provides the correct layout information or a different way to achieve the above requirements.
Thanks for your feedback. It is good to know that suggested code worked for you.
We have to draw our image somewhere close to the upper-left corner of the page [the origine (0,0) is the lower-left corner of the page]
Please use following sample code for adding images to your specified position.
C#
// Open document Document pdfDocument = new Document(dataDir+ "AddImage.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 Operator.GSave()); // Create Rectangle and Matrix objects Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY); Matrix matrix = new Matrix(newdouble[] { 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 Operator.ConcatenateMatrix(matrix)); XImage ximage = page.Resources.Images[page.Resources.Images.Count]; // Using Do operator: this operator draws image page.Contents.Add(new Operator.Do(ximage.Name)); // Using GRestore operator: this operator restores graphics state page.Contents.Add(new Operator.GRestore()); dataDir = dataDir + "AddImage_out.pdf"; // Save updated document pdfDocument.Save(dataDir);
The PDF page format is a mix between landscape and portrait with different heights and width
As per my understanding, you want to determine whether the page is landscape or portrait. Please use following sample code to determine whether the PDF page is landscape or not.
C#
Document doc = new Document(dataDir + "mypdf.pdf"); // Get a specific page Page pdfPage = doc.Pages[1]; Console.WriteLine(pdfPage.PageInfo.Width); Console.WriteLine(pdfPage.PageInfo.Height); Console.WriteLine(pdfPage.PageInfo.IsLandscape);
If you still face any issue, or need further assistance please feel free to contact us.
Thanks for your feedback. I am glad to know that suggested code worked for you.
In the Preview (version 8.1 (877.7)) the viewer removes the image
I have tested the scenario and have managed to reproduce the problem that in the Preview app images are not visible for the generated PDF file. For the sake of correction, I have logged a ticket PDFNET-42545 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of its resolution within this forum thread. Please be patient and spare us little time.
In Firefox default the image is removed
I have tested the scenario and have not noticed any issue in FireFox 52.0.2 (64-bit). Please share your sample file and let us know the FireFox version you are checking.
Thanks for your feedback. I am glad your issue has been resolved. Please keep using our API and feel free to contact us for any question or concern, we will be more than happy to extend our support.