We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Images in PDFs

My application stamps PDF’s on each page with details of the PDF. Most of the PDF’s are scanned documents. Some of them were scanned portrait, and others landscape. To work out where to position the stamp, I check the page height and page width. The problem is that on the pages that are landscape, the box is being put outside the image. This is rectified if I just change the Width and Height around manually.

To check whether the page is landscape, I check the Pages(x).PageInfo.IsLandscape, but this is always false, even if it is in landscape.

Another check I have been doing is the Height and Width of the mediabox. Sometimes this is set correctly. But there are then times where it isn’t.

I figure that the page height and width has nothing to do with the size and width of the image for that page, even if the image is bigger than the page. So how do I find out what the size of the image is within the page?

@runrobert

You can get resolution and dimension of the embedded images in a PDF using Aspose.PDF. In case you still experience any issue, please share a sample PDF document with us so that we can test the scenario in our environment and address it accordingly.

Thanks for your reply. I now have something that is working. Here is the code that I am using to extract the image, insert my image into this image, and save it back to the PDF:
Dim abs As New ImagePlacementAbsorber
pdfDoc.Pages(x).Accept(abs)
Dim pdfImage As XImage
Dim ImageStream As New IO.MemoryStream
pdfImage = abs.ImagePlacements(1).Image
pdfImage.Save(ImageStream, Imaging.ImageFormat.Jpeg)

Dim imageBitmap As New Bitmap(ImageStream)
Dim imageGraphic As Graphics = Graphics.FromImage(imageBitmap)

Dim ImageRect As New RectangleF(pdfImage.Width / 2, 0, pdfImage.Width / 2, pdfImage.Height / 32)
Dim InsertRect As New RectangleF(0, 0, ImageWidth, ImageHeight)
imageGraphic.DrawImage(newBitmap, ImageRect, InsertRect, GraphicsUnit.Pixel)
Dim NewStream As New IO.MemoryStream
imageBitmap.Save(NewStream, Imaging.ImageFormat.Jpeg)
pdfDoc.Pages(x).Resources.Images.Replace(1, NewStream)

The problem now is that the files are increasing in size. The first file went from 22K to 1.2Megs. So something is wrong with the way that the image is being saved back to the PDF that is making it too big. Any ideas?

@runrobert

Adding image to PDF resources will result increase in file size. You can optimize PDF file size after saving and reloading it for optimization. In case you still face any issue, please share your sample input files with us so that we can test the scenario in our environment and address it accordingly.

Thanks for your help on this, except it hasn’t sorted out the problem. Trying to do it the above way just takes way too long, so it is not an acceptable solution.

Please note that I started off with not being able to tell whether a page is landscape or portrait. You have a function to tell this - Pages(x).PageInfo.IsLandscape. This doesn’t work, pure and simple. So if you can get this working, I will be able to do the program with Aspose. At the moment I have gone back to GDPicture for this solution.

@runrobert

Would you kindly share the sample PDF document with us. We will test the scenario in our environment and address it accordingly.