Free Support Forum - aspose.com

Replace Images destroys pdf if the source pdf is optimized

Hi,


I’ve got a strange behaviour if you try something like this:

  public void Convert(string srcDoc, string destDoc)
{
Document doc = new Document(srcDoc);
        <span style="color:blue;">byte</span>[] outImg;
        <span style="color:#2b91af;">MemoryStream</span> outStrm;
        <span style="color:#2b91af;">MemoryStream</span> imgStream;
        <span style="color:#2b91af;">Page</span> pg;

        <span style="color:green;">//doc.OptimizeResources();</span>

        <span style="color:blue;">for</span> (<span style="color:blue;">int</span> p = 1; p <= doc.Pages.Count; p++)
        {
            pg = doc.Pages[p];

            <span style="color:blue;">for</span> (<span style="color:blue;">int</span> i = 1; i <= pg.Resources.Images.Count; i++)
            {

                <span style="color:#2b91af;">XImage</span> img = pg.Resources.Images[i];

                <span style="color:blue;">using</span> (imgStream = <span style="color:blue;">new</span> <span style="color:#2b91af;">MemoryStream</span>())
                {
                    img.Save(imgStream, <span style="color:#2b91af;">ImageFormat</span>.Jpeg);
                    imgStream.Position = 0;
                    outImg = imgStream.ToArray();

                    <span style="color:green;">//outImg = SetJPEGCompression(imgStream.ToArray(), 40);</span>

                    <span style="color:blue;">using</span> (outStrm = <span style="color:blue;">new</span> <span style="color:#2b91af;">MemoryStream</span>(outImg))
                    {
                        pg.Resources.Images.Replace(i, outStrm);
                        outStrm.Flush();
                        outStrm.Close();
                    }
                }
            }
        }

        <span style="color:green;">//doc.OptimizeResources();</span>

        doc.Pages.Flatten();

        doc.Save(destDoc);
    }</pre><pre style="font-family: Consolas; font-size: 13px; background-color: white; background-position: initial initial; background-repeat: initial initial; ">In the attached PDF are images in the header and the footer. </pre><pre style="font-family: Consolas; font-size: 13px; background-color: white; background-position: initial initial; background-repeat: initial initial; ">If I do the replace of the image, the first page is okay, but on the other pages the header and footer is missing and an error is thrown by acrobot reader.</pre><pre style="font-family: Consolas; font-size: 13px; background-color: white; background-position: initial initial; background-repeat: initial initial; ">The other images are okay.</pre><pre style="font-family: Consolas; font-size: 13px; background-color: white; background-position: initial initial; background-repeat: initial initial; "><br></pre><pre style="font-family: Consolas; font-size: 13px; background-color: white; background-position: initial initial; background-repeat: initial initial; ">The second problem is if you do the "<span style="color: green; background-color: white; ">doc.OptimizeResources();" </span><span style="background-color: white; ">before you replace the images the problem still remains.</span></pre><pre style="font-size: 13px; background-color: white; "><font face="Consolas">If you do the </font><span style="font-family: Consolas; background-color: white; ">"</span><span style="background-color: white; "><font face="Consolas"><font color="#008000">doc.OptimizeResources();" </font>twice (before and after the images are replaced) a null-reference-exception is thrown.</font></span></pre><pre style="font-size: 13px; background-color: white; "><span style="background-color: white; "><font face="Consolas"><br></font></span></pre><pre style="font-size: 13px; background-color: white; "><font face="Consolas">Thanks,</font></pre><pre style="font-size: 13px; background-color: white; "><font face="Consolas">Thomas</font></pre><pre style="font-size: 13px; background-color: white; "><span style="background-color: white; "><font face="Consolas"><br></font></span></pre><pre style="font-family: Consolas; font-size: 13px; background-color: white; background-position: initial initial; background-repeat: initial initial; "><br></pre><pre style="font-family: Consolas; font-size: 13px; background-color: white; background-position: initial initial; background-repeat: initial initial; "><br></pre></div>

Hi Thomas,

Thanks for using our products and sharing the sample source code and template document with us.

I tested the scenario and able to notice the same problem. For rectification, I logged this problem with ID: PDFNEWNET-33804 in our Issue Tracking System. We will further look into the details of this issue and will keep you updated via this forum thread on the status of correction.

We apologize for your inconvenience.

Thanks & Regards,

The issues you have found earlier (filed as PDFNEWNET-33804) have been fixed in Aspose.Pdf for .NET 7.4.0.


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