PDF数据提取------2.相关类介绍
1.简介
构造数据类型PdfString封装Rect类,PdfAnalyzer类中定义一些PDF解析方法。
2.PdfString类与Rect类
public class PdfString : IComparable<PdfString>
{
public string Words { get; set; }
public Rect Position { get; set; }
public int PageNumber { get; set; } public PdfString(string str, Rect pos, int pageNum)
{
this.Words = str;
this.Position = pos;
this.PageNumber = pageNum;
} public override string ToString()
{
return this.Words;
} public static PdfStringComparer GetComparer()
{
return new PdfString.PdfStringComparer();
} public int CompareTo(PdfString rhs)
{
return this.Position.x2.CompareTo(rhs.Position.x2);
} /// <summary>
///comparer by Horizontal or vertical
/// </summary>
/// <param name="rhs"></param>
/// <param name="which">comparer type</param>
/// <returns></returns>
public int CompareTo(PdfString rhs, PdfString.PdfStringComparer.ComparisonType which)
{
switch (which)
{
case PdfStringComparer.ComparisonType.Horizontal:
return this.Position.x2.CompareTo(rhs.Position.x2);
case PdfStringComparer.ComparisonType.Vertical:
return this.Position.y2.CompareTo(rhs.Position.y2);
} return ;
} /// <summary>
/// for comparer custom comparer type
/// </summary>
public class PdfStringComparer : IComparer<PdfString>
{
private PdfString.PdfStringComparer.ComparisonType whichComparison; public enum ComparisonType { Horizontal, Vertical }; public int Compare(PdfString lhs, PdfString rhs)
{
return lhs.CompareTo(rhs, whichComparison);
} public PdfString.PdfStringComparer.ComparisonType WhichComparison
{
get { return whichComparison; }
set { whichComparison = value; }
}
}
}
public class Rect
{
public Rect();
public Rect(Obj rect);
public Rect(double x1, double y1, double x2, double y2); public double x1 { get; set; }
public double x2 { get; set; }
public double y1 { get; set; }
public double y2 { get; set; } public void __dtor();
public void Attach(Obj obj);
public bool Contains(double x, double y);
public void Get(ref double out_x1, ref double out_y1, ref double out_x2, ref double out_y2);
public double Height();
public void Inflate(double amount);
public void Inflate(double x, double y);
public bool IntersectRect(Rect rect1, Rect rect2);
public void Normalize();
public static Rect op_Assign(Rect lr, Rect rr);
public void Set(Rect p);
public void Set(double x1, double y1, double x2, double y2);
public bool Update();
public bool Update(Obj obj);
public double Width();
}
3.关于PdfString类
1.封装Rect原因
a.Rect这个类粒度太小,必须有个类能够保存搜索到关键信息的数据结构,为了根据语义二次识别出准备关键信息。
b. Words属性保存每个搜索结果值
c.Rect:保存结果左上角坐标(x1,y1)右下角坐标(x2,y2)信息
d.PageNumber:数据所在页码,由于PDF中坐标相对于每一页来说的,所以需要页码定位数据为止。
2.实现IComparable<PdfString>接口和加入一个嵌入类PdfStringComparer
a.因为在根据坐标解析PDF中获取数据信息并不是有序,就是说抓取数据并不是按照一定规律保存的,而是杂乱无章的。
b.如果单纯实现IComparable<T>接口可以根据Position属性选择一种方式来讲得到结果集排序,注意实现IComparable<T>
只能选择一种固定排序方式,无法根据情况自定义排序方式,于是PdfString类中引入一个嵌入类PdfStringComparer当使用
x1或x2坐标作为基准则按Horizontal位置坐标排序结果集,反之按照Vertical。
4.关于PdfAnalyzer类
public enum PositionRect { X1, Y1, X2, Y2 }
public class PdfAnalyzer
{
public List<PdfString> RegexSearchAllPages(PDFDoc doc, string pattern)
{
return RegexSearch(doc, pattern, false, -, -, true);
}
public List<PdfString> RegexSearchByPageRange(PDFDoc doc, string pattern, int startPage, int endPage)
{
if (endPage > doc.GetPageCount())
throw new Exception("endPage out of MaxRange of pdf.");
if (startPage < )
throw new Exception("startPage out of MixRange of pdf.");
if (startPage > endPage)
throw new Exception("pageRange is invalid.");
return RegexSearch(doc, pattern, false, startPage, endPage, true);
}
public List<PdfString> RegexSearchByPage(PDFDoc doc, string pattern, int pageIndex)
{
if (pageIndex > doc.GetPageCount())
throw new Exception("pageIndex out of MaxRange of pdf.");
if (pageIndex < )
throw new Exception("pageIndex out of MixRange of pdf.");
return RegexSearch(doc, pattern, false, pageIndex, pageIndex, true);
}
public List<PdfString> RegexSearch(PDFDoc doc, string pattern, bool ifWholeWord, int startPage, int endPage, bool ignoreCase)
{
List<PdfString> result = new List<PdfString>();
Int32 page_num = ;
string result_str = "";
string ambient_string = "";
Highlights hlts = new Highlights();
Int32 mode = (Int32)(TextSearch.SearchMode.e_reg_expression | TextSearch.SearchMode.e_highlight);
if (ifWholeWord) mode |= (Int32)TextSearch.SearchMode.e_whole_word;
if (ignoreCase) mode |= (Int32)TextSearch.SearchMode.e_case_sensitive;
int pageCount = doc.GetPageCount();
if (endPage > pageCount) endPage = pageCount;
TextSearch txt_search = new TextSearch();
txt_search.Begin(doc, pattern, mode, startPage, endPage);
while (true)
{
TextSearch.ResultCode code = txt_search.Run(ref page_num, ref result_str, ref ambient_string, hlts);
if (code == TextSearch.ResultCode.e_found)
{
hlts.Begin(doc);
double[] box = null;
string temp = result_str;
while (hlts.HasNext())
{
box = hlts.GetCurrentQuads();
if (box.Length != )
{
hlts.Next();
continue;
}
result.Add(new PdfString(result_str, new Rect(box[], box[], box[], box[]), page_num));
hlts.Next();
}
}
else if (code == TextSearch.ResultCode.e_done)
{
break;
}
}
return result;
}
public List<PdfString> RegexExtractByPositionWithPage(PDFDoc doc, string pattern, int pageIndex, Rect rect, PositionRect positionRect = PositionRect.Y2, double range = 2.0)
{
return GetNearbyPdfString(RegexSearchByPage(doc, pattern, pageIndex), rect, positionRect, range);
}
public List<PdfString> GetNearbyPdfString(List<PdfString> pdfStringAll, Rect rect, PositionRect positionRect, double range)
{
List<PdfString> pdfStringFilter = new List<PdfString>();
foreach (var pdf in pdfStringAll)
{
if (pdf == null)
continue;
if (GetRange(pdf.Position, rect, positionRect) > range)
continue;
pdfStringFilter.Add(pdf);
}
PdfString.PdfStringComparer comparerType = PdfString.GetComparer();
if (positionRect.Equals(PositionRect.Y2) || positionRect.Equals(PositionRect.Y1))
comparerType.WhichComparison = PdfString.PdfStringComparer.ComparisonType.Horizontal;
else if (positionRect.Equals(PositionRect.X1) || positionRect.Equals(PositionRect.X2))
comparerType.WhichComparison = PdfString.PdfStringComparer.ComparisonType.Vertical;
pdfStringFilter.Sort(comparerType);
return pdfStringFilter;
}
private double GetRange(Rect pdf, Rect title, PositionRect positionRect)
{
switch (positionRect)
{
case PositionRect.X1:
return Math.Abs(pdf.x1 - title.x1);
case PositionRect.X2:
return Math.Abs(pdf.x2 - title.x2);
case PositionRect.Y1:
return Math.Abs(pdf.y1 - title.y1);
case PositionRect.Y2:
return Math.Abs(pdf.y2 - title.y2);
default:
throw new Exception("calculation range of pdfstring with title error.");
}
}
public List<PdfString> RegexExtractByPositionWithAllPage(PDFDoc doc, string pattern, Rect rect, PositionRect positionRect = PositionRect.Y2, double range = 2.0)
{
return GetNearbyPdfString(RegexSearchAllPages(doc, pattern), rect, positionRect, range);
}
public List<PdfString> RegexExtractByPositionWithRangePage(PDFDoc doc, string pattern, int startPage, int endPage, Rect rect, PositionRect positionRect = PositionRect.Y2, double range = 2.0)
{
return GetNearbyPdfString(RegexSearchByPageRange(doc, pattern, startPage, endPage), rect, positionRect, range);
}
}
注释:
1.RegexSearchAllPages、RegexSearchByPageRange、RegexSearchByPage由方法名可知通过正则表达式得到搜索的 PdfString结果集合。
2.RegexExtractByPositionWithPage、RegexExtractByPositionWithAllPage、RegexExtractByPositionWithRangePage由方法名只知根据传入坐标和误差范围或者每一行或者每列的排序后数据集合。
PDF数据提取------2.相关类介绍的更多相关文章
- PDF数据提取------3.解析Demo
1.PDF中文本字符串格式中关键值信息抓取(已完成) 简介:这种解析比较传统最简单主要熟练使用Regular Expression做语义识别和验证.例如抓取下面红色圈内关键信息 string mett ...
- 大数据及hadoop相关知识介绍
一.大数据的基本概念 1.1什么是大数据 互联网企业是最早收集大数据的行业,最典型的代表就是Google和百度,这两个公司是做搜索引擎的,数量都非常庞大,每天都要去把互联网上的各种各样的网页信息抓取下 ...
- I/O---BufferedInputStream及相关类介绍
关于BufferedInputStream 是java提供的具有缓存作用的字节输入流.与之对应的还有BufferedOutStream 和 BufferedRead 和BufferedWriter 这 ...
- scrapy架构与目录介绍、scrapy解析数据、配置相关、全站爬取cnblogs数据、存储数据、爬虫中间件、加代理、加header、集成selenium
今日内容概要 scrapy架构和目录介绍 scrapy解析数据 setting中相关配置 全站爬取cnblgos文章 存储数据 爬虫中间件和下载中间件 加代理,加header,集成selenium 内 ...
- Util应用程序框架公共操作类(一):数据类型转换公共操作类(介绍篇)
本系列文章将介绍一些对初学者有帮助的辅助类,这些辅助类本身并没有什么稀奇之处,如何能发现需要封装它们可能更加重要,所谓授之以鱼不如授之以渔,掌握封装公共操作类的技巧才是关键,我会详细说明创建这些类的动 ...
- VS2010/MFC编程入门之十三(对话框:属性页对话框及相关类的介绍)
前面讲了模态对话框和非模态对话框,本节开始鸡啄米讲一种特殊的对话框--属性页对话框.另外,本套教程所讲大部分对VC++各个版本均可适用或者稍作修改即可,但考虑到终究还是基于VS2010版本的,所以将& ...
- VS2010-MFC(对话框:属性页对话框及相关类的介绍)
转自:http://www.jizhuomi.com/software/164.html 一 属性页对话框的分类 属性页对话框想必大家并不陌生,XP系统中桌面右键点属性,弹出的就是属性页对话框,它通过 ...
- MFC编程入门之十三(对话框:属性页对话框及相关类的介绍)
前面讲了模态对话框和非模态对话框,本节来将一种特殊的对话框--属性页对话框. 属性页对话框的分类 属性页对话框想必大家并不陌生,XP系统中桌面右键点属性,弹出的就是属性页对话框,它通过标签切换各个页面 ...
- SQL Server数据库读取数据的DateReader类及其相关类
之前学了几天的SQL Server,现在用C#代码连接数据库了. 需要使用C#代码连接数据库,读取数据. 涉及的类有: ConfigurationManage SqlConnection SqlCom ...
随机推荐
- 如何创建PostgreSQL数据库
PostgreSQL提供两种方式创建一个新的数据库:第一种是使用CREATE DATABASE的SQL命令.第二种使用createdb的一个命令行可执行文件. 第一种:使用CREATE DATABAS ...
- 错误 -force-32bit 与 ANDROID_EMULATOR_FORCE_32BIT=true
1,配置环境变量, 加上ANDROID_EMULATOR_FORCE_32BIT=true 2,在AS中启动模拟器用下面方法 在你要运行的个工程右击->Run as -> Run conf ...
- BZOJ 1018 堵塞的交通traffic(线段树)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1018 题意:一个2*n的格子,相邻格子之间有一条道路.初始时道路是不通的. 三种操作:( ...
- 第四篇 在中国做ERP系统实施你必须知道的一些常识
1. ERP实施要特别从参与全球竞争的视角指引系统建设.中国社会经历了一个从计划经济体制到市场经济体制的转变.中国加入WTO后,要与国际接轨,要按照世界贸易组织有关的贸易规则开展国际贸易.中国的关税与 ...
- Large Object Heap内存碎片在.NET 4.5中的改进
.NET 4.5已然到来,预览了解了下Large Object Heap在.NET 4.5中的效能改进.借此和大家来探讨下.本文不讨论Loder Heap,SOH(samll object heap) ...
- C#分页类
using System.Linq; using System.Collections.Generic; namespace CommonLibrary { public class PagedLis ...
- Machine Learning for hackers读书笔记(六)正则化:文本回归
data<-'F:\\learning\\ML_for_Hackers\\ML_for_Hackers-master\\06-Regularization\\data\\' ranks < ...
- Jqgrid入门-Jqgrid分组的实现(八)
上一章主要说明了如果实现Jqgrid列数据拖动,这一章主要讨论在Jqgrid中如何实现分组功能. 类似于Sql语句的Group By,Jqgrid提供了属性实现数据分组,这样表现数据会 ...
- Volley : "参数param:{ inoutNo:inoutNo ,whcode:’’}
private void fuzzySearch() { mRequestQueue = Volley.newRequestQueue(getActivity()); String str = Sha ...
- oraclede chuangjian yu dajian(zhuan)
http://wenku.baidu.com/link?url=pIKLZJ4sAurjNGjwgChqjRMhCXfn77qy1K_EW3nlGn4eN4roDN8mhSG0GakYbrTBcsD4 ...