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

Free Support Forum - aspose.com

Document Corruption when using Resources.Images.Replace

Doing some proof of concepts...

I am having some issues with documents that have been resampled using the following code. I can also replicate the error by passing the retrieved stream back into the document.

I attached the original doc, and corrupted doc.

Page 2 Images are getting a black background.
Page 4 Image missing and throws error in Adobe Acrobat
Page 8 Image missing and throws error in Adobe Acrobat

static void ResampleDoc(String srcDoc, String destDoc)
		{
			Aspose.Pdf.License license = new Aspose.Pdf.License();
			license.SetLicense(@"C:\Users\sean.martin\Documents\Visual Studio 2010\Projects\AsposePDFTesting\_SharedLibraries\Aspose.Total.lic");
        Aspose<span style="color: rgb(232, 226, 183);">.</span>Pdf<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">Document</span> doc <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(147, 199, 99);">new</span> Aspose<span style="color: rgb(232, 226, 183);">.</span>Pdf<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">Document</span>(srcDoc);

        <span style="color: rgb(103, 140, 177);">Console</span><span style="color: rgb(232, 226, 183);">.</span>WriteLine(<span style="color: rgb(103, 140, 177);">String</span><span style="color: rgb(232, 226, 183);">.</span>Format(<span style="color: rgb(236, 118, 0);">"Optimizing {0} /r/n"</span>, srcDoc));
        <span style="color: rgb(102, 116, 123);">//doc.OptimizeResources();</span>

        <span style="color: rgb(147, 199, 99);">byte</span>[] outImg;
        <span style="color: rgb(103, 140, 177);">MemoryStream</span> outStrm;
        <span style="color: rgb(103, 140, 177);">MemoryStream</span> imgStream;
        Aspose<span style="color: rgb(232, 226, 183);">.</span>Pdf<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">Page</span> pg;
        
        <span style="color: rgb(147, 199, 99);">for</span> (<span style="color: rgb(147, 199, 99);">int</span> p <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(255, 205, 34);">1</span>; p <span style="color: rgb(232, 226, 183);"><=</span> doc<span style="color: rgb(232, 226, 183);">.</span>Pages<span style="color: rgb(232, 226, 183);">.</span>Count; p<span style="color: rgb(232, 226, 183);">++</span>)
        {
            pg <span style="color: rgb(232, 226, 183);">=</span> doc<span style="color: rgb(232, 226, 183);">.</span>Pages[p];
            <span style="color: rgb(103, 140, 177);">Console</span><span style="color: rgb(232, 226, 183);">.</span>WriteLine(<span style="color: rgb(103, 140, 177);">String</span><span style="color: rgb(232, 226, 183);">.</span>Format(<span style="color: rgb(236, 118, 0);">"Processing Page {0}"</span>, p));
            <span style="color: rgb(147, 199, 99);">for</span> (<span style="color: rgb(147, 199, 99);">int</span> i <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(255, 205, 34);">1</span>; i <span style="color: rgb(232, 226, 183);"><=</span> pg<span style="color: rgb(232, 226, 183);">.</span>Resources<span style="color: rgb(232, 226, 183);">.</span>Images<span style="color: rgb(232, 226, 183);">.</span>Count ; i<span style="color: rgb(232, 226, 183);">++</span>)
            {
                <span style="color: rgb(103, 140, 177);">Console</span><span style="color: rgb(232, 226, 183);">.</span>WriteLine(<span style="color: rgb(103, 140, 177);">String</span><span style="color: rgb(232, 226, 183);">.</span>Format(<span style="color: rgb(236, 118, 0);">"   Processing Img {0}"</span>, p));

                Aspose<span style="color: rgb(232, 226, 183);">.</span>Pdf<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">XImage</span> img <span style="color: rgb(232, 226, 183);">=</span> pg<span style="color: rgb(232, 226, 183);">.</span>Resources<span style="color: rgb(232, 226, 183);">.</span>Images[i];
                imgStream <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(147, 199, 99);">new</span> <span style="color: rgb(103, 140, 177);">MemoryStream</span>();
                
                img<span style="color: rgb(232, 226, 183);">.</span>Save(imgStream, System<span style="color: rgb(232, 226, 183);">.</span>Drawing<span style="color: rgb(232, 226, 183);">.</span>Imaging<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">ImageFormat</span><span style="color: rgb(232, 226, 183);">.</span>Jpeg);
                outImg <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(103, 140, 177);">OptimizeBase</span><span style="color: rgb(232, 226, 183);">.</span>Optimize(imgStream<span style="color: rgb(232, 226, 183);">.</span>ToArray(), <span style="color: rgb(255, 205, 34);">150</span>, <span style="color: rgb(255, 205, 34);">40</span>);
                outStrm <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(147, 199, 99);">new</span> <span style="color: rgb(103, 140, 177);">MemoryStream</span>(outImg);

                <span style="color: rgb(147, 199, 99);">if</span> (outStrm<span style="color: rgb(232, 226, 183);">.</span>Length <span style="color: rgb(232, 226, 183);">></span> <span style="color: rgb(255, 205, 34);">0</span> <span style="color: rgb(232, 226, 183);">&&</span> imgStream<span style="color: rgb(232, 226, 183);">.</span>Length <span style="color: rgb(232, 226, 183);">></span> outStrm<span style="color: rgb(232, 226, 183);">.</span>Length)
                {
                    pg<span style="color: rgb(232, 226, 183);">.</span>Resources<span style="color: rgb(232, 226, 183);">.</span>Images<span style="color: rgb(232, 226, 183);">.</span>Replace(i, outStrm);
                    outStrm<span style="color: rgb(232, 226, 183);">.</span>Flush();
                    outStrm<span style="color: rgb(232, 226, 183);">.</span>Close();
                }
            }
        }

        doc<span style="color: rgb(232, 226, 183);">.</span>Pages<span style="color: rgb(232, 226, 183);">.</span>Flatten();

        <span style="color: rgb(103, 140, 177);">Console</span><span style="color: rgb(232, 226, 183);">.</span>WriteLine(<span style="color: rgb(103, 140, 177);">String</span><span style="color: rgb(232, 226, 183);">.</span>Format(<span style="color: rgb(236, 118, 0);">"Saving!! {0} /r/n"</span>, srcDoc));

        doc<span style="color: rgb(232, 226, 183);">.</span>Save(destDoc);

	}</pre>

Otherwise it is working...

Thanks for the help!

Hi Sean,

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

I tested the scenario using your source code (change one line of code as mentioned below) with Aspose.Pdf for .NET v7.0 and unable to find any issue. Resultant PDF document is attached for your reference. Kindly use the latest version of Aspose.Pdf for .NET v7.0 and check if it works fine for you.

[C#]

static void ResampleDoc(String srcDoc, String destDoc)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

{

Aspose.Pdf.Document doc = new Aspose.Pdf.Document(srcDoc);

Console.WriteLine(String.Format("Optimizing {0} /r/n", srcDoc));

//doc.OptimizeResources();

byte[] outImg;

MemoryStream outStrm;

MemoryStream imgStream;

Aspose.Pdf.Page pg;

for (int p = 1; p <= doc.Pages.Count; p++)

{

pg = doc.Pages[p];

Console.WriteLine(String.Format("Processing Page {0}", p));

for (int i = 1; i <= pg.Resources.Images.Count; i++)

{

Console.WriteLine(String.Format(" Processing Img {0}", p));

Aspose.Pdf.XImage img = pg.Resources.Images[i];

imgStream = new MemoryStream();

img.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);

//outImg = OptimizeBase.Optimize(imgStream.ToArray(), 150, 40);

outImg = imgStream.ToArray();

outStrm = new MemoryStream(outImg);

if (outStrm.Length > 0 && imgStream.Length > outStrm.Length)

{

pg.Resources.Images.Replace(i, outStrm);

outStrm.Flush();

outStrm.Close();

}

}

}

doc.Pages.Flatten();

Console.WriteLine(String.Format("Saving!! {0} /r/n", srcDoc));

doc.Save(destDoc);

}

Please feel free to contact support in case you need any further assistance.

Thanks & Regards,

Thank you for the response. If I write the original image back to the document, it does not corrupt the document. However, if I change the quality settings on a JPEG it does corrupt the document.

I realized I left an important bit out of the code, here is everything:

        static void ResampleDoc(String srcDoc, String destDoc)
        {
            Aspose.Pdf.License license = new Aspose.Pdf.License();
            license.SetLicense(@"C:\Users\sean.martin\Documents\Visual Studio 2010\Projects\AsposePDFTesting\_SharedLibraries\Aspose.Total.lic");
        Aspose<span style="color: rgb(232, 226, 183);">.</span>Pdf<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">Document</span> doc <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(147, 199, 99);">new</span> Aspose<span style="color: rgb(232, 226, 183);">.</span>Pdf<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">Document</span>(srcDoc);

        <span style="color: rgb(103, 140, 177);">Console</span><span style="color: rgb(232, 226, 183);">.</span>WriteLine(<span style="color: rgb(103, 140, 177);">String</span><span style="color: rgb(232, 226, 183);">.</span>Format(<span style="color: rgb(236, 118, 0);">"Optimizing {0} /r/n"</span>, srcDoc));

        <span style="color: rgb(147, 199, 99);">byte</span>[] outImg;
        <span style="color: rgb(103, 140, 177);">MemoryStream</span> outStrm;
        <span style="color: rgb(103, 140, 177);">MemoryStream</span> imgStream;
        Aspose<span style="color: rgb(232, 226, 183);">.</span>Pdf<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">Page</span> pg;

        <span style="color: rgb(147, 199, 99);">for</span> (<span style="color: rgb(147, 199, 99);">int</span> p <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(255, 205, 34);">1</span>; p <span style="color: rgb(232, 226, 183);"><=</span> doc<span style="color: rgb(232, 226, 183);">.</span>Pages<span style="color: rgb(232, 226, 183);">.</span>Count; p<span style="color: rgb(232, 226, 183);">++</span>)
        {
            pg <span style="color: rgb(232, 226, 183);">=</span> doc<span style="color: rgb(232, 226, 183);">.</span>Pages[p];
            <span style="color: rgb(103, 140, 177);">Console</span><span style="color: rgb(232, 226, 183);">.</span>WriteLine(<span style="color: rgb(103, 140, 177);">String</span><span style="color: rgb(232, 226, 183);">.</span>Format(<span style="color: rgb(236, 118, 0);">"Processing Page {0}"</span>, p));
            <span style="color: rgb(147, 199, 99);">for</span> (<span style="color: rgb(147, 199, 99);">int</span> i <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(255, 205, 34);">1</span>; i <span style="color: rgb(232, 226, 183);"><=</span> pg<span style="color: rgb(232, 226, 183);">.</span>Resources<span style="color: rgb(232, 226, 183);">.</span>Images<span style="color: rgb(232, 226, 183);">.</span>Count; i<span style="color: rgb(232, 226, 183);">++</span>)
            {
                <span style="color: rgb(103, 140, 177);">Console</span><span style="color: rgb(232, 226, 183);">.</span>WriteLine(<span style="color: rgb(103, 140, 177);">String</span><span style="color: rgb(232, 226, 183);">.</span>Format(<span style="color: rgb(236, 118, 0);">"   Processing Img {0}"</span>, p));

                Aspose<span style="color: rgb(232, 226, 183);">.</span>Pdf<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">XImage</span> img <span style="color: rgb(232, 226, 183);">=</span> pg<span style="color: rgb(232, 226, 183);">.</span>Resources<span style="color: rgb(232, 226, 183);">.</span>Images[i];

                <span style="color: rgb(147, 199, 99);">using</span> (imgStream <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(147, 199, 99);">new</span> <span style="color: rgb(103, 140, 177);">MemoryStream</span>())
                {
                    img<span style="color: rgb(232, 226, 183);">.</span>Save(imgStream, <span style="color: rgb(103, 140, 177);">ImageFormat</span><span style="color: rgb(232, 226, 183);">.</span>Jpeg);
                    outImg <span style="color: rgb(232, 226, 183);">=</span> SetJPEGCompression(imgStream<span style="color: rgb(232, 226, 183);">.</span>ToArray(), <span style="color: rgb(255, 205, 34);">40</span>);

                    <span style="color: rgb(147, 199, 99);">using</span> (outStrm <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(147, 199, 99);">new</span> <span style="color: rgb(103, 140, 177);">MemoryStream</span>(outImg))
                    {
                        <span style="color: rgb(147, 199, 99);">if</span> (outStrm<span style="color: rgb(232, 226, 183);">.</span>Length <span style="color: rgb(232, 226, 183);">></span> <span style="color: rgb(255, 205, 34);">0</span> <span style="color: rgb(232, 226, 183);">&&</span> imgStream<span style="color: rgb(232, 226, 183);">.</span>Length <span style="color: rgb(232, 226, 183);">></span> outStrm<span style="color: rgb(232, 226, 183);">.</span>Length)
                        {
                            pg<span style="color: rgb(232, 226, 183);">.</span>Resources<span style="color: rgb(232, 226, 183);">.</span>Images<span style="color: rgb(232, 226, 183);">.</span>Replace(i, outStrm);
                            outStrm<span style="color: rgb(232, 226, 183);">.</span>Flush();
                            outStrm<span style="color: rgb(232, 226, 183);">.</span>Close();
                        }
                    }
                }
            }
        }

        doc<span style="color: rgb(232, 226, 183);">.</span>Pages<span style="color: rgb(232, 226, 183);">.</span>Flatten();

        <span style="color: rgb(103, 140, 177);">Console</span><span style="color: rgb(232, 226, 183);">.</span>WriteLine(<span style="color: rgb(103, 140, 177);">String</span><span style="color: rgb(232, 226, 183);">.</span>Format(<span style="color: rgb(236, 118, 0);">"Saving!! {0} /r/n"</span>, srcDoc));

        doc<span style="color: rgb(232, 226, 183);">.</span>Save(destDoc);

    }

    <span style="color: rgb(147, 199, 99);">public</span> <span style="color: rgb(147, 199, 99);">static</span> <span style="color: rgb(147, 199, 99);">byte</span>[] SetJPEGCompression(<span style="color: rgb(147, 199, 99);">byte</span>[] srcBytes, <span style="color: rgb(147, 199, 99);">long</span> compressionLevel)
    {
        <span style="color: rgb(103, 140, 177);">ImageConverter</span> ic <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(147, 199, 99);">new</span> <span style="color: rgb(103, 140, 177);">ImageConverter</span>();
        <span style="color: rgb(103, 140, 177);">Image</span> image <span style="color: rgb(232, 226, 183);">=</span> (<span style="color: rgb(103, 140, 177);">Image</span>)ic<span style="color: rgb(232, 226, 183);">.</span>ConvertFrom(srcBytes);

        <span style="color: rgb(147, 199, 99);">if</span> (image<span style="color: rgb(232, 226, 183);">.</span>RawFormat<span style="color: rgb(232, 226, 183);">.</span>Guid <span style="color: rgb(232, 226, 183);">==</span> System<span style="color: rgb(232, 226, 183);">.</span>Drawing<span style="color: rgb(232, 226, 183);">.</span>Imaging<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">ImageFormat</span><span style="color: rgb(232, 226, 183);">.</span>Jpeg<span style="color: rgb(232, 226, 183);">.</span>Guid)
        {
            <span style="color: rgb(147, 199, 99);">if</span> (compressionLevel <span style="color: rgb(232, 226, 183);"><</span> <span style="color: rgb(255, 205, 34);">0</span>)
            {
                compressionLevel <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(255, 205, 34);">0</span>;
            }
            <span style="color: rgb(147, 199, 99);">else</span> <span style="color: rgb(147, 199, 99);">if</span> (compressionLevel <span style="color: rgb(232, 226, 183);">></span> <span style="color: rgb(255, 205, 34);">100</span>)
            {
                compressionLevel <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(255, 205, 34);">100</span>;
            }
            <span style="color: rgb(103, 140, 177);">ImageCodecInfo</span> jgpEncoder <span style="color: rgb(232, 226, 183);">=</span> GetEncoder(<span style="color: rgb(103, 140, 177);">ImageFormat</span><span style="color: rgb(232, 226, 183);">.</span>Jpeg);
            System<span style="color: rgb(232, 226, 183);">.</span>Drawing<span style="color: rgb(232, 226, 183);">.</span>Imaging<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">Encoder</span> myEncoder <span style="color: rgb(232, 226, 183);">=</span> System<span style="color: rgb(232, 226, 183);">.</span>Drawing<span style="color: rgb(232, 226, 183);">.</span>Imaging<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">Encoder</span><span style="color: rgb(232, 226, 183);">.</span>Quality;
            <span style="color: rgb(103, 140, 177);">EncoderParameters</span> myEncoderParameters <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(147, 199, 99);">new</span> <span style="color: rgb(103, 140, 177);">EncoderParameters</span>(<span style="color: rgb(255, 205, 34);">1</span>);
            <span style="color: rgb(103, 140, 177);">EncoderParameter</span> myEncoderParameter <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(147, 199, 99);">new</span> <span style="color: rgb(103, 140, 177);">EncoderParameter</span>(myEncoder, compressionLevel);
            myEncoderParameters<span style="color: rgb(232, 226, 183);">.</span>Param[<span style="color: rgb(255, 205, 34);">0</span>] <span style="color: rgb(232, 226, 183);">=</span> myEncoderParameter;
            <span style="color: rgb(147, 199, 99);">using</span> (<span style="color: rgb(103, 140, 177);">MemoryStream</span> ms <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(147, 199, 99);">new</span> <span style="color: rgb(103, 140, 177);">MemoryStream</span>())
            {
                image<span style="color: rgb(232, 226, 183);">.</span>Save(ms, jgpEncoder, myEncoderParameters);
                <span style="color: rgb(147, 199, 99);">return</span> ms<span style="color: rgb(232, 226, 183);">.</span>ToArray();
            }
        }
        <span style="color: rgb(147, 199, 99);">else</span>
        {
            <span style="color: rgb(147, 199, 99);">return</span> srcBytes;
        }
    }

    <span style="color: rgb(147, 199, 99);">public</span> <span style="color: rgb(147, 199, 99);">static</span> <span style="color: rgb(103, 140, 177);">ImageCodecInfo</span> GetEncoder(<span style="color: rgb(103, 140, 177);">ImageFormat</span> format)
    {
        <span style="color: rgb(103, 140, 177);">ImageCodecInfo</span>[] codecs <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(103, 140, 177);">ImageCodecInfo</span><span style="color: rgb(232, 226, 183);">.</span>GetImageDecoders();
        <span style="color: rgb(147, 199, 99);">foreach</span> (<span style="color: rgb(103, 140, 177);">ImageCodecInfo</span> codec <span style="color: rgb(147, 199, 99);">in</span> codecs)
        {
            <span style="color: rgb(147, 199, 99);">if</span> (codec<span style="color: rgb(232, 226, 183);">.</span>FormatID <span style="color: rgb(232, 226, 183);">==</span> format<span style="color: rgb(232, 226, 183);">.</span>Guid)
            {
                <span style="color: rgb(147, 199, 99);">return</span> codec;
            }
        }
        <span style="color: rgb(147, 199, 99);">return</span> <span style="color: rgb(147, 199, 99);">null</span>;
    }

        static void ResampleDoc(String srcDoc, String destDoc)
{
Aspose.Pdf.License license = new Aspose.Pdf.License();
license.SetLicense(@“C:\Users\sean.martin\Documents\Visual Studio 2010\Projects\AsposePDFTesting_SharedLibraries\Aspose.Total.lic”);
        Aspose<span style="color: rgb(232, 226, 183);">.</span>Pdf<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">Document</span> doc <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(147, 199, 99);">new</span> Aspose<span style="color: rgb(232, 226, 183);">.</span>Pdf<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">Document</span>(srcDoc);

        <span style="color: rgb(103, 140, 177);">Console</span><span style="color: rgb(232, 226, 183);">.</span>WriteLine(<span style="color: rgb(103, 140, 177);">String</span><span style="color: rgb(232, 226, 183);">.</span>Format(<span style="color: rgb(236, 118, 0);">"Optimizing {0} /r/n"</span>, srcDoc));

        <span style="color: rgb(147, 199, 99);">byte</span>[] outImg;
        <span style="color: rgb(103, 140, 177);">MemoryStream</span> outStrm;
        <span style="color: rgb(103, 140, 177);">MemoryStream</span> imgStream;
        Aspose<span style="color: rgb(232, 226, 183);">.</span>Pdf<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">Page</span> pg;

        <span style="color: rgb(147, 199, 99);">for</span> (<span style="color: rgb(147, 199, 99);">int</span> p <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(255, 205, 34);">1</span>; p <span style="color: rgb(232, 226, 183);"><=</span> doc<span style="color: rgb(232, 226, 183);">.</span>Pages<span style="color: rgb(232, 226, 183);">.</span>Count; p<span style="color: rgb(232, 226, 183);">++</span>)
        {
            pg <span style="color: rgb(232, 226, 183);">=</span> doc<span style="color: rgb(232, 226, 183);">.</span>Pages[p];
            <span style="color: rgb(103, 140, 177);">Console</span><span style="color: rgb(232, 226, 183);">.</span>WriteLine(<span style="color: rgb(103, 140, 177);">String</span><span style="color: rgb(232, 226, 183);">.</span>Format(<span style="color: rgb(236, 118, 0);">"Processing Page {0}"</span>, p));
            <span style="color: rgb(147, 199, 99);">for</span> (<span style="color: rgb(147, 199, 99);">int</span> i <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(255, 205, 34);">1</span>; i <span style="color: rgb(232, 226, 183);"><=</span> pg<span style="color: rgb(232, 226, 183);">.</span>Resources<span style="color: rgb(232, 226, 183);">.</span>Images<span style="color: rgb(232, 226, 183);">.</span>Count; i<span style="color: rgb(232, 226, 183);">++</span>)
            {
                <span style="color: rgb(103, 140, 177);">Console</span><span style="color: rgb(232, 226, 183);">.</span>WriteLine(<span style="color: rgb(103, 140, 177);">String</span><span style="color: rgb(232, 226, 183);">.</span>Format(<span style="color: rgb(236, 118, 0);">"   Processing Img {0}"</span>, p));

                Aspose<span style="color: rgb(232, 226, 183);">.</span>Pdf<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">XImage</span> img <span style="color: rgb(232, 226, 183);">=</span> pg<span style="color: rgb(232, 226, 183);">.</span>Resources<span style="color: rgb(232, 226, 183);">.</span>Images[i];

                <span style="color: rgb(147, 199, 99);">using</span> (imgStream <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(147, 199, 99);">new</span> <span style="color: rgb(103, 140, 177);">MemoryStream</span>())
                {
                    img<span style="color: rgb(232, 226, 183);">.</span>Save(imgStream, <span style="color: rgb(103, 140, 177);">ImageFormat</span><span style="color: rgb(232, 226, 183);">.</span>Jpeg);
                    outImg <span style="color: rgb(232, 226, 183);">=</span> SetJPEGCompression(imgStream<span style="color: rgb(232, 226, 183);">.</span>ToArray(), <span style="color: rgb(255, 205, 34);">40</span>);

                    <span style="color: rgb(147, 199, 99);">using</span> (outStrm <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(147, 199, 99);">new</span> <span style="color: rgb(103, 140, 177);">MemoryStream</span>(outImg))
                    {
                        <span style="color: rgb(147, 199, 99);">if</span> (outStrm<span style="color: rgb(232, 226, 183);">.</span>Length <span style="color: rgb(232, 226, 183);">></span> <span style="color: rgb(255, 205, 34);">0</span> <span style="color: rgb(232, 226, 183);">&&</span> imgStream<span style="color: rgb(232, 226, 183);">.</span>Length <span style="color: rgb(232, 226, 183);">></span> outStrm<span style="color: rgb(232, 226, 183);">.</span>Length)
                        {
                            pg<span style="color: rgb(232, 226, 183);">.</span>Resources<span style="color: rgb(232, 226, 183);">.</span>Images<span style="color: rgb(232, 226, 183);">.</span>Replace(i, outStrm);
                            outStrm<span style="color: rgb(232, 226, 183);">.</span>Flush();
                            outStrm<span style="color: rgb(232, 226, 183);">.</span>Close();
                        }
                    }
                }
            }
        }

        doc<span style="color: rgb(232, 226, 183);">.</span>Pages<span style="color: rgb(232, 226, 183);">.</span>Flatten();

        <span style="color: rgb(103, 140, 177);">Console</span><span style="color: rgb(232, 226, 183);">.</span>WriteLine(<span style="color: rgb(103, 140, 177);">String</span><span style="color: rgb(232, 226, 183);">.</span>Format(<span style="color: rgb(236, 118, 0);">"Saving!! {0} /r/n"</span>, srcDoc));

        doc<span style="color: rgb(232, 226, 183);">.</span>Save(destDoc);

    }

    <span style="color: rgb(147, 199, 99);">public</span> <span style="color: rgb(147, 199, 99);">static</span> <span style="color: rgb(147, 199, 99);">byte</span>[] SetJPEGCompression(<span style="color: rgb(147, 199, 99);">byte</span>[] srcBytes, <span style="color: rgb(147, 199, 99);">long</span> compressionLevel)
    {
        <span style="color: rgb(103, 140, 177);">ImageConverter</span> ic <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(147, 199, 99);">new</span> <span style="color: rgb(103, 140, 177);">ImageConverter</span>();
        <span style="color: rgb(103, 140, 177);">Image</span> image <span style="color: rgb(232, 226, 183);">=</span> (<span style="color: rgb(103, 140, 177);">Image</span>)ic<span style="color: rgb(232, 226, 183);">.</span>ConvertFrom(srcBytes);

        <span style="color: rgb(147, 199, 99);">if</span> (image<span style="color: rgb(232, 226, 183);">.</span>RawFormat<span style="color: rgb(232, 226, 183);">.</span>Guid <span style="color: rgb(232, 226, 183);">==</span> System<span style="color: rgb(232, 226, 183);">.</span>Drawing<span style="color: rgb(232, 226, 183);">.</span>Imaging<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">ImageFormat</span><span style="color: rgb(232, 226, 183);">.</span>Jpeg<span style="color: rgb(232, 226, 183);">.</span>Guid)
        {
            <span style="color: rgb(147, 199, 99);">if</span> (compressionLevel <span style="color: rgb(232, 226, 183);"><</span> <span style="color: rgb(255, 205, 34);">0</span>)
            {
                compressionLevel <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(255, 205, 34);">0</span>;
            }
            <span style="color: rgb(147, 199, 99);">else</span> <span style="color: rgb(147, 199, 99);">if</span> (compressionLevel <span style="color: rgb(232, 226, 183);">></span> <span style="color: rgb(255, 205, 34);">100</span>)
            {
                compressionLevel <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(255, 205, 34);">100</span>;
            }
            <span style="color: rgb(103, 140, 177);">ImageCodecInfo</span> jgpEncoder <span style="color: rgb(232, 226, 183);">=</span> GetEncoder(<span style="color: rgb(103, 140, 177);">ImageFormat</span><span style="color: rgb(232, 226, 183);">.</span>Jpeg);
            System<span style="color: rgb(232, 226, 183);">.</span>Drawing<span style="color: rgb(232, 226, 183);">.</span>Imaging<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">Encoder</span> myEncoder <span style="color: rgb(232, 226, 183);">=</span> System<span style="color: rgb(232, 226, 183);">.</span>Drawing<span style="color: rgb(232, 226, 183);">.</span>Imaging<span style="color: rgb(232, 226, 183);">.</span><span style="color: rgb(103, 140, 177);">Encoder</span><span style="color: rgb(232, 226, 183);">.</span>Quality;
            <span style="color: rgb(103, 140, 177);">EncoderParameters</span> myEncoderParameters <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(147, 199, 99);">new</span> <span style="color: rgb(103, 140, 177);">EncoderParameters</span>(<span style="color: rgb(255, 205, 34);">1</span>);
            <span style="color: rgb(103, 140, 177);">EncoderParameter</span> myEncoderParameter <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(147, 199, 99);">new</span> <span style="color: rgb(103, 140, 177);">EncoderParameter</span>(myEncoder, compressionLevel);
            myEncoderParameters<span style="color: rgb(232, 226, 183);">.</span>Param[<span style="color: rgb(255, 205, 34);">0</span>] <span style="color: rgb(232, 226, 183);">=</span> myEncoderParameter;
            <span style="color: rgb(147, 199, 99);">using</span> (<span style="color: rgb(103, 140, 177);">MemoryStream</span> ms <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(147, 199, 99);">new</span> <span style="color: rgb(103, 140, 177);">MemoryStream</span>())
            {
                image<span style="color: rgb(232, 226, 183);">.</span>Save(ms, jgpEncoder, myEncoderParameters);
                <span style="color: rgb(147, 199, 99);">return</span> ms<span style="color: rgb(232, 226, 183);">.</span>ToArray();
            }
        }
        <span style="color: rgb(147, 199, 99);">else</span>
        {
            <span style="color: rgb(147, 199, 99);">return</span> srcBytes;
        }
    }

    <span style="color: rgb(147, 199, 99);">public</span> <span style="color: rgb(147, 199, 99);">static</span> <span style="color: rgb(103, 140, 177);">ImageCodecInfo</span> GetEncoder(<span style="color: rgb(103, 140, 177);">ImageFormat</span> format)
    {
        <span style="color: rgb(103, 140, 177);">ImageCodecInfo</span>[] codecs <span style="color: rgb(232, 226, 183);">=</span> <span style="color: rgb(103, 140, 177);">ImageCodecInfo</span><span style="color: rgb(232, 226, 183);">.</span>GetImageDecoders();
        <span style="color: rgb(147, 199, 99);">foreach</span> (<span style="color: rgb(103, 140, 177);">ImageCodecInfo</span> codec <span style="color: rgb(147, 199, 99);">in</span> codecs)
        {
            <span style="color: rgb(147, 199, 99);">if</span> (codec<span style="color: rgb(232, 226, 183);">.</span>FormatID <span style="color: rgb(232, 226, 183);">==</span> format<span style="color: rgb(232, 226, 183);">.</span>Guid)
            {
                <span style="color: rgb(147, 199, 99);">return</span> codec;
            }
        }
        <span style="color: rgb(147, 199, 99);">return</span> <span style="color: rgb(147, 199, 99);">null</span>;
    }

I am essentially resampling the JPEGs in the file and setting their quality lower. When I do this, some images in the file get corrupted.

Also, is there a way to tell what the underlying image type is of a resource. I have heard that PDF images are either native bitmaps, or JPEG images.

Thanks,

Sean

Hi Sean,

Thanks for sharing the details and complete source code with us. I tested the scenario and able to notice the same problem. For rectification, I logged this problem with ID: PDFNEWNET-33782 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,

Can you provide a workaround in the meantime? I would gladly skip over those files that would be corrupted by the resampling operation. Is there a way to determine ahead of time which images would be damaged by this operation?

Thank you!

Hi Sean,

Thanks for your inquiry, this is very difficult to find the damaged images before time. However, as a workaround you can use the images without compression same as mentioned in my first post.

Please feel free to contact support in case you need any further assistance.

Thanks & Regards,

Has this issue ( PDFNEWNET-33782 ) been addressed? We are looking into the issue again and were hoping that it had been corrected.

Hi Sean,


We are truly sorry for the inconvenience. I’m afraid your reported issue is not fixed completely. However, I’ve requested our development team to share an ETA at their earliest. We will keep you updated about the issue progress via this forum thread.

Thanks for your patience and cooperation.

Best Regards,

Hello,

I’m facing the same issue when trying to downsample images from a PDF file. If I change the resolution to a different value then some/most of images will get corrupted :

img.Save(imgStream, System.Drawing.Imaging.ImageFormat.Png,96);

If I’ll use the Save function without the resolution parameter it will work, but this doesn’t really help me because I want to be able to downsample them. Using Aspose.PDF 9.0.0.0 eval version here.

This issue is keeping me for purchasing a license right now, as everything else seems to fit my needs.


Thank you for your time.

Hi Razvan,


Thanks for your interest in Aspose.

We have recorded your concern and shared with the development team. I am afraid this issue is a bit complex and is not resolved completely. However we will update you as soon as we made some significant progress towards issue resolution.

Moreover, we will appreciate if you please share your sample input and output document here. It would help us to ensure your issue relates to the subjected issue.

We are sorry for the inconvenience caused.

Best Regards,

Hi,

Thanks for your reply. I’ll attach my original pdf and the one with images replaced. The code is your example from docs (or the one above by Sean):

Document pdfDocument = new Aspose.Pdf.Document(“c:/pdftest/Memory+Test.pdf”);
foreach (Page page in pdfDocument.Pages)
{
int idx = 1;
foreach (Aspose.Pdf.XImage image in page.Resources.Images)
{
using (var imageStream = new MemoryStream())
{
image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png,72);
imageStream.Seek(0, SeekOrigin.Begin);
page.Resources.Images.Replace(idx, imageStream);
}
idx = idx + 1;
}
}

pdfDocument.Save(“c:/pdftest/Compressed_output.pdf”);

The pattern I’ve found is that it will succeed for the first page but not for the next ones. And the problems is in the image.Save
function. If I save that imageStream resulted from the Save function to
file without further processing the image is just missing/malformed for the next page in PDF. One
workaround for me was to process all images from a page, close the document and save;
open it again and process images from next page; close it again and save it and so
on until all pages are done. But it takes to long to do that… but I guess it’s better then nothing…

Thank you,
Razvan


Hi Razvan,


Thanks for sharing the details. We will update you as soon as we made some significant progress towards issue resolution.

Thanks for your patience and cooperation.

Best Regards,

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

The blog post for this release is created over this link


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

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


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

Hi Razvan,


Thanks for your patience. Your above reported issue, PDFNEWNET-33782, has been fixed. Please use following code snippet to compress PDF file, it will help you to accomplish the task.

string inputFile = @“c:\33782.pdf”;<o:p></o:p>

string outputFile = @"c:\output.pdf";

using (Document doc = new Document(inputFile))

{

doc.Pages.Flatten();

Document.OptimizationOptions opt = Document.OptimizationOptions.All();

opt.CompressImages = true;

opt.ImageQuality = 20;

doc.OptimizeResources(opt);

doc.Save(outputFile);

}

Please feel free to contact us for any further assistance.


Best Regards,