Generating the template

To generate a printable form and a recognition pattern file, process the form’s source code with GenerateTemplate or GenerateJSONTemplate methods of OmrEngine class instance.

Aspose.OMR.Api.OmrEngine omrEngine = new Aspose.OMR.Api.OmrEngine();

The method depends on the template source markup.

Text markup

Aspose.OMR text markup is a lightweight markup language specifically tailored for describing content and layout of Aspose.OMR forms. It is content-focused, with minimal number of tags or formatting instructions.

You can pass the template source code to the generator in a number of ways:

As a path to the text file

The easiest way is to call the GenerateTemplate method with a relative or absolute path to the file with the source code:

Aspose.OMR.Generation.GenerationResult generationResult = omrEngine.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. Note, that if the form contains images, they will not be rendered through the basic call and you will get an error.

Optionally, you can customize the paper size, orientation, font, and other layout settings that apply to all template pages, as well as specify paths to images used in the form by passing GlobalPageSettings object to the method:

Aspose.OMR.Generation.GlobalPageSettings globalPageSettings = new Aspose.OMR.Generation.GlobalPageSettings();
globalPageSettings.PaperSize = Aspose.OMR.Generation.PaperSize.Letter;
Aspose.OMR.Generation.GenerationResult generationResult = omrEngine.GenerateTemplate("source.txt", globalPageSettings);

If your form contains images and you are fine with the default page settings, pass the absolute path to each image file in a string array:

string workingDirectory = System.IO.Directory.GetCurrentDirectory();
string[] images = {
	System.IO.Path.Combine(workingDirectory, "aspose-logo.png")
};
Aspose.OMR.Generation.GenerationResult generationResult = omrEngine.GenerateTemplate("source.txt", images);

You can also define the encoding of a text file. By default, the API treats the source file as UTF-8 encoded.

Aspose.OMR.Generation.GenerationResult generationResult = omrEngine.GenerateTemplate("source.txt", Encoding.ASCII);

As an array of lines

Pass all lines of the source code as a string array to the GenerateTemplate method:

string[] sourceCode = {
	"?grid=Phone number",
	"\tsections_count=7",
	"\torientation=vertical"
};
Aspose.OMR.Generation.GenerationResult generationResult = omrEngine.GenerateTemplate(sourceCode);

It will create a printable form for A4 (210 x 297 mm) paper in portrait (vertical) orientation using the default font and colors. Note, that if the form contains images, they will not be rendered through this call and you will get an error.

Optionally, you can customize the paper size, orientation, font, and other layout settings that apply to all template pages, as well as specify paths to images used in the form by passing GlobalPageSettings object to the method:

string workingDirectory = System.IO.Directory.GetCurrentDirectory();
Aspose.OMR.Generation.GlobalPageSettings globalPageSettings = new Aspose.OMR.Generation.GlobalPageSettings();
globalPageSettings.PaperSize = Aspose.OMR.Generation.PaperSize.Letter;
globalPageSettings.ImagesPaths = new string[] {
	System.IO.Path.Combine(workingDirectory, "aspose-logo.png")
};
string[] sourceCode = {
	"?grid=Phone number",
	"\tsections_count=7",
	"\torientation=vertical"
};
Aspose.OMR.Generation.GenerationResult generationResult = omrEngine.GenerateTemplate(sourceCode, globalPageSettings);

Alternatively, you can load images used in a form from memory through a special Aspose.OMR.Api.ImageCollection object - a collection of key-value pairs where the key contains the image file name and the value contains the binary content (System.IO.MemoryStream) of the image file. This parameter always takes precedence over page settings - if two images with the same file name are defined in both parameters, the one from the page settings will be ignored.

Aspose.OMR.Generation.GlobalPageSettings globalPageSettings = new Aspose.OMR.Generation.GlobalPageSettings();
globalPageSettings.PaperSize = Aspose.OMR.Generation.PaperSize.Letter;
string[] sourceCode = {
	"?grid=Phone number",
	"\tsections_count=7",
	"\torientation=vertical"
};
Aspose.OMR.Api.ImageCollection images = new Aspose.OMR.Api.ImageCollection();
images.Add("aspose-logo.png", new MemoryStream(File.ReadAllBytes(@"form\images\aspose-logo.png")));
Aspose.OMR.Generation.GenerationResult generationResult = omrEngine.GenerateTemplate(sourceCode, globalPageSettings, images);

As a memory stream

Pass the source code as a MemoryStream object to the GenerateTemplate method:

Aspose.OMR.Generation.GenerationResult generationResult = null;
using(System.IO.FileStream fs = new System.IO.FileStream("source.txt", System.IO.FileMode.Open))
{
	System.IO.MemoryStream ms = new System.IO.MemoryStream();
	fs.CopyTo(ms);
	generationResult = omrEngine.GenerateTemplate(ms);
};

It will create a printable form for A4 (210 x 297 mm) paper in portrait (vertical) orientation using the default font and colors. Note, that if the form contains images, they will not be rendered through this call and you will get an error.

