Page 1 of 1

File can't be compressed.

Posted: Mon Feb 25, 2019 12:23 pm
by Joslu
Hello,

currently my software has an utility to compress PDF files.

I've read the forum and I've followed this tutorial: https://www.gdpicture.com/guides/gdpicture/web ... geMRC.html

The problem is that some documents have the exactly file size after the compression.

I let attached one of the documents which can't be compressed.

Than you so much.

Re: File can't be compressed.

Posted: Tue Feb 26, 2019 3:59 pm
by Loïc
Hi,

This document is basically not a good candidate as it is including vector text and image. So basically you re not validating this condition in the code: if (isPageImage).

You may want to reduce size of the embedded image, for such purpose here a good option: https://www.gdpicture.com/guides/gdpicture/web ... Image.html

Kind regards,

Loïc

Re: File can't be compressed.

Posted: Mon Mar 04, 2019 4:38 pm
by Joslu
Hello there,

First of all thanks for the reply, I've been able to code the function and compress the image.

On the other hand I'm afraid it makes not a huge difference. The original file is 2 Mb and the compressed is 1.8 Mb.

I've realised that the function has no overloads to specify parameters.

Could I specify compression parameters somehow, or obtain the image from PDF to compress it externally and then use ReplaceImage function?

Thank you so much,
Jose Luis.

Re: File can't be compressed.

Posted: Mon Mar 04, 2019 5:50 pm
by Gabriela
Hello,

You can use these methods to render the current page to GdPictureImage object:
https://www.gdpicture.com/guides/gdpicture/web ... Image.html
https://www.gdpicture.com/guides/gdpicture/web ... ageEx.html
And then you can use methods of GdPictureImaging class to improve the image.

Re: File can't be compressed.

Posted: Tue Mar 05, 2019 9:08 am
by Joslu
Hello Gabriela, thanks for your reply.

Documentation for RenderPageToGdPictureImageEx Method says "The method's behaviour is specific for pages, which are entirely image-based", nevertheless my PDF is a text one with an image.

Will these functions work anyway?

Re: File can't be compressed.

Posted: Tue Mar 05, 2019 9:18 am
by Gabriela
Hello,

Yes, of course, they will work exactly like it is described. For image-based pages, the whole image is extracted "as is" in its original bit depth with the original dpi, so the method does not respect the DPI you will specify. For mixed pages, it will work with the required DPI you set.

Re: File can't be compressed.

Posted: Tue Mar 05, 2019 1:55 pm
by Joslu
Thank you so much for the replies, they've been very useful.

I'm getting some troubles with ReplaceImageMRC and the attached pdf. It seems it's doing the compression over the top of the image but not on the rest of the image.

This is my current code on the 'else' of 'isPageImage':

Code: Select all

int imageCount = pdf.GetPageImageCount();

for (int i = 0; i < imageCount; i++) {
    int imgID = pdf.ExtractPageImage(i);
    string imgRes = pdf.GetPageImageResName(imgID);
    pdf.ReplaceImageMRC(imgRes, imgID);
    GdPictureDocumentUtilities.DisposeImage(imgID);
}
You can find the output attached as well.

Re: File can't be compressed.

Posted: Tue Mar 05, 2019 2:39 pm
by Gabriela
Hi,

Could you please send the whole code snippet you are using? It has no sense to add the 'else' of 'isPageImage' to ReplaceImageMRC() method.

Re: File can't be compressed.

Posted: Tue Mar 05, 2019 2:45 pm
by Joslu
I took this tutorial as example: https://www.gdpicture.com/guides/gdpicture/web ... geMRC.html

Code: Select all

