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

Free Support Forum - aspose.com

Pages(n).Resources.Images.Replace doesn't delete the image being replaced

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>

I’ve also tested this with version 6.4.2.0 - still the same result.

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for sharing the sample code and template file.

I have observed a slight change in the file size from 15.3 MB to 15.4 MB (around 104KB) using the latest version of Aspose.Pdf for .NET. However, the image is replicated and only latest image is present in the file. You may check that by using the following line after replacing the image.

MessageBox.Show(page.Resources.Images.Count)

For size change, I have created an issue in our issue tracking system for our development team to further investigate this issue. You will be notified via this forum thread regarding any update against your issue. The issue id is PDFNEWNET-32526.

Thank You & Best Regards,

Anything new regarding the filesize problem? It would be really nice to be able to compress the images in a pdf to make it smaller.

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for being patient.

Our development team is looking into your issue and will share their feedback soon. I have asked the development team the details and ETA regarding this issue. I will update you as soon as I get a feedback from their side.

Thank You & Best Regards,

Hello
Bjarke,


Thanks for your patience. I am pleased to share that the issue reported earlier has been fixed and its hotfix will be included in our upcoming release version of Aspose.Pdf for .NET 6.6.0 which is expected to be released in few days. Please be patient and spare us little time. Your patience and comprehension is greatly appreciated in this regard.

The issues you have found earlier (filed as PDFNEWNET-32526) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.