Optionally, you can customize the paper size, orientation, font, and other layout settings that apply to all template pages, as well as specify paths to images used in the form by passing GlobalPageSettings object to the method:

string workingDirectory = System.IO.Directory.GetCurrentDirectory();
Aspose.OMR.Generation.GlobalPageSettings globalPageSettings = new Aspose.OMR.Generation.GlobalPageSettings();
globalPageSettings.PaperSize = Aspose.OMR.Generation.PaperSize.Letter;
globalPageSettings.ImagesPaths = new string[] {
	System.IO.Path.Combine(workingDirectory, "aspose-logo.png")
};
Aspose.OMR.Generation.GenerationResult generationResult = null;
using(System.IO.FileStream fs = new System.IO.FileStream("source.txt", System.IO.FileMode.Open))
{
	System.IO.MemoryStream ms = new System.IO.MemoryStream();
	fs.CopyTo(ms);
	generationResult = omrEngine.GenerateTemplate(ms, globalPageSettings);
};

Alternatively, you can load images used in a form from memory through a special Aspose.OMR.Api.ImageCollection object - a collection of key-value pairs where the key contains the image file name and the value contains the binary content (System.IO.MemoryStream) of the image file. This parameter always takes precedence over page settings - if two images with the same file name are defined in both parameters, the one from the page settings will be ignored.

Aspose.OMR.Generation.GlobalPageSettings globalPageSettings = new Aspose.OMR.Generation.GlobalPageSettings();
globalPageSettings.PaperSize = Aspose.OMR.Generation.PaperSize.Letter;
Aspose.OMR.Api.ImageCollection images = new Aspose.OMR.Api.ImageCollection();
images.Add("aspose-logo.png", new MemoryStream(File.ReadAllBytes(@"form\images\aspose-logo.png")));
Aspose.OMR.Generation.GenerationResult generationResult = null;
using(System.IO.FileStream fs = new System.IO.FileStream("source.txt", System.IO.FileMode.Open))
{
	System.IO.MemoryStream ms = new System.IO.MemoryStream();
	fs.CopyTo(ms);
	generationResult = omrEngine.GenerateTemplate(ms, globalPageSettings, images);
};

You can also define the encoding of a memory stream. By default, the API treats the stream as UTF-8 encoded text.

Aspose.OMR.Generation.GenerationResult generationResult = null;
using(System.IO.FileStream fs = new System.IO.FileStream("source.txt", System.IO.FileMode.Open))
{
	System.IO.MemoryStream ms = new System.IO.MemoryStream();
	fs.CopyTo(ms);
	generationResult = omrEngine.GenerateTemplate(ms, Encoding.ASCII);
};

JSON markup

Aspose.OMR JSON markup is an easy-to-read, open standard format that supports syntax highlighting, automatic formatting, and code folding in all popular code editors.

You can pass the template source code to the generator in a number of ways:

As a path to the JSON file

The easiest way is to call the GenerateJSONTemplate method with a relative or absolute path to the file with the source code:

Aspose.OMR.Generation.GenerationResult generationResult = omrEngine.GenerateJSONTemplate("source.json");

It will create a printable form for A4 (210 x 297 mm) paper in portrait (vertical) orientation using the default font and colors. Note, that if the form contains images, they will not be rendered through the basic call and you will get an error.

Optionally, you can customize the paper size, orientation, font, and other layout settings that apply to all template pages, as well as specify paths to images used in the form by passing GlobalPageSettings object to the method:

string workingDirectory = System.IO.Directory.GetCurrentDirectory();
Aspose.OMR.Generation.GlobalPageSettings globalPageSettings = new Aspose.OMR.Generation.GlobalPageSettings();
globalPageSettings.PaperSize = Aspose.OMR.Generation.PaperSize.Letter;
globalPageSettings.ImagesPaths = new string[] {
	System.IO.Path.Combine(workingDirectory, "aspose-logo.png")
};
Aspose.OMR.Generation.GenerationResult generationResult = omrEngine.GenerateJSONTemplate("source.json", globalPageSettings);

You can also define the encoding of a JSON file. By default, the API treats the source file as UTF-8 encoded.

Aspose.OMR.Generation.GenerationResult generationResult = omrEngine.GenerateJSONTemplate("source.json", Encoding.ASCII);

As a string containing JSON markup data

Pass the source code as a string to the GenerateJSONTemplateFromString method:

string sourceCode = @"{
	""element_type"": ""Template"",
	""children"": [
		{
			""element_type"": ""Page"",
			""children"": [
				{
					""element_type"": ""Container"",
					""name"": ""Example"",
					""children"": [
						{
							""element_type"": ""Block"",
							""border"": ""square"",
							""children"": [
								{
									""element_type"": ""Content"",
									""name"": ""JSON markup example""
								}
							]
						}
					]
				}
			]
		}
	]
}";
Aspose.OMR.Generation.GenerationResult generationResult = omrEngine.GenerateJSONTemplateFromString(sourceCode);

