[SerializableAttribute()]
[FlagsAttribute()]
public enum OcrXmlOutputOptions
Value | Member | Description |
---|---|---|
0x00000000 | None | Default. Write the recognized word values in the result XML data. |
0x00000001 | Characters | Write the recognized character values instead of the word values in the result XML data |
0x00000002 | CharacterAttributes | Only valid with Characters. Write the character attributes (font for example) in the result XML data. |
The various IOcrDocument.SaveXml methods accept a combination of one or more of the OcrXmlOutputOptions enumeration members to control the format of the output XML data.
The format of the result XML data is as follows:
<?xml version="1.0" encoding="UTF-16" standalone="yes"?> <pages> <page> <zone> <paragraph> <line> <word> <character/> <character/> </word> </line> </paragraph> </zone> </page> </pages>
The pages
element is repeated once per document and it has no value and no additional attributes.
The page
element is repeated for every page in the document (IOcrDocument.Pages.Count). If this page has not been recognized or contains no zones, then the page
element will not contain any child zone
elements.
The page
element has no value and contains the following additional attributes:
Attribute | Value |
---|---|
horizontal_resolution |
Horizontal resolution of the page. The value is IOcrPage.DpiX. |
vertical_resolution |
Vertical resolution of the page. The value is IOcrPage.DpiY. |
width |
Width of the page in pixels. The value is IOcrPage.Width. |
height |
Height of the page in pixels. The value is IOcrPage.Height. |
The zone
element is repeated for every zone in the current page (IOcrPage.Zones). The zone
element has no value and contains the following additional attributes:
Attribute | Value |
---|---|
type |
The zone type. Either "text" or "graphics". If the zone element is of type "text", then it will contain zero or more paragraph child elements. If the zone is of type "graphics", then it will not contain and other child elements. |
left |
The zone left position in pixels. The value is OcrZone.Bounds.Left converted to pixels. |
top |
The zone top position in pixels. The value is OcrZone.Bounds.Top converted to pixels. |
right |
The zone right position in pixels. The value is OcrZone.Bounds.Right converted to pixels. |
bottom |
The zone bottom position in pixels. The value is OcrZone.Bounds.Bottom converted to pixels. |
subtype |
The zone type. The value is OcrZone.ZoneType. |
The paragraph
element is repeated for every text paragraph in the current zone and it has no attributes. If this zone has no recognition text, then the paragraph
element will not contain any child line
elements.
The paragraph
element has no attributes and no value.
The line
element is repeated for every line of text in the current paragraph. The line
element has no value and contains the following additional attributes:
Attribute | Value |
---|---|
left |
The line left position in pixels. |
top |
The line top position in pixels. |
right |
The line right position in pixels. |
bottom |
The line bottom position in pixels. The value of left , top , right and bottom is calculated from the summation of all the boundaries of the words that make up this line. |
base |
The position of the baseline of this line. The value is calculated from the summation of the baselines of all the words that make up this line. |
The word
element is repeated for every word of text in the current line. If OcrXmlOutputOptions.Characters was not specified in the generation options; then the word
element will contain the value of the word as its value. Otherwise, the word
element will contain no value.
The word
element has the following attributes:
Attribute | Value |
---|---|
left |
The word left position in pixels. |
top |
The word top position in pixels. |
right |
The word right position in pixels. |
bottom |
The word bottom position in pixels. The value of left , top , right and bottom is calculated from the summation of all the boundaries of the characters that make up this word. |
base |
The position of the baseline of this word. The value is calculated from the summation of the baselines of all the characters that make up this word. |
The character
element is repeated for every character in the following word only if OcrXmlOutputOptions.Characters was specified in the generation options. Otherwise, the word
element will contain no child character
elements. If OcrXmlOutputOptions.Characters was specified in the generation options; then the character
element will contain the value of the character as its value. Otherwise, the character
element will contain no value.
The character
element contains the following additional attributes:
Attribute | Value |
---|---|
left |
The character left position in pixels. |
top |
The character top position in pixels. |
right |
The character right position in pixels. |
bottom |
The character bottom position in pixels. The value of left , top , right and bottom is calculated from OcrCharacter.Bounds. |
base |
The position of the baseline of this character. The value is OcrCharacter.Base. |
confidence |
The confidence of this character. The value is OcrCharacter.Confidence. |
font_size |
The font size in points. The value is OcrCharacter.FontSize. Only available if OcrXmlOutputOptions.CharacterAttributes is specified. |
proportional |
"yes" if the character font is proportional, "no"; otherwise. The value is calculated from OcrCharacter.FontStyle. Only available if OcrXmlOutputOptions.CharacterAttributes is specified. |
serif |
"yes" if the character font is serif, "no"; otherwise. The value is calculated from OcrCharacter.FontStyle. Only available if OcrXmlOutputOptions.CharacterAttributes is specified. |
bold |
"yes" if the character font is bold, "no"; otherwise. The value is calculated from OcrCharacter.FontStyle. Only available if OcrXmlOutputOptions.CharacterAttributes is specified. |
italic |
"yes" if the character font is italic, "no"; otherwise. The value is calculated from OcrCharacter.FontStyle. Only available if OcrXmlOutputOptions.CharacterAttributes is specified. |
underline |
"yes" if the character font is underline, "no"; otherwise. The value is calculated from OcrCharacter.FontStyle. Only available if OcrXmlOutputOptions.CharacterAttributes is specified. |
The following is an example of the XML output when OcrXmlOutputOptions.None is specified:
<?xml version="1.0" encoding="UTF-16" standalone="yes"?> <pages> <page horizontal_resolution="300" vertical_resolution="300" width="2544" height="3294"> <zone type="Text" left="371" top="370" right="831" bottom="420" subtype="Text" recognition_module="Auto" fill_method="Default"> <paragraph> <line left="372" top="371" right="830" bottom="419" base="29"> <word left="372" top="371" right="554" bottom="409" base="30">License</word> <word left="570" top="372" right="830" bottom="419" base="29">Agreement</word> </line> </paragraph> </zone> </page> </pages>
Here is the same XML output when OcrXmlOutputOptions.Characters is specified:
<?xml version="1.0" encoding="UTF-16" standalone="yes"?> <pages> <page horizontal_resolution="300" vertical_resolution="300" width="2544" height="3294"> <zone type="Text" left="371" top="370" right="831" bottom="420" subtype="Text" recognition_module="Auto" fill_method="Default"> <paragraph> <line left="372" top="371" right="830" bottom="419" base="29"> <word left="372" top="371" right="554" bottom="409" base="30"> <character left="372" top="372" right="398" bottom="408" base="36" confidence="100">L</character> <character left="402" top="371" right="409" bottom="408" base="37" confidence="100">i</character> <character left="414" top="381" right="438" bottom="409" base="27" confidence="100">c</character> <character left="442" top="381" right="468" bottom="409" base="27" confidence="100">e</character> <character left="472" top="381" right="496" bottom="408" base="27" confidence="100">n</character> <character left="501" top="381" right="525" bottom="408" base="27" confidence="100">s</character> <character left="529" top="381" right="554" bottom="408" base="27" confidence="100">e</character> </word> <word left="570" top="372" right="830" bottom="419" base="29"> <character left="570" top="372" right="604" bottom="408" base="36" confidence="100">A</character> <character left="607" top="381" right="633" bottom="419" base="27" confidence="100">g</character> <character left="639" top="381" right="655" bottom="408" base="27" confidence="100">r</character> <character left="657" top="381" right="682" bottom="408" base="27" confidence="100">e</character> <character left="685" top="381" right="710" bottom="408" base="27" confidence="100">e</character> <character left="715" top="381" right="753" bottom="408" base="27" confidence="100">m</character> <character left="758" top="381" right="783" bottom="408" base="27" confidence="100">e</character> <character left="788" top="381" right="812" bottom="408" base="27" confidence="100">n</character> <character left="815" top="374" right="830" bottom="408" base="34" confidence="100">t</character> </word> </line> </paragraph> </zone> </page> </pages>
You can use the OcrXmlOutputOptions.CharacterAttributes option along with IOcrDocumentManager.GetFontNames to obtain the font family name of each character. When performing OCR, the engine cannot distinguish similar fonts such as Arial and Calibri, instead, the engine gets information on whether the character has serif and whether the font is proportional or fixed.
According to GetFontNames, the returned value is an array of fonts as follows:
Index | Description |
---|---|
0 | The font used with proportional serif characters |
1 | The font used with proportional sans-serif characters |
2 | The font used with monospaced serif characters |
3 | The font used with monospaced sans-serif characters |
4 | The font used with ICR (hand-written) characters |
5 | The font used with MICR (check font) characters |
We are only interested in the first four values and with this information. As described above, using the CharacterAttributes option will add the following attributes to the element of each character in the XML: "proportional, serif, bold, italic". With this information and the font names obtained from GetFontNames, you can easily conclude the font family name. The following code snippet accomplishes this:
private static void ShowFontNames(IOcrDocumentManager ocrDocumentManager, string xmlFileName)
{
// Get the Latin font names
string[] fontNames = ocrDocumentManager.GetFontNames("en");
// Note, you can also use a fixed array for most cases. The font names are not changed by the OCR engines
// unless changed manually by the user:
/*
string[] fontNames =
{
"Times New Roman", // Proportional Serif
"Arial", // Proportional Sans-Serif
"Courier New", // Monospace Serif
"Arial" // Monospace Sans-Serif
};
*/
// Read the XML file
XPathDocument xmlDoc = new XPathDocument(xmlFileName);
XPathNavigator nav = xmlDoc.CreateNavigator();
XPathNodeIterator charactersIter = nav.Select(@"//character");
while(charactersIter.MoveNext())
{
XPathNavigator characterNav = charactersIter.Current;
// Read its attributes
bool isSerif;
bool isProportional;
if(string.Compare(characterNav.GetAttribute("serif", characterNav.NamespaceURI), "yes", StringComparison.InvariantCultureIgnoreCase) == 0)
isSerif = true;
else
isSerif = false;
if(string.Compare(characterNav.GetAttribute("proportional", characterNav.NamespaceURI), "yes", StringComparison.InvariantCultureIgnoreCase) == 0)
isProportional = true;
else
isProportional = false;
// Now find the font based on this info
int index;
if(isProportional && isSerif)
index = 0;
else if(isProportional && !isSerif)
index = 1;
else if(!isProportional && isSerif)
index = 2;
else
index = 3;
string fontName = fontNames[index];
Console.WriteLine("Character: {0} font: {1}", characterNav.Value, fontName);
}
}
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Ocr;
using Leadtools.Forms.Common;
using Leadtools.WinForms;
using Leadtools.Drawing;
public void SaveAsXmlExample()
{
string tifFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif");
string xmlFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.xml");
// Create an instance of the engine
using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD))
{
// Start the engine using default parameters
ocrEngine.Startup(null, null, null, LEAD_VARS.OcrLEADRuntimeDir);
// Create an OCR document
using (IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument())
{
// Add this image to the document
IOcrPage ocrPage = ocrDocument.Pages.AddPage(tifFileName, null);
// Recognize it
ocrPage.Recognize(null);
// Save the result XML to a disk file
// Only save the words not characters, if we want the characters
// we need to change the options to OcrXmlOutputOptions.Characters
ocrDocument.SaveXml(xmlFileName, OcrXmlOutputOptions.None);
}
// Shutdown the engine
// Note: calling Dispose will also automatically shutdown the engine if it has been started
ocrEngine.Shutdown();
}
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images";
public const string OcrLEADRuntimeDir = @"C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime";
}