File can't be compressed.

Discussions about machine vision support in GdPicture.
Post Reply
Joslu
Posts: 8
Joined: Mon Jan 21, 2019 12:36 pm

File can't be compressed.

Post by Joslu » Mon Feb 25, 2019 12:23 pm

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.
Attachments
Testdokument.pdf
(2 MiB) Downloaded 648 times

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: File can't be compressed.

Post by Loïc » Tue Feb 26, 2019 3:59 pm

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

Joslu
Posts: 8
Joined: Mon Jan 21, 2019 12:36 pm

Re: File can't be compressed.

Post by Joslu » Mon Mar 04, 2019 4:38 pm

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.

Gabriela
Posts: 436
Joined: Wed Nov 22, 2017 9:52 am

Re: File can't be compressed.

Post by Gabriela » Mon Mar 04, 2019 5:50 pm

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.

Joslu
Posts: 8
Joined: Mon Jan 21, 2019 12:36 pm

Re: File can't be compressed.

Post by Joslu » Tue Mar 05, 2019 9:08 am

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?

Gabriela
Posts: 436
Joined: Wed Nov 22, 2017 9:52 am

Re: File can't be compressed.

Post by Gabriela » Tue Mar 05, 2019 9:18 am

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.

Joslu
Posts: 8
Joined: Mon Jan 21, 2019 12:36 pm

Re: File can't be compressed.

Post by Joslu » Tue Mar 05, 2019 1:55 pm

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.
Attachments
Testdokument_compressed.pdf
(1.8 MiB) Downloaded 619 times

Gabriela
Posts: 436
Joined: Wed Nov 22, 2017 9:52 am

Re: File can't be compressed.

Post by Gabriela » Tue Mar 05, 2019 2:39 pm

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.

Joslu
Posts: 8
Joined: Mon Jan 21, 2019 12:36 pm

Re: File can't be compressed.

Post by Joslu » Tue Mar 05, 2019 2:45 pm

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);

Gabriela
Posts: 436
Joined: Wed Nov 22, 2017 9:52 am

Re: File can't be compressed.

Post by Gabriela » Tue Mar 05, 2019 3:20 pm

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.

Joslu
Posts: 8
Joined: Mon Jan 21, 2019 12:36 pm

Re: File can't be compressed.

Post by Joslu » Tue Mar 05, 2019 3:30 pm

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 :)

Gabriela
Posts: 436
Joined: Wed Nov 22, 2017 9:52 am

Re: File can't be compressed.

Post by Gabriela » Tue Mar 05, 2019 7:06 pm

Hi,

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

Joslu
Posts: 8
Joined: Mon Jan 21, 2019 12:36 pm

Re: File can't be compressed.

Post by Joslu » Fri Mar 08, 2019 9:38 am

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);
            }
        }
    }
}
Attachments
Testdokument_converted.pdf
(1.89 MiB) Downloaded 609 times

Gabriela
Posts: 436
Joined: Wed Nov 22, 2017 9:52 am

Re: File can't be compressed.

Post by Gabriela » Fri Mar 08, 2019 2:30 pm

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.
Attachments
Testdokument_imagebased.pdf
(96.49 KiB) Downloaded 623 times

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest