Saving a printable form and recognition pattern
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 as an image or PDF document.
- A recognition pattern used by Aspose.OMR recognition engine, in a special .OMR format. This file is required for mapping optical marks to form elements and is necessary for recognizing filled forms!
Saving form to a file
The printable form can be saved in one or more images (one image per page) or in one PDF document. Regardless of the chosen format, the recognition pattern file is always saved to the save folder.
Saving form as an image
Call Save
method of of the GenerationResult
object returned by GenerateTemplate
or GenerateJSONTemplate
methods. 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.
- File name (without extension) for printable form pages and the recognition pattern. Each page is always saved as a separate file. If multiple pages are generated, the suffix “Page{NUMBER}” will be added to each file; for example, “OMR-FormPage1.png”.
Printable pages are saved as PNG images. Their dimensions match the provided paper size and orientation.
Saving form as a PDF document
Call SaveAsPdf
method of of the GenerationResult
object returned by GenerateTemplate
or GenerateJSONTemplate
methods. 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.
- File name (without extension) for a printable form and the recognition pattern. All pages are saved to a single PDF document.
Generating form to a memory stream
If you do not have direct access to storage (for example, when building web applications), you can generate the form into memory.
To enable in-memory form generation, convert the object returned by GenerateTemplate
or GenerateJSONTemplate
methods to MemoryGenerationResult
object:
Aspose.OMR.Api.OmrEngine omrEngine = new Aspose.OMR.Api.OmrEngine();
Aspose.OMR.Generation.GenerationResult generationResult = omrEngine.GenerateTemplate("source.txt");
if(generationResult.ErrorCode != 0)
{
Console.WriteLine(generationResult.ErrorMessage);
return generationResult.ErrorCode;
}
Aspose.OMR.Generation.MemoryGenerationResult memoryGenerationResult = new Aspose.OMR.Generation.MemoryGenerationResult(generationResult);
Getting form pages as images
Call GetImages
method of the MemoryGenerationResult
object. All pages of the OMR form are returned as a collection of MemoryStream
objects containing bitmap images of form pages.
IEnumerable<MemoryStream> pages = memoryGenerationResult.GetImages();
foreach(var page in pages)
{
byte[] pageImageBytes = page.ToArray();
}
Getting form as PDF document
Call GetPDF
method of the MemoryGenerationResult
object. All pages of the OMR form are returned as a MemoryStream
object containing a PDF document.
MemoryStream form = memoryGenerationResult.GetPDF();
byte[] formBytes = form.ToArray();
Getting recognition pattern
Call GetOmr
method of the MemoryGenerationResult
object. The recognition pattern is returned as a MemoryStream
object.
MemoryStream omr = memoryGenerationResult.GetOmr();
byte[] recognitionPattern = omr.ToArray();
Iterating through pages
GenerationResult object returned by GenerateTemplate
or GenerateJSONTemplate
methods also contains a collection of all generated printable pages (as System.Drawing.Bitmap
objects). You can manually iterate through this list and save the pages in different formats if necessary.
Example
Aspose.OMR.Api.OmrEngine omrEngine = new Aspose.OMR.Api.OmrEngine();
Aspose.OMR.Generation.GenerationResult generationResult = omrEngine.GenerateTemplate("source.txt");
if(generationResult.ErrorCode != 0)
{
Console.WriteLine(generationResult.ErrorMessage);
return generationResult.ErrorCode;
}
int i = 1;
foreach(System.Drawing.Bitmap bitmap in generationResult.MultipageTemplateImages)
{
bitmap.Save($"page-{i++}.bmp");
}