« Xojo developer meetin… | Home | 20 Years Monkeybread … »

Crop a two side page document to a single page document

Have you ever had the problem that you scan documents that’s are printed with two pages on each side and you need this document as a single page Document?

The MBS Xojo Plugins can help you with this problem in Xojo. Our Xojo Plugins includes a GraphicsMagick plugin. With the classes in this plugin you can do a lot of graphic editing, like using graphics effects, add text on an image or crop an image by a given area. Compared to Xojo pictures, we can work even with 16bit color depth here and use exact same functions cross platform.

In this article I want to show you how to cut the pages and set a serial page number at the bottom of each page. At first we load the scanned pictures from a folder to our application. For that we query the folder items to an array. With the method SortedFilesMBS we get an array that is sorted by name already. In the parameters of this method, we can set as the second Parameter that we want to sort the array by filenames. My scanned documents are names Scan_1, Scan_2 and so on.

Dim folder As FolderItem = SpecialFolder.Desktop.Child("Scan")
Dim files() As FolderItem = folder.SortedFilesMBS(False, True, False)

Then we want to loop over this array and cut the pages:

Dim u As Integer = files.Ubound
For i As Integer = 0 To u

We load the current file in two GMImage Objects. One for the Left side (imageL) and one to the right side (imageR). Instead of loading the file twice, we use the copy constructor to get two identical image objects.

Dim imageL As New GMImageMBS(files(i))
Dim imageR As New GMImageMBS(imageL) // copy constructor

If no exception is raised, we determine the height and width of the target pictures. We imagine we have a Din A4 paper on which two pages are printed. The long site is at the bottom side. The page is halved on this side. Therefore we need the full height of the sheet but only half the width. The clipping area is defined as GMGeometryMBS object. In the parameters we can defined the height and width of the cropping rectangle and optional the position of the top left edge coordinates. The default position coordinates are 0/0. That’s why we don’t need to set the coordinates for the left mask. For the left side we only need the height and width of the output. The right side has the same size but the x position is different. It is the half of the long bottom page side and that is w.

Dim h As Integer = imageL.height
Dim w As Integer = imageL.width / 2
Dim geoL As New GMGeometryMBS(w, h)
Dim geoR As New GMGeometryMBS(w, h, w, 0)

With the method crop we cut out the pages.


Then we want to write a page number in the bottom of each page. For drawing on a GMImage we set a graphic object as a property of the image.Then we set the color, the stroke width and the font, before we setting the text and the position of the page number. Afterwards we can draw the page number to the image. We need to take care about the stroke width, because if the value is too big the number is no longer legible. We need to calculate the text of the page number. With each loop run we manage two pages, we multiply it with two. Our variable i starts at 0, but our first left side has the number 1. That's why we add 1 for the left side and add 2 for the right side. For this reason we have for the left side the formula: I * 2 + 1 and for the right side the formula I * 2 + 2.

The code for the right page number looks similar.

Dim drawL As GMGraphicsMBS = imageL.Graphics
drawL.strokeColor(New GMColorRGBMBS("black"))
drawL.Text(w / 2, h - 100, str(i * 2 + 1))

Next we set the format of the image, create folder items and save the images on our desktop in a folder named “Output”, creating it if necessary.

imageL.magick = "PNG"
imageR.magick = "PNG"
Dim Output as folderitem = SpecialFolder.Desktop.Child("Output")
Dim sl As FolderItem = Output.Child(Str(i) + "a.png")
Dim sr As FolderItem = Output.Child(Str(i) + "b.png")


I hope that you like this example. We wish you lots of fun with the GraphicsMagick classes and functionalities. If you have any questions, please do not hesitate to contact us. The biggest plugin in space...
28 02 20 - 09:33