FileInputStream fileInputStream = new FileInputStream(filePath);
XWPFDocument document = new XWPFDocument(fileInputStream);
for (XWPFParagraph p : document.getParagraphs()) {
Boolean isPrg=true;
XmlCursor cursor = p.getCTP().newCursor();
cursor.selectPath(
"declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//*/w:txbxContent/w:p/w:r");
List<XmlObject> ctrsintxtbx = new ArrayList<XmlObject>();
while (cursor.hasNextSelection()) {
cursor.toNextSelection();
XmlObject obj = cursor.getObject();
ctrsintxtbx.add(obj);
}
for (XmlObject obj : ctrsintxtbx) {
CTR ctr = CTR.Factory.parse(obj.newInputStream());
XWPFRun bufferrun = new XWPFRun(ctr, (IRunBody) p);
String text = bufferrun.getText(0);
if (text != null && text != "") {
isPrg= false;
text = text.replace("[RECEPIENT_NAME]", name).replace("[ADDRESS1]", address1).replace("LETTER_GEN_DT", date)
.replace("[ID]",sbsbid).replace("MEMR_RX_ID", memrid)
.replace("MEMR_MCTR_RX_GROUP", memrgroup).replace("MEMR_MCTR_RXBIN", memrrxbin)
.replace("MEMR_MCTR_PCN", memrpcn);
System.out.println("header line is "+ text);
if((address2 == null || address2.equals("")) && (address3 == null || address3.equals(""))) {
text = text.replace("[ADDRESS2]",city+", "+state+" "+zip).replace("[ADDRESS3]","").replace("[ZIP]", "").replace("[CITY]", "").replace("[STATE]", "");
}
else if((address2 != null && !address2.equals("")) && (address3 == null || address3.equals(""))) {
text = text.replace("[ADDRESS2]",address2).replace("[ADDRESS3]",city+", "+state+" "+zip).replace("[ZIP]", "").replace("[CITY]", "").replace("[STATE]", "");
}
else if((address2 == null || address2.equals("")) && (address3 != null && !address3.equals(""))) {
text = text.replace("[ADDRESS2]",address3).replace("[ADDRESS3]",city+", "+state+" "+zip).replace("[ZIP]", "").replace("[CITY]", "").replace("[STATE]", "");
}
else if((address2 != null && !address2.equals("")) && (address3 != null && !address3.equals(""))) {
text = text.replace("[ADDRESS2]",address2).replace("[ADDRESS3]",address3).replace("[ZIP]",zip).replace("[CITY]", city).replace("[STATE]", state);
}
bufferrun.setText(text,0);
}
obj.set(bufferrun.getCTR());
}
if(isPrg) {
String runTxt="";
p.getCTP().documentProperties();
for(int i = 0; i < p.getRuns().size(); i++)
{
runTxt=runTxt+ p.getRuns().get(i);
}
System.out.println("main footer is :"+runTxt);
runTxt=runTxt.replace("[RECEPIENT_NAME]", name).replace("[PDP_NAME]", pdpname)
.replace("[ENRL_EFF_DT]", enrolldate).replace("[GLBL_PHONE_NUMBER]", phoneNumber);
int psize= p.getRuns().size();
if(psize>1) {
for(int i = 0; i < psize-1; i++)
{
try
{
p.removeRun(psize - i - 1);
}
catch(Exception e)
{
//e.printStackTrace();
}
}
}
for(XWPFRun ffr:p.getRuns()) {
ffr.setText(runTxt,0);
break;
}
}
}
outFilePath = filePath.replace(".docx", "_convert.docx");
FileOutputStream out = new FileOutputStream(
new File(outFilePath));
document.write(out);
out.close();
System.out.println("Output.docx written successully");
} catch (IOException | SQLException e) {
System.out.println("We had an error while reading the Word Doc");
}
return outFilePath;
}
Input Text :
will automatically terminate without notice when the base Contract terminates for any reason
Output i am getting like this:
will automatically terminate without notice when the base Contract terminates for any reason
For output also i need Bold text format after replacing the words in documents
Expected Output is :
will automatically terminate without notice when the base Contract terminates for any reason
Related
I am working on a project where I need to compare 2 CSVs for equality. Its just a single row in each CSV with multiple columns and report the differences in form of columna name and value which are different. For eg :
CSV 1 :
Name Roll No Dept
Brij 1 IT
CSV 2 :
Name Roll No Dept
Brij 2 IT
I need to report the diff as
Roll No 1 does not match Roll No 2 i.e. Col name and value in both csvs
My work till now :
#Test
public void f() {
try {
FileReader reader1 = new FileReader(
new File(System.getProperty("user.dir") + "\\src\\test\\resources\\csvFile\\Reader.csv"));
FileReader reader2 = new FileReader(
new File(System.getProperty("user.dir") + "\\src\\test\\resources\\csvFile\\Reader - Copy.csv"));
CSVReader csvReader1 = new CSVReader(reader1);
CSVReader csvReader2 = new CSVReader(reader2);
String[] nextRecordReader1;
String[] nextRecordReader2;
// we are going to read data line by line
while ((nextRecordReader1 = csvReader1.readNext()) != null
&& (nextRecordReader2 = csvReader2.readNext()) != null) {
checkEquality(nextRecordReader1, nextRecordReader2);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void checkEquality(String[] s1, String[] s2) {
if (s1 == s2) {}
if (s1 == null || s2 == null) {}
int n = s1.length;
if (n != s2.length) {}
for (int i = 0; i < n; i++) {
if (!s1[i].trim().equals(s2[i].trim())) {
System.out.println(s1[i] + " is not equal to " + s2[i]);
}
}
}
This gives me the below result :
1 is not equal to 2
I am not able to mention the column name. How shall I do that ?
public void checkEquality(String[] s1, String[] s2) {
if (s1 == s2) {}
if (s1 == null || s2 == null) {}
String[] headers= new String[]{"Name","Roll No","Dept"};
int n = s1.length;
if (n != s2.length) {}
for (int i = 0; i < n; i++) {
if (!s1[i].trim().equals(s2[i].trim())) {
System.out.println(headers[i]+" "+s1[i] + " is not equal to " headers[i]+" "+ s2[i]);
}
}
}
You need a collection to hold column headers, and use this collection to get header based on index.
enter code here
public class SampleTest {
int rowCountCounter;
String[] headers1;
String[] headers2;
#Test
public void f() {
try {
FileReader reader1 = new FileReader(
new File(System.getProperty("user.dir") + "\\src\\test\\resources\\csvFile\\Reader.csv"));
FileReader reader2 = new FileReader(
new File(System.getProperty("user.dir") + "\\src\\test\\resources\\csvFile\\Reader - Copy.csv"));
CSVReader csvReader1 = new CSVReader(reader1);
CSVReader csvReader2 = new CSVReader(reader2);
String[] nextRecordReader1;
String[] nextRecordReader2;
rowCountCounter=0;
// we are going to read data line by line
while ((nextRecordReader1 = csvReader1.readNext()) != null
&& (nextRecordReader2 = csvReader2.readNext()) != null) {
if(rowCountCounter==0) {
headers1=nextRecordReader1;
headers2=nextRecordReader2;
}
checkEquality(nextRecordReader1, nextRecordReader2);
rowCountCounter++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void checkEquality(String[] s1, String[] s2) {
if (s1 == s2) {}
if (s1 == null || s2 == null) {}
int n = s1.length;
if (n != s2.length) {
System.out.println("Header in CSV1 " + n + " is not equal to header in CSV2 " + s2.length);
}
boolean headerFlag=true;
for (int i = 0; i < n; i++) {
if(rowCountCounter==0) {
if (!s1[i].trim().equals(s2[i].trim())) {
System.out.println("CSV1 header "+s1[i] + " is not equal to CSV2 header " + s2[i]);
headerFlag=false;
}
}else {
if (!s1[i].trim().equals(s2[i].trim())) {
System.out.println("CSV1 "+headers1[i] +" cellvalue "+" \"" +s1[i] +"\"" +" is not equal to CSV2 " + headers2[i]+" value \""+s2[i]+"\"");
headerFlag=false;
}
// else {
// System.out.println("CSV data is equal");
// }
}
}
if(headerFlag==true && rowCountCounter==0) {
System.out.println("CSV headers are equal.Below are the headers");
for(int k=0;k<s1.length;k++) {
System.out.print(s1[k]+" ");
}
}
if(headerFlag==true && rowCountCounter!=0) {
System.out.println("CSV data is equal.");
}
}
}
I want to pre-populate my database. So I generate SQL queries from browser and write code to insert them in db. It is working fine for the single line query but most insert queries have multiple line for example `
INSERT INTO `poem` VALUES (780,'سلام القلب واشواقه
من قلب يحب مجنونه
ياليت هالقلب يحن
ويقول هالدنيا
ماتسوى دون *_*','0',NULL);`
My requirement is to insert them as they are. That is the method i wrote for multiple line (works perfect for single line query)
public static int countLines(InputStream is) throws IOException {
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
boolean empty = true;
int index = 0;
while ((readChars = is.read(c)) != -1) {
empty = false;
String temp = new String(c);
for (int i = 0; i < readChars; i++) {
if (c[i] == ';' && index == 0) {
index++;
} else if (c[i] == '\n' && index == 1) {
index++;
} else if (c[i] == '\t' && index == 2) {
index++;
} else if (c[i] == 'I' && index == 3) {
count++;
index = 0;
} else {
i -= index;
index = 0;
}
}
}
return (count == 0 && !empty) ? 1 : count + 1;
} finally {
is.close();
}
}
Anyone have idea how to insert such queries from file to DB.?
Any help would be great. Thanks
What you are asking is an ugly way of doing things. Use sqliteassethelper to create prepopulated databases and tables. It works extremely similar to SQLiteOpenHelper so it is very easy to use.
Sorry for late response in case of anyone having same issue
Here is the solution step by step
I create a .sql file which contain insert queries (some Include multiline queries) ends with :.
In my onCreate() method of SQLiteOpenHelper I m counting lines of my file using getFileLineCount(). This method return me the count of line. This line count is never used , but I had it because if all your queries are single line, it might comes into play. You can skip this method.
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE \"" + TABLE_POEM + "\" (\"" + POEM_LOCAL_ID + "\" INTEGER PRIMARY KEY AUTOINCREMENT , \"" + POEM_TEXT + "\" TEXT,\"" + POEM_ID + "\" INTEGER , \"" + POEM_IS_FAV + "\" TEXT);");
int totalLineCount = getFileLineCount("poem.sql");
int insertCount = insertFromFile(db, "poem.sql", totalLineCount);
Log.d("DatabaseHelper", "------------onCreate(SQLiteDatabase db) >>>>>>> completed--------");
}
private int getFileLineCount(String assetFilePath) {
InputStream insertStream = null;
int totalCount = 0;
try {
insertStream = context.getAssets().open(assetFilePath);
totalCount = FileUtil.countLines(new BufferedInputStream(insertStream));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (insertStream != null) {
try {
insertStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return totalCount;
}
By using this method I insert queries from file. It handles both single line queries and multi line queries. First I check if line contain ";" it means the line is ended else not ended. Pretty simple it was at the end.
while ((currentLine = insertReader.readLine()) != null) {
if (currentLine.length() != 0) {
if (currentLine.charAt(currentLine.length() - 1) == ';') {
insertInDb(db, insertStr + "\n" + currentLine);
insertStr = "";
} else {
insertStr += currentLine;
}
}
}
private int insertFromFile(SQLiteDatabase db, String assetFilePath, int totalCount) {
int result = 0;
BufferedReader insertReader = null;
try {
InputStream insertStream = context.getAssets().open(assetFilePath);
insertReader = new BufferedReader(new InputStreamReader(insertStream));
db.beginTransaction();
while (insertReader.ready()) {
// String insertStr = insertReader.toString();
String insertStr = "";
String currentLine;
while ((currentLine = insertReader.readLine()) != null) {
if (currentLine.length() != 0) {
if (currentLine.charAt(currentLine.length() - 1) == ';') {
insertInDb(db, insertStr + "\n" + currentLine);
insertStr = "";
} else {
insertStr += currentLine;
}
}
}
}
db.setTransactionSuccessful();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (insertReader != null) {
try {
insertReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
db.endTransaction();
}
return result;
}
private void insertInDb(SQLiteDatabase db, String assetFilePath) throws IOException {
if (assetFilePath != null && assetFilePath.length() > 0) {
SQLiteStatement statement = db.compileStatement(assetFilePath);
statement.execute();
}
}
I used this technique to insert 4000 records in db. And it was working fine without much of the lag. If anyone have any batter solution for it. Kindly post it.
Here I am able to merge/span the cell using 'setColumnSpannedNumber()' but could not set background color of the cell and alignment.I am using odfdom-java-0.8.6.jar currently..please suggest me a way to set the color for the cells. Thank you.
try
{
document = OdfSpreadsheetDocument.newSpreadsheetDocument();
OdfOfficeSpreadsheet contentRoot = document.getContentRoot();
Node node = contentRoot.getFirstChild();
while (node != null) {
contentRoot.removeChild(node);
node = contentRoot.getFirstChild();
}
} catch (Exception e) {
signature throws Exception
throw new ReportFileGenerationException("Cannot create new ODF spread sheet document. Error: "+ e.getMessage(), e);
}
OdfTable table = OdfTable.newTable(document);
for (int i = 0; i < report.size(); i++) {
List<String> row = report.get(i);
for (int j = 0; j < row.size(); j++) {
String str= row.get(j);
String newStr = str.replaceAll("[\u0000-\u001f]", "");
OdfTableCell cell = table.getCellByPosition(j, i);
if(i==0 && j==17)
{
cell.setColumnSpannedNumber(4);
cell.setCellBackgroundColor(new Color("#ffff00"));
cell.setHorizontalAlignment("center");
}
else if(i==0 && j==21)
{
cell.setColumnSpannedNumber(4);
}
else if(i==0 && j==25)
{
cell.setColumnSpannedNumber(4);
}
else if(i==0 && j==29)
{
cell.setColumnSpannedNumber(4);
}
else if(i==0 && j==33)
{
cell.setColumnSpannedNumber(4);
}
cell.setStringValue(newStr);
}
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
document.save(os);
return os.toByteArray();
} catch (Exception e) {
throw new ReportFileGenerationException("Cannot save the ODF spread sheet document as byte array. Error: "
+ e.getMessage(), e);
} finally {
Helper.close(os);
}
}
}
I use the API property CellBackColor, not CellBackgroundColor.
Also HoriJustify rather than HorizontalAlignment.
At least in StarBasic, this is how I set background colors:
Dim Yellow As Long : Yellow = 16777113
Dim Blue As Long : Blue = 13434879
Dim White As Long : White = -1
Dim Red As Long : Red = 15425853
cell.setCellBackColor(Yellow)
If I want a new color, I manually recolor the background and then use a macro to read out the Long value associated with that color.
And to center align:
cell.setHoriJustify(2)
The background info here is that I have a working Indexer and Search (in java) that indexes and searches a file directory for the filenames and then copies the files to a "Results" Directory.
What I need/ don't have much experience in is writing jsp files. I need the jsp file to have a search bar for the text and then a search button. When text is entered in the bar, and the button is clicked, I need it to run my search program with the entered text as an arg.
I have added the IndexFiles and the SearchFiles classes for reference.
Please explain with a good example if you can help out!
public class SearchFiles {
static File searchDirectory = new File(
"C:\\Users\\flood.j.2\\Desktop\\IndexSearch\\Results");
static String v = new String();
static String path = null;
String title = null;
File addedFile = null;
OutputStream out = null;
String dirName = "C:\\Users\\flood.j.2\\Desktop\\IndexSearch\\Results";
public static void main(String[] args) throws Exception {
String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string]";
if (args.length > 0
&& ("-h".equals(args[0]) || "-help".equals(args[0]))) {
System.out.println(usage);
System.exit(0);
}
for (int j = 5; j < args.length; j++) {
v += args[j] + " ";
}
String index = "index";
String field = "contents";
String queries = null;
boolean raw = false;
String queryString = null;
int hits = 100;
for (int i = 0; i < args.length; i++) {
if ("-index".equals(args[i])) {
index = args[i + 1];
i++;
} else if ("-field".equals(args[i])) {
field = args[i + 1];
i++;
} else if ("-queries".equals(args[i])) {
queries = args[i + 1];
i++;
} else if ("-query".equals(args[i])) {
queryString = v;
i++;
}
}
IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(
index)));
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
BufferedReader in = null;
if (queries != null) {
in = new BufferedReader(new InputStreamReader(new FileInputStream(
queries), "UTF-8"));
} else {
in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
}
QueryParser parser = new QueryParser(Version.LUCENE_40, field, analyzer);
for (int m = 0; m < 2; m++) {
if (queries == null && queryString == null) {
System.out.println("Enter query: ");
}
String line = queryString != null ? queryString : in.readLine();
if (line == null || line.length() == -1) {
break;
}
line = line.trim();
if (line.length() == 0) {
break;
}
Query query = parser.parse(line);
System.out.println("Searching for: " + query.toString(field));
doPagingSearch(in, searcher, query, hits, raw, queries == null
&& queryString == null);
if (queryString == null) {
break;
}
}
reader.close();
}
public static void doPagingSearch(BufferedReader in,
IndexSearcher searcher, Query query, int hitsPerPage, boolean raw,
boolean interactive) throws IOException {
// Collect enough docs to show 500 pages
TopDocs results = searcher.search(query, 5 * hitsPerPage);
ScoreDoc[] hits = results.scoreDocs;
int numTotalHits = results.totalHits;
System.out.println(numTotalHits + " total matching documents");
int start = 0;
int end = Math.min(numTotalHits, hitsPerPage);
FileUtils.deleteDirectory(searchDirectory);
while (true) {
for (int i = start; i < end; i++) {
Document doc = searcher.doc(hits[i].doc);
path = doc.get("path");
if (path != null) {
System.out.println((i + 1) + ". " + path);
File addFile = new File(path);
try {
FileUtils.copyFileToDirectory(addFile, searchDirectory);
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (!interactive || end == 0) {
break;
}
System.exit(0);
}
}
}
public class IndexFiles {
private IndexFiles() {
}
public static void main(String[] args) {
String usage = "java org.apache.lucene.demo.IndexFiles"
+ " [-index INDEX_PATH] [-docs DOCS_PATH] [-update]\n\n"
+ "This indexes the documents in DOCS_PATH, creating a Lucene index"
+ "in INDEX_PATH that can be searched with SearchFiles";
String indexPath = null;
String docsPath = null;
boolean create = true;
for (int i = 0; i < args.length; i++) {
if ("-index".equals(args[i])) {
indexPath = args[i + 1];
i++;
} else if ("-docs".equals(args[i])) {
docsPath = args[i + 1];
i++;
} else if ("-update".equals(args[i])) {
create = false;
}
}
if (docsPath == null) {
System.err.println("Usage: " + usage);
System.exit(1);
}
final File docDir = new File(docsPath);
if (!docDir.exists() || !docDir.canRead()) {
System.out
.println("Document directory '"
+ docDir.getAbsolutePath()
+ "' does not exist or is not readable, please check the path");
System.exit(1);
}
Date start = new Date();
try {
System.out.println("Indexing to directory '" + indexPath + "'...");
Directory dir = FSDirectory.open(new File(indexPath));
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_40,
analyzer);
if (create) {
iwc.setOpenMode(OpenMode.CREATE);
} else {
iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
}
IndexWriter writer = new IndexWriter(dir, iwc);
indexDocs(writer, docDir);
writer.close();
Date end = new Date();
System.out.println(end.getTime() - start.getTime()
+ " total milliseconds");
} catch (IOException e) {
System.out.println(" caught a " + e.getClass()
+ "\n with message: " + e.getMessage());
}
}
static void indexDocs(IndexWriter writer, File file) throws IOException {
if (file.canRead()) {
if (file.isDirectory()) {
String[] files = file.list();
if (files != null) {
for (int i = 0; i < files.length; i++) {
indexDocs(writer, new File(file, files[i]));
}
}
} else {
FileInputStream fis;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException fnfe) {
return;
}
try {
Document doc = new Document();
Field pathField = new StringField("path",
file.getAbsolutePath(), Field.Store.YES);
doc.add(pathField);
doc.add(new LongField("modified", file.lastModified(),
Field.Store.NO));
doc.add(new TextField("title", file.getName(), null));
System.out.println(pathField);
if (writer.getConfig().getOpenMode() == OpenMode.CREATE) {
System.out.println("adding " + file);
writer.addDocument(doc);
} else {
System.out.println("updating " + file);
writer.updateDocument(new Term("path", file.getPath()),
doc);
}
} finally {
fis.close();
}
}
}
}
}
First, you should definitely do this in a servlet rather than a JSP. Putting lots of logic in JSP is bad practice. (See the servlets info page).
Second, it would probably be better on performance to make a cronjob (Linux) or Task (Windows) to run the search program every hour and store the results in a database and just have your servlet pull from there rather than allow the user to initiate the search program.
I'm using flexicapture processor for recognizing my document. I've a case where i've a document with multiple pages i.e. a document have multiple images and each image is need to recognize.
I'm following below procedure to achieve my general task ,either one image in document or multiple image in a document;
create a processor
add document definition file or afl file
run recognition as IDocument document = processor.RecognizeNextDocument();
But when it return a document, document, it has only one page,which is the first page of document, why is it so?
On the other case, if i use project instead processor,IProject, with below procedure
create a project
get batches from project project.getBatches(),
add a document (that have multiple page) to batch
recognize them
The i've all pages information of document,IDocuments documents = batch.getDocuments(),
How i can achieve the same task wit processor? I want processor recognize all pages in a document and return a document with all pages in it. ?
if something is unclear, pls ask for more information.
Please reply asap...
Code :1 using flexicapture processor
/**
*
*/
/**
* #author Nitin
*
*/
import java.sql.BatchUpdateException;
import com.abbyy.FCEngine.*;
public class FlexicaputreVerificationUsingProcessor {
private static Object verificationWorkSet(Object object) {
// TODO Auto-generated method stub
return null;
}
private static void trace( String txt )
{
System.out.println( txt );
}
static private String samplesFolder;
static private String projectFolder;
static private String serialNumber;
static private String dllPath;
static {
samplesFolder = "C:\\ProgramData\\ABBYY\\SDK\\10\\FlexiCapture Engine\\Samples\\";
projectFolder = "C:\\Users\\Nitin\\FlexicaptureTest\\flexiverificationtest" ;
try {
java.io.FileInputStream file = new java.io.FileInputStream( samplesFolder + "SampleConfig\\SamplesConfig.txt" );
java.io.BufferedReader reader = new java.io.BufferedReader( new java.io.InputStreamReader( file ) );
serialNumber = reader.readLine();
dllPath = reader.readLine();
file.close();
} catch( java.io.IOException e ) {
System.out.println( e.getMessage() );
e.printStackTrace();
}
}
/**
* #param args
*/
public static void main(String[] args) {
// Load Engine
try {
trace("Loading engine");
IEngineLoader engineLoader= Engine.CreateEngineOutprocLoader();
IEngine engine = engineLoader.Load(serialNumber,dllPath);
try {
// Create and configure FlexiCaptureProcessor
trace("Creating and configureing FlexiCaptureProcessor");
IFlexiCaptureProcessor processor = engine.CreateFlexiCaptureProcessor();
processor.AddDocumentDefinitionFile( projectFolder + "\\Document_Definition_1.fcdot" );
trace("Adding images/pdf to processor");
final int fileCount = 1 ;
processor.AddImageFile(projectFolder + "\\don't upload to big .pdf");
engine.EnableRecognitionVariants( true );
trace("Creating Document collection");
IDocumentsCollection documentsCollection = engine.CreateDocumentsCollection();
trace( "Reconizing Images/pdfs..." );
int totalErrors = 0 ;
for ( int iterator = 0 ; iterator<fileCount; iterator++ ){
trace("Recongnizing image/pdf number: " +(iterator+1));
IDocument document = processor.RecognizeNextDocument();
trace("Getting last processing error for checksum");
IProcessingError lastProcessingError = processor.GetLastProcessingError() ;
if ( lastProcessingError !=null)
{
String errormsg = lastProcessingError.MessageText();
totalErrors++;
trace("Error occured while recognizeing document, Document number: "+(iterator+1)+ " with Error msg: "+errormsg);
//since we are not handling error (right now) so moving to next document for recognization
processor.ResumeProcessing(false);
}else {
trace("No error occured while recognization of document number : "+(iterator+1));
}
trace("Adding documents in Documents collection");
documentsCollection.Add(document);
}
if ( totalErrors == fileCount){
trace("Facing Error for all document while recongnization");
return ;
}
trace("Creaing Verification session");
try {
IVerificationSession verificationSession = engine.CreateVerificationSession(documentsCollection) ;
try {
//enabling context verification
verificationSession.getOptions().setVerifyFields(true);
//disabling group verification
verificationSession.getOptions().setVerifyBaseSymbols(false);
verificationSession.getOptions().setVerifyExtraSymbols(false);
try {
trace("Get NextWork Set");
IVerificationWorkSet verificationWorkSet = verificationSession.NextWorkSet();
if ( verificationWorkSet == null){
trace("first verificationWork set is null");
}else {
//process each work set in Verification session
trace("Processing Work Set");
while ( verificationWorkSet != null ){
try{
trace("Geting Verification group");
//get next group for verification
IVerificationGroup verificationGroup = verificationWorkSet.NextGroup();
if ( verificationGroup == null ){
trace("First verification group is null");
}else {
trace("processing each group of a workset");
//processing each group of a work set
while ( verificationGroup!= null){
int verificationObjectInAGroupCount = verificationGroup.getCount();
trace("Total number of verification object: " +verificationObjectInAGroupCount);
for ( int iterator = 0; iterator<verificationObjectInAGroupCount; iterator++){
trace ( "getting and Processing "+(iterator +1 ) + " verification object of A group");
//getting verification object
IVerificationObject verificationObject = verificationGroup.getElement(iterator);
if ( verificationObject == null){
trace("verification object is null");
}else {
if ( verificationObject.getType() == VerificationObjectTypeEnum.VOT_Group ) {
IGroupVerificationObject groupVerificationObject = verificationObject.AsGroupVerificationObject();
if ( groupVerificationObject == null){
System.out.println("group verification object is null ");
}
}else if ( verificationObject.getType() == VerificationObjectTypeEnum.VOT_Context) {
IContextVerificationObject contextVerificationObject = verificationObject.AsContextVerificationObject();
if ( contextVerificationObject == null){
trace("ContextVerification object is null");
}else {
IField field = contextVerificationObject.getField();
if ( field == null){
trace("field getting null");
}else {
System.out.println(" field full name: " +field.getFullName() + "\n Name: " +field.getName());
IFieldValue fieldValue = field.getValue();
if ( fieldValue == null){
trace("Field Value is Null");
}else {
trace ( "getting text from field value");
IText text = fieldValue.getAsText() ;
if ( text == null){
trace("text getting null in field value");
}else {
int wordCount = text.getRecognizedWordsCount() ;
trace("recognized word count: "+wordCount);
//getting words from text
for ( int wordIndex = 0 ; wordIndex<wordCount; wordIndex++ ){
trace ("processing word number :" +wordIndex);
IRecognizedWordInfo recognizedWordInfo = engine.CreateRecognizedWordInfo() ;
if ( recognizedWordInfo == null){
trace("Can't create recognizedWordInfo object using engine");
}else {
text.GetRecognizedWord(wordIndex, -1, recognizedWordInfo);
//getting characters from word
for (int characterIndex = 0 ; characterIndex<recognizedWordInfo.getText().length(); characterIndex++ ){
trace("processing character number : " +characterIndex);
IRecognizedCharacterInfo recognizedCharacterInfo = engine.CreateRecognizedCharacterInfo();
if ( recognizedCharacterInfo == null) {
trace("can't create recognizedCharacterInfo object");
}else {
recognizedWordInfo.GetRecognizedCharacter(characterIndex, -1, recognizedCharacterInfo);
System.out.println(" Character: " + recognizedCharacterInfo.getCharacter());
System.out.println(" Confidence level : " +recognizedCharacterInfo.getCharConfidence());
}
}
}
}
}
System.out.println(" Field Value : " +fieldValue.getAsString());
}
}
}
}
}
}
trace("Geting next Verification group");
verificationGroup = verificationWorkSet.NextGroup();
}
}
}catch (Exception e){
trace("Exception occured in getting next work group");
e.printStackTrace();
}
trace("Get next worksets");
//get next work set
verificationWorkSet = verificationSession.NextWorkSet();
}
}
}catch (Exception e){
e.printStackTrace();
}
}finally {
trace("closing Verification object");
verificationSession.Close();
}
} catch (Exception e) {
trace("Exception occured in creating verification sessions");
}
}catch (Exception e){
trace ("Exception occured in");
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally {
trace("unloading Engine");
Engine.Unload();
}
}
}
Code : 2 using project
import java.io.File;
import java.io.IOException;
import java.sql.BatchUpdateException;
import com.abbyy.FCEngine.*;
public class VerificationStep {
//same as above
public static void main( String[] args )
{
// Load Engine
try {
trace("Loading engine");
IEngineLoader engineLoader= Engine.CreateEngineOutprocLoader();
IEngine engine = engineLoader.Load(serialNumber,dllPath);
try{
IProject project = engine.OpenProject( projectFolder + "\\flexitest.fcproj" );
try {
IBatch batch = null ;
trace( "Creating Batch..." );
IBatches batchs = project.getBatches();
if (batchs == null || batchs.getCount() == 0){
batch = project.getBatches().AddNew("TestBatch");
}
batch = batchs.getElement(0);
assert(batch == null);
try{
trace("opening batch");
batch.Open();
trace( "Adding pdfs..." );
batch.AddImage(projectFolder + "\\don't upload to big .pdf");
trace( "Reconizing pdfs..." );
batch.Recognize(null, RecognitionModeEnum.RM_ReRecognizeAll,null);
trace("Creating Verification object");
try {
IVerificationSession verificationSession = project.StartVerification(null);
try {
//enabling context verification
verificationSession.getOptions().setVerifyFields(true);
//disabling group verification
verificationSession.getOptions().setVerifyBaseSymbols(false);
verificationSession.getOptions().setVerifyExtraSymbols(false);
try {
trace("Get NextWork Set");
IVerificationWorkSet verificationWorkSet = verificationSession.NextWorkSet();
if ( verificationWorkSet == null){
trace("first verificationWork set is null");
}else {
//process each work set in Verification session
trace("Processing Work Set");
while ( verificationWorkSet != null ){
try{
trace("Geting Verification group");
//get next group for verification
IVerificationGroup verificationGroup = verificationWorkSet.NextGroup();
if ( verificationGroup == null ){
trace("First verification group is null");
}else {
trace("processing each group of a workset");
//processing each group of a work set
while ( verificationGroup!= null){
int verificationObjectInAGroupCount = verificationGroup.getCount();
trace("Total number of verification object: " +verificationObjectInAGroupCount);
for ( int iterator = 0; iterator<verificationObjectInAGroupCount; iterator++){
trace ( "getting and Processing "+(iterator +1 ) + " verification object of A group");
//getting verification object
IVerificationObject verificationObject = verificationGroup.getElement(iterator);
if ( verificationObject == null){
trace("verification object is null");
}else {
if ( verificationObject.getType() == VerificationObjectTypeEnum.VOT_Group ) {
IGroupVerificationObject groupVerificationObject = verificationObject.AsGroupVerificationObject();
if ( groupVerificationObject == null){
System.out.println("group verification object is null ");
}
}else if ( verificationObject.getType() == VerificationObjectTypeEnum.VOT_Context) {
IContextVerificationObject contextVerificationObject = verificationObject.AsContextVerificationObject();
if ( contextVerificationObject == null){
trace("ContextVerification object is null");
}else {
IField field = contextVerificationObject.getField();
if ( field == null){
trace("field getting null");
}else {
System.out.println(" field full name: " +field.getFullName() + "\n Name: " +field.getName());
IFieldValue fieldValue = field.getValue();
if ( fieldValue == null){
trace("Field Value is Null");
}else {
trace ( "getting text from field value");
IText text = fieldValue.getAsText() ;
if ( text == null){
trace("text getting null in field value");
}else {
int wordCount = text.getRecognizedWordsCount() ;
trace("recognized word count: "+wordCount);
//getting words from text
for ( int wordIndex = 0 ; wordIndex<wordCount; wordIndex++ ){
trace ("processing word number :" +wordIndex);
IRecognizedWordInfo recognizedWordInfo = engine.CreateRecognizedWordInfo() ;
if ( recognizedWordInfo == null){
trace("Can't create recognizedWordInfo object using engine");
}else {
text.GetRecognizedWord(wordIndex, -1, recognizedWordInfo);
//getting characters from word
for (int characterIndex = 0 ; characterIndex<recognizedWordInfo.getText().length(); characterIndex++ ){
trace("processing character number : " +characterIndex);
IRecognizedCharacterInfo recognizedCharacterInfo = engine.CreateRecognizedCharacterInfo();
if ( recognizedCharacterInfo == null) {
trace("can't create recognizedCharacterInfo object");
}else {
recognizedWordInfo.GetRecognizedCharacter(characterIndex, -1, recognizedCharacterInfo);
System.out.println(" Character: " + recognizedCharacterInfo.getCharacter());
System.out.println(" Confidence level : " +recognizedCharacterInfo.getCharConfidence());
}
}
}
}
}
System.out.println(" Field Value : " +fieldValue.getAsString());
}
}
}
}
}
}
verificationGroup = verificationWorkSet.NextGroup();
}
}
}catch (Exception e){
e.printStackTrace();
}
//get next work set
verificationWorkSet = verificationSession.NextWorkSet();
}
}
}catch (Exception e){
e.printStackTrace();
}
}finally {
verificationSession.Close();
}
}catch (Exception e){
e.printStackTrace();
}
trace ("Getting Documents");
IDocuments documents = batch.getDocuments();
trace ("Getting Fields and printing");
for ( int j = 0 ; j < documents.getCount(); j++){
trace ("Getting documnets:" +(j+1));
IDocument document = documents.getElement(j);
IDocumentDefinition definition = document.getDocumentDefinition();
assert( definition != null );
assert( document.getPages().getCount() == 1 );
trace( "DocumentType: " + document.getDocumentDefinition().getName() );
try {
trace("opening document");
document.Open(true);
IFields fields = document.getSections().Item( 0 ).getChildren();
for( int i = 0; i < fields.getCount(); i++ ) {
IField field = fields.getElement( i );
trace( field.getName() + ": " +
( field.getValue() != null ? field.getValue().getAsString() : "." ) );
}
}finally {
trace("closing document");
document.Close(true);
}
}
}finally {
trace("Closing Batch");
batch.Close();
}
}catch (Exception e){
System.out.println("Exception in creating Batch");
e.printStackTrace();
}
finally {
trace("closing project");
project.Close();
}
}catch (Exception e){
System.out.println("Exception occured while loading project");
e.printStackTrace();
}
}catch (Exception e) {
// TODO: handle exception
System.out.println("Exception occured while loading engine");
e.printStackTrace();
}
finally {
trace("unloading Engine");
Engine.Unload();
}
}
}
Finally i got my solution, actually it recognize correctly, i'm handling them wrong way...