Generating the form

To generate a printable form and a recognition pattern file, provide the path to the form’s source code to generateTemplate method of OmrEngine class.

OmrEngine engine = new OmrEngine();
GenerationResult res = engine.generateTemplate("source.txt");

It will create a printable form for A4 (210 x 297 mm) paper in portrait (vertical) orientation using the default font and colors. Optionally, you can customize the paper size, font, colors, and other layout settings:

OmrEngine engine = new OmrEngine();
GlobalPageSettings pageSettings = new GlobalPageSettings();
pageSettings.FontFamily = "Courier New";
pageSettings.FontSize = 16;
GlobalPageSettings.FontStyle = FontStyle.Italic;
GenerationResult res = engine.generateTemplate("source.txt", pageSettings);

Adding images

Aspose.OMR for Java allows you to personalize machine-readable forms by adding images (such as your company logo or respondent’s photo) to them. In addition to describing the element in the source code, the full path to each image file must be directly passed to the form generator.

Image paths are provided through the ImageCollection object. The collection keys must exactly match the names of the image files referenced in the form’s source code. For example, if the source code refers to the file logo.png (?image=logo.png):

OmrEngine engine = new OmrEngine();
// Configure form layout
GlobalPageSettings pageSettings = new GlobalPageSettings();
pageSettings.PaperSize = PaperSize.Letter;
// Add images
InputStream logoStream = ReadFile("sources/logo.png");
ImageCollection images = new ImageCollection();
images.add("logo.png", logoStream);
// Generate form
GenerationResult res = engine.generateTemplate("source.txt", images, pageSettings);

Saving a printable form

After the template has been successfully generated, you can save it to disk in the preferred format.

The saved results consist of several files:

  • Printable form pages as PNG images. Their dimensions match the provided paper size.
  • A recognition pattern used by Aspose.OMR recognition engine, in a special .OMR format. This file is required for recognizing completed forms, make sure you do not accidentally delete it!

To save a form, call Save method of of the GenerationResult object returned by generateTemplate method. The method takes the following arguments:

  • Target directory name, either absolute or relative to your application’s working directory. Provide an empty string to save files into the working directory of the application.
  • File name (without extension) for printable form pages and the recognition pattern.

Example

OmrEngine engine = new OmrEngine();
GenerationResult res = engine.generateTemplate("source.txt");
res.save("forms", "survey");

This code generates the following files under forms sub-directory of the application’s working directory:

File Description
survey.png An image of a machine-readable form that can be printed and distributed to respondents.
survey.omr Recognition pattern file, that is used to map optical marks to bubbles for highly accurate recognition.