Inserted images are too big

Discussions about PDF management.
Post Reply
SarrasiM
Posts: 22
Joined: Thu Dec 17, 2015 6:20 pm

Inserted images are too big

Post by SarrasiM » Thu Oct 20, 2016 2:29 pm

Good morning,

I've been experimenting with the page insertion in PDF lately and I can't figure out why the pages that are added to an existing PDF are oversized. It does not happen when I append image. You can see the the actual view after insertion (text have been redacted) in the os.png attachment.

Following is the code snippet used:

Code: Select all

        private static void Insert(GdPictureImaging gdp)
        {
            var position = 3;
            var searchable = true;
            var idx = 1;
            var inserts = Directory.GetFiles(@".\add");

            var pdf = new GdPicturePDF();

            pdf.LoadFromFile(@".\base.pdf", true);
           
            if (position > 0) { 
                idx = position;
            }
            else
                idx = pdf.GetPageCount();

            foreach (var insert in inserts)
            {
                var id = gdp.CreateGdPictureImageFromFile(insert);

                if (position == -1) { 
                    pdf.AddImageFromGdPictureImage(id, PdfAdvancedImageCompression.PdfAdvancedImageCompressionColorDetection);

                    pdf.SelectPage(++idx);                    
                }
                else
                {
                    var res = pdf.AddImageFromGdPictureImage(id, false, false);
                    var width = (float)gdp.GetWidth(id);
                    var height = (float)gdp.GetHeight(id);
                                                            
                    pdf.InsertPage(width, height, idx);
                    pdf.SelectPage(idx++);
                    pdf.DrawImage(res, 0, 0, width, height);
                }

                if (searchable)
                    pdf.OcrPage("eng+fra", @".\data", string.Empty, 200f);
            }

            pdf.SaveToFile(@".\base.pdf");
            pdf.Dispose();
        }
I suspect this might be a measurement unit problem. The 'base.pdf' file is generated with Adobe Acrobat Pro from multiple combined images.

Thanks for your help!
Attachments
os.png
os.png (3.07 KiB) Viewed 2526 times

Cedric
Posts: 269
Joined: Sun Sep 02, 2012 7:30 pm

Re: Inserted images are too big

Post by Cedric » Fri Oct 21, 2016 10:49 am

First thing is that you cannot use pixel because the resolution concept does not exist in PDF, therefore you have to convert your image height and width to inches.
This is definitely a measurement unit problem, you do not define the unit to use which you should by calling this method : https://guides.gdpicture.com/content/we ... tUnit.html

SarrasiM
Posts: 22
Joined: Thu Dec 17, 2015 6:20 pm

Re: Inserted images are too big

Post by SarrasiM » Mon Oct 24, 2016 2:25 pm

Hello Cedric,

I've changed the code so that the 'width' and 'height' variables are respectively set to '8.5' and '11' with tis code:
var width = (float)gdp.GetWidth(id) / (float)gdp.GetHorizontalResolution(id); // 8.5
var height = (float)gdp.GetHeight(id) / (float)gdp.GetVerticalResolution(id); // 11

Those are then passed to InsertPage() and DrawImage(). The result is now the total opposite, the images are undersized as shown in the attached file. Could you elaborate a little more on how that convertion is done, if it's not the actual inches size?

Thanks,
Attachments
us.png
us.png (3.57 KiB) Viewed 2512 times

Cedric
Posts: 269
Joined: Sun Sep 02, 2012 7:30 pm

Re: Inserted images are too big

Post by Cedric » Mon Oct 24, 2016 2:35 pm

Please read my previous answer. Setting a size without first setting the measurement unit to use makes sense.
The default is most probably point (which is 1/72 inch) so it would explain why it is now undersized.

SarrasiM
Posts: 22
Joined: Thu Dec 17, 2015 6:20 pm

Re: Inserted images are too big

Post by SarrasiM » Mon Oct 24, 2016 2:53 pm

Cedric, the measurement unit is set to this pdf.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitInch).

SarrasiM
Posts: 22
Joined: Thu Dec 17, 2015 6:20 pm

Re: Inserted images are too big

Post by SarrasiM » Mon Oct 24, 2016 2:57 pm

Got it! I was setting the PDF measurement before reloading a PDF. Thanks Cedric :)

Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests