using System.Collections.Generic;
using org.apache.pdfbox.pdmodel;
using org.apache.pdfbox.util;
public static void Main()
DataTable dt = new DataTable();
StringBuilder results = new StringBuilder();
List<DBValue> dbValues = new List<DBValue>();
dbValues.Add(new DBValue() { Descriptor = "box1name", Coords = new int[] { 9, 7, 184, 12 }, ExpectedValue = dt.Rows[0].Field<string>("PhyscName") });
dbValues.Add(new DBValue() { Descriptor = "box1address", Coords = new int[] { 14, 17, 178, 17 }, ExpectedValue = dt.Rows[0].Field<string>("PhyscAddress") });
List<BoxName> boxes = new List<BoxName>();
foreach (var dbvalue in dbValues)
boxes.Add(new BoxName(dbvalue.Coords, dbvalue.Descriptor));
foreach (var box in boxes)
var boxData = dbValues.Where(x => x.Descriptor == box.BoxDescriptor).FirstOrDefault();
if (boxData.ExpectedValue != box.ActualValue)
results.AppendFormat("Descriptor {0} failed.\r\nExpectedValue: {1} - ActualValue: {2}\r\n{3}\r\n", boxData.Descriptor, boxData.ExpectedValue, box.ActualValue, new String('-', 20));
results.AppendFormat("Descriptor {0} was success.\r\nExpectedValue: {1} - ActualValue: {2}\r\n{3}\r\n", boxData.Descriptor, boxData.ExpectedValue, box.ActualValue, new String('-', 20));
Console.WriteLine(results);
public string Descriptor { get; set; }
public int[] Coords { get; set; }
public string ExpectedValue { get; set; }
private string boxDescriptor = "";
private string actualValue = "";
public BoxName(int iux, int iuy, int iw, int ih, string sBoxDescriptor)
boxDescriptor = sBoxDescriptor;
actualValue = extractText(ux,uy,w,h);
public BoxName(int[] iCoords, string sBoxDescriptor)
boxDescriptor = sBoxDescriptor;
actualValue = extractText(ux,uy,w,h);
public string BoxDescriptor { get { return boxDescriptor; } }
public string CoordsString
get { return ux + "," + uy + "," + w + "," + h; }
get { return new int[] { ux, uy, w, h }; }
public string ActualValue
get { return actualValue; }
private string extractText(int ux, int uy, int w, int h)
string path = Path.Combine(Directory.GetCurrentDirectory(), "91349039.pdf");
PDDocument doc = PDDocument.load(path);
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.addRegion("testRegion", new java.awt.Rectangle(ux, uy, w, h));
stripper.extractRegions((PDPage)doc.getDocumentCatalog().getAllPages().get(0));
string text = stripper.getTextForRegion("testRegion");