for (int pageNo = 1; (pageNo <= pageCount) && (status == GdPictureStatus.OK); pageNo++) {
    status = pdf.SelectPage(pageNo);
    if (status == GdPictureStatus.OK) {
        //We can ignore hidden text in the example.
        bool isPageImage = pdf.IsPageImage(true);
        status = pdf.GetStat();
        if (status == GdPictureStatus.OK) {
            if (isPageImage) {
                //As the page is image-based, the image number is 1 and we can extract this image.
                int imageID = pdf.ExtractPageImage(1);
                status = pdf.GetStat();
                if (status == GdPictureStatus.OK) {
                    //Getting the resource name of the extracted image, the index of the image to be used here is 0.
                    string imageResName = pdf.GetPageImageResName(0);
                    status = pdf.GetStat();
                    if (status == GdPictureStatus.OK) {
                        //The original image is replaced with the compressed image on the whole page surface here.
                        status = pdf.ReplaceImageMRC(imageResName, imageID);
                        //You need to dispose the used image resource here.
                        GdPictureDocumentUtilities.DisposeImage(imageID);
                    }
                }
            } 
            else {
                GdPictureImaging pic = new GdPictureImaging();
                int imageCount = pdf.GetPageImageCount();

                for (int i = 0; i < imageCount; i++) {
                    int imgID = pdf.ExtractPageImage(i);
                    string imgRes = pdf.GetPageImageResName(imgID);
                    pdf.ReplaceImageMRC(imgRes, imgID);
                    GdPictureDocumentUtilities.DisposeImage(imgID);
                }


                
            }
        }
    }
}
if (status == GdPictureStatus.OK) {
    status = pdf.SaveToFile(tempPath, true);

Re: File can't be compressed.

Posted: Tue Mar 05, 2019 3:20 pm
by Gabriela
Hi,

This makes no sense. The ReplaceImageMRC() is ONLY for image-based pages. So the else statement is meaningless here. You need to use RenderPageToGdPictureImage() and then you can use AddImageFromGdPictureImage() method to make your source document image-based. So basically you need to prepare your source document first and then you can use the ReplaceImageMRC() method.

Re: File can't be compressed.

Posted: Tue Mar 05, 2019 3:30 pm
by Joslu
Hi,

I agree, since the beginning of the topic I specified that my PDF contains vector text and images, so it's not an image-based PDF and I can't convert the PDF because I need the text to be selectable.

My question is: Can I compress somehow the embeded images in a non image-based PDF?

Thank you :)

Re: File can't be compressed.

Posted: Tue Mar 05, 2019 7:06 pm
by Gabriela
Hi,

This question has been answered above. You should try to use these methods:
https://www.gdpicture.com/guides/gdpicture/web ... Image.html

Re: File can't be compressed.

Posted: Fri Mar 08, 2019 9:38 am
by Joslu
Hello there,
Eventually I was allowed to convert mixed PDF's to image-based PDF's. So, following your instructions I get the page using RenderPageToGdPictureImage and then I replace the original with ReplaceImage.

Unfortunately, the PDF output is corrupted.

I let attached my current code and the PDF output:

Code: Select all

for (int pageNo = 1; (pageNo <= pageCount) && (status == GdPictureStatus.OK); pageNo++) {
    status = pdf.SelectPage(pageNo);
    if (status == GdPictureStatus.OK) {
        //We can ignore hidden text in the example.
        bool isPageImage = pdf.IsPageImage(true);
        status = pdf.GetStat();
        if (status == GdPictureStatus.OK) {
            if (!isPageImage) {
                int imgID = pdf.RenderPageToGdPictureImage(200, true);
                string imgRes = pdf.GetPageImageResName(imgID);
                pdf.ReplaceImage(imgRes, imgID, false, true);
            }
        }
    }
}

Re: File can't be compressed.

Posted: Fri Mar 08, 2019 2:30 pm
by Gabriela
Hello,

Here is a very simple code snippet that should work for you:

Code: Select all

GdPicturePDF sourcePDF = new GdPicturePDF();
GdPicturePDF destPDF = new GdPicturePDF();
if (sourcePDF.LoadFromFile("Testdokument.pdf", false) == GdPictureStatus.OK)
{
    int imageID = sourcePDF.RenderPageToGdPictureImage(200, false);
    if (sourcePDF.GetStat() == GdPictureStatus.OK)
    {
        if ((destPDF.NewPDF(PdfConformance.PDF) == GdPictureStatus.OK) &&
            (destPDF.AddImageFromGdPictureImage(imageID, PdfAdvancedImageCompression.PdfAdvancedImageCompressionMRC) == GdPictureStatus.OK) &&
            (destPDF.SaveToFile("Testdokument_imagebased.pdf") == GdPictureStatus.OK))
            MessageBox.Show("Done!");
        else
            MessageBox.Show("Error!");
    }
}
else MessageBox.Show("The file can't be loaded.");
oGdPicturePDF.Dispose();
Please find the resulting file attached.