It will create a printable form for A4 (210 x 297 mm) paper in portrait (vertical) orientation using the default font and colors. Note, that if the form contains images, they will not be rendered through this call and you will get an error.

Optionally, you can customize the paper size, orientation, font, and other layout settings that apply to all template pages, as well as specify paths to images used in the form by passing GlobalPageSettings object to the GenerateJSONTemplateFromString method:

string sourceCode = @"{
	""element_type"": ""Template"",
	""children"": [
		{
			""element_type"": ""Page"",
			""children"": [
				{
					""element_type"": ""Container"",
					""name"": ""Example"",
					""children"": [
						{
							""element_type"": ""Block"",
							""border"": ""square"",
							""children"": [
								{
									""element_type"": ""Content"",
									""name"": ""JSON markup example""
								}
							]
						}
					]
				}
			]
		}
	]
}";
string workingDirectory = System.IO.Directory.GetCurrentDirectory();
Aspose.OMR.Generation.GlobalPageSettings globalPageSettings = new Aspose.OMR.Generation.GlobalPageSettings();
globalPageSettings.PaperSize = Aspose.OMR.Generation.PaperSize.Letter;
globalPageSettings.ImagesPaths = new string[] {
	System.IO.Path.Combine(workingDirectory, "aspose-logo.png")
};
Aspose.OMR.Generation.GenerationResult generationResult = omrEngine.GenerateJSONTemplateFromString(sourceCode, globalPageSettings);

Alternatively, you can load images used in a form from memory through a special Aspose.OMR.Api.ImageCollection object - a collection of key-value pairs where the key contains the image file name and the value contains the binary content (System.IO.MemoryStream) of the image file. This parameter always takes precedence over page settings - if two images with the same file name are defined in both parameters, the one from the page settings will be ignored.

string sourceCode = @"{
	""element_type"": ""Template"",
	""children"": [
		{
			""element_type"": ""Page"",
			""children"": [
				{
					""element_type"": ""Container"",
					""name"": ""Example"",
					""children"": [
						{
							""element_type"": ""Block"",
							""border"": ""square"",
							""children"": [
								{
									""element_type"": ""Content"",
									""name"": ""JSON markup example""
								}
							]
						}
					]
				}
			]
		}
	]
}";
Aspose.OMR.Generation.GlobalPageSettings globalPageSettings = new Aspose.OMR.Generation.GlobalPageSettings();
globalPageSettings.PaperSize = Aspose.OMR.Generation.PaperSize.Letter;
Aspose.OMR.Api.ImageCollection images = new Aspose.OMR.Api.ImageCollection();
images.Add("aspose-logo.png", new MemoryStream(File.ReadAllBytes(@"form\images\aspose-logo.png")));
Aspose.OMR.Generation.GenerationResult generationResult = omrEngine.GenerateJSONTemplateFromString(sourceCode, globalPageSettings, images);

Building forms programmatically

The contents of an OMR form can be described directly in the application code.

To generate a printable form, pass the corresponding TemplateConfig object to the GenerateJSONTemplate method along with GlobalPageSettings object that defines the paper size, orientation, font, images, and other layout settings that apply to all template pages:

string workingDirectory = System.IO.Directory.GetCurrentDirectory();
Aspose.OMR.Generation.GlobalPageSettings globalPageSettings = new Aspose.OMR.Generation.GlobalPageSettings();
globalPageSettings.PaperSize = Aspose.OMR.Generation.PaperSize.Letter;
globalPageSettings.ImagesPaths = new string[] {
	System.IO.Path.Combine(workingDirectory, "aspose-logo.png")
};
Aspose.OMR.Generation.GenerationResult generationResult = omrEngine.GenerateTemplate(templateConfig, globalPageSettings);

Alternatively, you can load images used in a form from memory through a special Aspose.OMR.Api.ImageCollection object - a collection of key-value pairs where the key contains the image file name and the value contains the binary content (System.IO.MemoryStream) of the image file. This parameter always takes precedence over page settings - if two images with the same file name are defined in both parameters, the one from the page settings will be ignored.

Aspose.OMR.Generation.GlobalPageSettings globalPageSettings = new Aspose.OMR.Generation.GlobalPageSettings();
globalPageSettings.PaperSize = Aspose.OMR.Generation.PaperSize.Letter;
Aspose.OMR.Api.ImageCollection images = new Aspose.OMR.Api.ImageCollection();
images.Add("aspose-logo.png", new MemoryStream(File.ReadAllBytes(@"form\images\aspose-logo.png")));
Aspose.OMR.Generation.GenerationResult generationResult = omrEngine.GenerateTemplate(templateConfig, globalPageSettings, images);

Read more

  • Page setup - customizing the paper size, orientation, font, images, and other layout settings of a printable form.
  • Adding images - including images in Aspose.OMR forms.
  • Error handling - debugging and identifying errors in the source code of the Aspose.OMR template during generation.
  • Saving to file - saving a generated printable form and a recognition pattern file on disk.