GdPicturePDF.Scale and GdPicturePDF.Resize only resizing image

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

GdPicturePDF.Scale and GdPicturePDF.Resize only resizing image

Post by SarrasiM » Fri Jan 06, 2017 9:36 pm

Good day,

I wanted to use these methods to resize the pages inside a PDF after the OCR has been done. To my surprise it didn't work as expected: it would only scale the image ON the page, and not the page itself. See the image for exemple (I have removed te page content for security reason).

I've used a scaling factor of 0.5f and dividing the current page width and height by 2. Both gave the same result.

Thanks for your time and help,
Attachments
scale.png

delbeke
Posts: 91
Joined: Wed Oct 31, 2012 5:07 pm

Re: GdPicturePDF.Scale and GdPicturePDF.Resize only resizing image

Post by delbeke » Tue Jan 10, 2017 10:46 am

Hi SarrasiM

Can you give us a small snippet to reproduce the problem on our side.

Best regards.
Jean-Luc

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

Re: GdPicturePDF.Scale and GdPicturePDF.Resize only resizing image

Post by SarrasiM » Tue Jan 10, 2017 3:10 pm

Hello delbeke :)

Here's the code snippet as requested:

using (GdPicturePDF pdf = new GdPicturePDF())
{
var position = 1;

pdf.NewPDF(true);
pdf.SetJpegQuality(75);
pdf.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitInch);

foreach (var img in Directory.GetFiles(@"C:\Tests"))
{
var imageId = gdp.CreateGdPictureImageFromFile(img);

if (imageId == 0)
throw new NullReferenceException(string.Format("The image {0} does not exists or could not be properly loaded.", img));

var intent = gdp.ColorDetection(imageId, false, true, true);

if (intent <= GdPictureOutputIntent.IntentGrayscale)
gdp.ConvertTo8BppGrayScaleAdv(imageId);

var res = pdf.AddImageFromGdPictureImage(imageId, false, false);

pdf.InsertPage((float)gdp.GetWidth(imageId) / (float)gdp.GetHorizontalResolution(imageId), (float)gdp.GetHeight(imageId) / (float)gdp.GetVerticalResolution(imageId), position);
pdf.SelectPage(position);
pdf.DrawImage(res, 0, 0, (float)gdp.GetWidth(imageId) / (float)gdp.GetHorizontalResolution(imageId), (float)gdp.GetHeight(imageId) / (float)gdp.GetVerticalResolution(imageId));

pdf.OcrPage("eng", @".\data", "", 200);
pdf.ScalePage(0.5f, 0.5f); // pdf.ResizePage(pdf.GetPageWidth() / 2f, pdf.GetPageHeight() / 2f);

status = pdf.GetStat();

if (status != GdPictureStatus.OK)
throw new InvalidOperationException(string.Format("Image {0} could not be added to pdf (Status Code: {1})", img, status.ToString()));

gdp.ReleaseGdPictureImage(imageId);

position++;
}

if (pdf.SaveToFile(@"C:\Tests\1.pdf") != GdPictureStatus.OK)
throw new InvalidOperationException(string.Format("File {0} could not be saved.", "1.pdf"));

pdf.CloseDocument();
}

Thank you,

delbeke
Posts: 91
Joined: Wed Oct 31, 2012 5:07 pm

Re: GdPicturePDF.Scale and GdPicturePDF.Resize only resizing image

Post by delbeke » Tue Jan 10, 2017 5:58 pm

Hi SarrasiM

I have made a try to scale a page from a pdf and with the latest version of GdPicture, it works as expected.
I have only used Pdf.ScalePage(0.5f, 0.5f) and the page is resized correctly, its contents is resized accordingly.

I think you should make a test on a multipage existing pdf and resize one of the page to see the impact of the ScalePage function.
You can help yourself with the sample there : http://guides.gdpicture.com/content/web ... ePage.html

Hope this help

Best regards.
Jean-Luc

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

Re: GdPicturePDF.Scale and GdPicturePDF.Resize only resizing image

Post by SarrasiM » Mon Jan 16, 2017 8:33 pm

Hello delbeke,

Please see the two images attached. One has been resized using Scale() while the other has kept the original images dimensions.

What we are looking to accomplish is to reduce the PDF size by resizing the images in it. This worked well in previous attempts as we scaled the image before adding to the PDF but we realised that doing so made the OCR suffer of a very low success rate. Right now, we are trying to add the image to the PDF in its original dimensions, OCR it, and then resize. After a few verification, the size was indeed divided by half, but the file size remains almost the same as you can see on the pictures included. How can we add image in original dimensions, OCR, and then resize and maintain the previous recognized text?

Thanks for your help, it is much appreciated.
Attachments
size_2.png
size_1.png

delbeke
Posts: 91
Joined: Wed Oct 31, 2012 5:07 pm

Re: GdPicturePDF.Scale and GdPicturePDF.Resize only resizing image

Post by delbeke » Tue Jan 17, 2017 5:44 pm

Hi SarrasiM
Hi, the problem is completely different, scaling add a scaling matrix at the beginning of the content stream, the pdf content is not changed.

What you need is a resize of the image inside the page after the OCR. You do not need to resize the pdf page..

something like this:
...
pdf.OcrPage("eng", @".\data", "", 200);
int ImageWidth = oGdPictureImaging.GetWidth(imageID);
int ImageHeight = oGdPictureImaging.GetHeight(imageID);
gdp.Resize(imageId, ImageWidth/2, ImageHeight/2, Drawing2D.InterpolationMode.Bicubic);
pdf.DeleteImage(res);
res = pdf.AddImageFromGdPictureImage(imageId, false, false);
pdf.DrawImage(0,0, pdf.GatPageWidth(), pdf.GetPageHeight);

Hope this help.
Jean-Luc

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

Re: GdPicturePDF.Scale and GdPicturePDF.Resize only resizing image

Post by SarrasiM » Wed Jan 18, 2017 4:41 pm

Hello delbeke,

Thanks, it worked :)

delbeke
Posts: 91
Joined: Wed Oct 31, 2012 5:07 pm

Re: GdPicturePDF.Scale and GdPicturePDF.Resize only resizing image

Post by delbeke » Fri Jan 20, 2017 10:18 am

Hi SarrasiM

Thanks for the return. Have a nice day.

Best regards
Jean-Luc

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests