I’m having trouble using the Pages(n).Resources.Images.Replace method. I load the attached original.pdf and loop through all the pages and all the images on each page (this file only contains 1 page with 1 image), replacing the images with new downscaled jpeg images. The resulting pdf (modified.pdf) of this operation is slightly bigger than original.pdf - it seems as if both images are saved in modified.pdf although only the resized jpeg image is shown in the pdf. Do you have some bug in the Images.Replace method such that the image being replaced do not get deleted? I’m using version 6.4.0.0 of Aspose.pdf.
Public Shared Function ResizeImage(ByVal image As System.Drawing.Image, ByVal width As Integer, ByVal height As Integer) As System.Drawing.Bitmap
'a holder for the result
Dim result As New System.Drawing.Bitmap(width, height)<span style="color:green;">'use a graphics object to draw the resized image into the bitmap</span> <span style="color:blue;">Using</span> graphics__1 <span style="color:blue;">As</span> System.Drawing.<span style="color:#2b91af;">Graphics</span> = System.Drawing.<span style="color:#2b91af;">Graphics</span>.FromImage(result) <span style="color:green;">'set the resize quality modes to high quality</span> graphics__1.CompositingQuality = System.Drawing.Drawing2D.<span style="color:#2b91af;">CompositingQuality</span>.HighQuality graphics__1.InterpolationMode = System.Drawing.Drawing2D.<span style="color:#2b91af;">InterpolationMode</span>.HighQualityBicubic graphics__1.SmoothingMode = System.Drawing.Drawing2D.<span style="color:#2b91af;">SmoothingMode</span>.HighQuality <span style="color:green;">'draw the image into the target bitmap</span> graphics__1.DrawImage(image, 0, 0, result.Width, result.Height) <span style="color:blue;">End</span> <span style="color:blue;">Using</span> <span style="color:green;">'return the resulting bitmap</span> <span style="color:blue;">Return</span> result <span style="color:blue;">End</span> <span style="color:blue;">Function</span> <span style="color:blue;">Public</span> <span style="color:blue;">Shared</span> <span style="color:blue;">Function</span> ConvertPDFToPDF(<span style="color:blue;">ByRef</span> inputStream <span style="color:blue;">As</span> <span style="color:#2b91af;">Stream</span>) <span style="color:blue;">As</span> <span style="color:#2b91af;">MemoryStream</span> <span style="color:blue;">Dim</span> pdfDocument <span style="color:blue;">As</span> <span style="color:blue;">New</span> Aspose.Pdf.<span style="color:#2b91af;">Document</span>(inputStream) <span style="color:blue;">For</span> <span style="color:blue;">Each</span> page <span style="color:blue;">As</span> Aspose.Pdf.<span style="color:#2b91af;">Page</span> <span style="color:blue;">In</span> pdfDocument.Pages <span style="color:blue;">Dim</span> idx <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = 1 <span style="color:blue;">For</span> <span style="color:blue;">Each</span> image <span style="color:blue;">As</span> Aspose.Pdf.<span style="color:#2b91af;">XImage</span> <span style="color:blue;">In</span> page.Resources.Images <span style="color:blue;">Using</span> imageStream <span style="color:blue;">As</span> <span style="color:blue;">New</span> <span style="color:#2b91af;">MemoryStream</span>() image.Save(imageStream) imageStream.Seek(0, <span style="color:#2b91af;">SeekOrigin</span>.Begin) <span style="color:blue;">Dim</span> jpegImage <span style="color:blue;">As</span> System.Drawing.<span style="color:#2b91af;">Bitmap</span> = <span style="color:blue;">CType</span>(System.Drawing.<span style="color:#2b91af;">Bitmap</span>.FromStream(imageStream), System.Drawing.<span style="color:#2b91af;">Bitmap</span>) <span style="color:blue;">Using</span> outputImageStream <span style="color:blue;">As</span> <span style="color:blue;">New</span> <span style="color:#2b91af;">MemoryStream</span>() jpegImage = ResizeImage(jpegImage, 800, 1100) jpegImage.Save(outputImageStream, <span style="color:#2b91af;">ImageFormat</span>.Jpeg) jpegImage.Save(<span style="color:#a31515;">"c:\temp\jpegImage.jpg"</span>, <span style="color:#2b91af;">ImageFormat</span>.Jpeg) page.Resources.Images.Replace(idx, outputImageStream) idx = idx + 1 jpegImage.Dispose() <span style="color:blue;">End</span> <span style="color:blue;">Using</span> <span style="color:blue;">End</span> <span style="color:blue;">Using</span> <span style="color:blue;">Next</span> <span style="color:blue;">Next</span> <span style="color:blue;">Dim</span> outputStream <span style="color:blue;">As</span> <span style="color:blue;">New</span> <span style="color:#2b91af;">MemoryStream</span>() pdfDocument.Save(outputStream) <span style="color:blue;">Return</span> outputStream <span style="color:blue;">End</span> <span style="color:blue;">Function</span></pre></div>