« Xojo developer availa… | Home | New testimonial from … »

Create PDF from scanned images and skip blank pages

When you scan images from a flatbed scanner or document feeder, you may get a collection of image files, one for each page. Than you may want to join them as a big PDF document and you may prefer skipping white pages.

Recently I wrote a script for this task using MBS FileMaker Plugin and DynaPDF.

In my script I scan the pages with our WIA.Scan function and get the list of picture file paths from WIA.Images function. The path is stored at $paths variable in the sample script below. In the following loop I use GMImage.NewFromFile function to load the image file into memory and get back the reference number for the image. This image is converted (if necessary) to the RGB colorspace with GMImage.SetType function. This is a necessary condition for the GMImage.AveragePixelValue function. It calculates the average pixel value and depending of the image content it is more black (0.0) or white (1.0). The function also need the dimensions of the region that should be tested. I want to test the whole page and simply pass the whole image size, queried via GMImage.GetWidth and GMImage.GetHeight functions. 

 

A perfect white page would have an average pixel value of 1.0. But scanners usually do not scan perfect white pages, because dust pollutes the scan result. So we need to test against a little bit smaller value like 0.999. 


If the value is bigger than 0.9999 (not completely white) the image is append to the current pdf document. Than we free the image from memory and continue in the loop.

Go to Record/Request/Page [ First ]

Set Variable [ $destPage ; Value: 1 ] 

Set Variable [ $CurrentImage ; Value: 1 ] 

Loop

Set Variable [ $currentPicturePaths ; Value: GetValue ($paths ; $currentImage ) ] 

# blank page?

Set Variable [ $RefNum ; Value: MBS( "GMImage.NewFromFile"; $currentPicturePaths  ) ] 

# Set RGB

Set Variable [ $r ; Value: MBS("GMImage.SetType"; $RefNum; 6) ] 

Set Variable [ $ImageHeight ; Value: MBS("GMImage.GetHeight"; $RefNum) ] 

Set Variable [ $ImageWidth ; Value: MBS("GMImage.GetWidth"; $RefNum) ] 

Set Variable [ $AveragePixelValue ; Value: MBS( "GMImage.AveragePixelValue"; $refNum; 0; 0; $imageWidth; $imageHeight ) ] 

# If AveragePixelValue can not be calculated the Image is not import into the pdf  

If [ MBS("IsError") ] 

Show Custom Dialog [ $r ] 

Else

If [ $AveragePixelValue   <  ,9999 ] 

# Adds new pages to the PDF

Set Variable [ $r ; Value: MBS( "DynaPDF.AppendImagePages"; $pdf; $currentPicturePaths ) ] 

# Releases the image from memory

Set Variable [ $r ; Value: MBS( "GMImage.Release"; $RefNum ) ] 

End If

End If

Set Variable [ $CurrentImage ; Value: $CurrentImage + 1 ] 

Exit Loop If [ $CurrentImage > $i - 1 ] 

Go to Record/Request/Page [ Next ; Exit after last: On ]

End Loop

For this example you need the MBS FileMaker Plugin and at least a DynaPDF starter license from us. You can even request a trial license on our homepage.

The script above skips parts for initializing DynaPDF, creating new document and later saving the document. Please refer to examples included with MBS Plugin for details.

Have fun with this functionality in your solutions. 

If you have questions, please do not hesitate to contact me.

 

Written by Stefanie Juchmes

13 03 19 - 17:15