Graccvs文件正文提取开发组件支持各种文件提取正文,为Lucene/CLucene, Elasticsearch, Sphinx等全文检索工具,为OA,ERP,CRM系统使用文件提供文件正文使用和搜索,支持常见各种文件格式”.pdf", ".doc", ".odt", ".docx", ".dotm", ".docm", ".wps", ".xls", ".xlsx", ".xlsm", ".xltm", ".et", ".ppt", ".pptx", ".potm", ".pptm", ".ppsm", ".dps", ".ofd"(电子发票版式文件), ".rtf",".html", ".htm", ".mht", ".mhtml", ".eml", ".emmx", "xmind", "gmind", ".chm", ".zip" 等。
Graccvs提供免费版本,以下是调用过程及代码,起来文件输入修改为要提取的文件名称和类型即可。
以下是使用DLL过程:
1:建立Console Application工程或者 VCL Application工程。
2:拷贝 graccvs64.dll到工程中, 默认在exe输出位置。
3:拷贝graccvsTest.dpr文件相关代码 。
4:编译运行工程 。
5:点击这里下载完整工程包,这里下载最新DLL文件。
graccvsTest.dpr :
- program graccvsTest;
-
- {$APPTYPE CONSOLE}
- {$R *.res}
-
- uses
- System.SysUtils,
- Windows,
- Types,
- DateUtils,
- Classes;
-
- type
- // ----------------------以下为函数说明----------------------
-
- // DLL加载并设置动态库需要的临时文件夹,且对此文件夹要有读写权限
- TLoad = procedure(TempDir: PRawByteString); cdecl;
- (*
- 注册软件:
- 方式1:输入参数,corp为公司名称licText为注册码
- 方式2:把授权文件grauth.lic保存到动态库相同文件夹,调用TAuth函数(corp, licText都为空)系统自动加载grauth.lic
- 返回值:
- 0:免费版本许可为空
- 1:许可正常
- 2:序列号(公司授权名称)错误
- 3:加密数据格式错误
- 4:许可过期
- 5:许可验证错误
- 6:无效的许可
- 7:未知错误
-
- 注1:如果注册失败,系统变为免费版本
- 注2:免费版也需要调用此函数,corp和licText都为空
- *)
- TAuth = function(Corp, LicText: PRawByteString): Cardinal; cdecl;
-
- // 释放TToString, THttpToString等函数的返回指针
- TFreeString = procedure(P: PRawByteString); cdecl;
- // 得到最后的错误信息
- TLastError = function(): PRawByteString; cdecl;
-
- // 提供文件正文
- // Infile输入文件地址, 返回UTF-8编码字符串数据指针(此指针需要使用TFreeString函数释放内存)
- TToString = function(Infile: PRawByteString): PRawByteString; cdecl;
- // 提供文件正文,并保存到目标文件
- // Infile输入文件地址, Outfile为TXT目标文件文件地址
- TToTextFile = function(Infile, Outfile: PRawByteString): Cardinal; cdecl;
-
- // 提取Http/Https文件,返回字符串数据指针
- // Url=Http/Https地址
- // FileExt=文件类型(比如:".pdf"),
- // Timeout=超时设置,超过此数值系统终止下载文件。单位为毫秒,默认为0(等待文件下载直到完成)
- // HttpParams=JSON格式Header数据和Cookie数据,默认为空
- (*
- JSON格式如下:
- {"headers":
- [{名称1: 值1},{名称2: 值2},...],
- "cookies":[
- {"name": 名称(字符串), "value": 值(字符串), "expires": 有效期(整数,单位毫秒),
- "path": 路径(字符串), "domain": 域名(字符串)},
- {"name": 名称(字符串), "value": 值(字符串), "expires": 有效期(整数,单位毫秒),
- "path": 路径(字符串), "domain": 域名(字符串)}
- ...
- ]}
- 例如:
- {"headers":[{"client_id": "g01x9"}, {"client_secret": "e23c89cc9fe"}, {"client_index": 10092}],
- "cookies":[{"name": "ga", "value": "1020", "expires":36000000, "path": "/hx/", "domain":"www.gaya-soft.cn"},
- {"name": "xc3", "value": "10099", "expires":240000, "path": "", "domain":""}]}
- *)
- // 返回UTF-8编码字符串数据指针(此指针需要使用TFreeString函数释放内存)
- THttpToString = function(Url, FileExt: PRawByteString; Timeout: Cardinal; HttpParams: PRawByteString)
- : PRawByteString; cdecl;
- // 下载Http文件,并提取文本,保存到目标文件
- // Outfile为TXT目标文件文件地址,其他参数和THttpToString参数相同
- THttpToTextFile = function(Url, FileExt, Outfile: PRawByteString; Timeout: Cardinal; HttpParams: PRawByteString)
- : Cardinal; cdecl;
-
- // 文件提取异步任务, Infile输入文件地址, Outfile为TXT目标文件文件
- // 如果提取某个文件错误,则 Outfile的内容如下格式: @ErrCode:错误代码, ErrMessage:错误提示
- TAddTask = procedure(InFilePtr, OutTxtFilePtr: PRawByteString); cdecl;
- // 异步提取Http文件任务,参数同 THttpToTextFile 函数
- TAddHttpTask = procedure(Url, FileExt, OutTxtFile: PRawByteString; Timeout: Cardinal;
- Params: PRawByteString); cdecl;
- // 开始执行异步任务,返回值=1开始执行, 其他值未识别
- // =2 免费版不支持此功能,=3 没有可以执行的任务 ,=4 当前任务未完成
- TAsyncStart = function(): Cardinal; cdecl;
- // 停止任务
- TAsyncStop = procedure(); cdecl;
- // 一直等待,直到全部异步任务结束
- TAsyncWait = procedure(); cdecl;
- // 得到执行异步任务的状态, =0 没开始, =1 正在处理中,=2 已中断, =99 处理完成
- TAsyncState = function(): Cardinal; cdecl;
- // 设置执行异步任务的并发数量(不大于软件授权数量),返回并发数量
- TAsyncMaxProcs = function(Num: Cardinal): Cardinal;
-
- // 关闭动态库前调用此函数释放资源,否则关闭DLL会发生错误
- TUnload = procedure(); cdecl;
-
- // 软件注册序列号,用此序列号申请注册码。每次调用返回的值是不一样的
- // 返回UTF-8编码字符串数据指针(此指针需要使用TFreeString函数释放内存)
- TUuid = function(): PRawByteString; cdecl;
-
- // 提取文本的错误类型
- const
- TFE_OK = 0;
- TFE_UNKNOW = 1;
- TFE_FILE_NOTEXIST = 2;
- TFE_SAVE_ERROR = 3;
- TFE_OUTSIZE = 4;
- TFE_UNSUPPORTED = 5;
- TFE_ERROR_INTERFACE = 6;
- TFE_HTTP_ERR = 7;
- TFE_HTTP_FILE_NULL = 8;
- TFE_LICENCE_ERR = 9;
-
- var
- PLib: DWORD;
- FToTextFile: TToTextFile;
- FToString: TToString;
- FHttpToTextFile: THttpToTextFile;
- FHttpToString: THttpToString;
- FFreeString: TFreeString;
- FLastError: TLastError;
-
- function DllInit(): Boolean;
- var
- P2, P3, P4, P5, P6, P7: Pointer;
- begin
- P2 := GetProcAddress(PLib, 'LastErr');
- P3 := GetProcAddress(PLib, 'ToTextFile');
- P4 := GetProcAddress(PLib, 'ToString');
- P5 := GetProcAddress(PLib, 'HttpToTextFile');
- P6 := GetProcAddress(PLib, 'HttpToString');
- P7 := GetProcAddress(PLib, 'FreeString');
- if (P2 <> nil) and (P3 <> nil) and (P4 <> nil) and (P5 <> nil) and (P6 <> nil) and (P7 <> nil) then
- begin
- FLastError := TLastError(P2);
- FToTextFile := TToTextFile(P3);
- FToString := TToString(P4);
- FHttpToTextFile := THttpToTextFile(P5);
- FHttpToString := THttpToString(P6);
- FFreeString := TFreeString(P7);
- Result := True;
- end
- else begin
- Result := False;
- end;
- end;
-
- // DLL初始化和设置软件授权
- procedure LoadAndAuth();
- var
- P1, P2: Pointer;
- Load: TLoad;
- Auth: TAuth;
- TempDir: RawByteString;
- Corp, LicTest: RawByteString;
- begin
- P1 := GetProcAddress(PLib, 'Load');
- P2 := GetProcAddress(PLib, 'Auth');
- if (P1 <> nil) and (P2 <> nil) then begin
- Load := TLoad(P1);
- // DLL工作的临时文件夹 ,需要程序对此文件夹有读写文件夹权限
- TempDir := Utf8Encode('tmp\');
- Load(PRawByteString(TempDir));
- //
- Auth := TAuth(P2);
- // 调用软件注册,免费版也需要调用此函数(传空值即可)
- Corp := Utf8Encode('Beij Gaya');
- LicTest := Utf8Encode('');
- Auth(PRawByteString(Corp), PRawByteString(LicTest));
- end;
- end;
-
- procedure Unload();
- var
- P: Pointer;
- Unload: TUnload;
- begin
- P := GetProcAddress(PLib, 'Unload');
- if (P <> nil) then begin
- Unload := TUnload(P);
- Unload();
- end;
- end;
-
- // 根据错误类型返回错误信息
- function ErrText(Code: Cardinal): string;
- begin
- case Code of
- TFE_OK:
- Result := 'ok';
- TFE_UNKNOW:
- Result := '未知错误';
- TFE_FILE_NOTEXIST:
- Result := '提取源文件不存在';
- TFE_SAVE_ERROR:
- Result := '保存目标文件失败';
- TFE_OUTSIZE:
- Result := '提取的源文件超出设置的大小范围';
- TFE_UNSUPPORTED:
- Result := '不支持的提取文件格式';
- TFE_ERROR_INTERFACE:
- Result := '得到接口失败';
- TFE_HTTP_ERR:
- Result := 'HTTP下载文件失败';
- TFE_HTTP_FILE_NULL:
- Result := 'HTTP文件为空';
- TFE_LICENCE_ERR:
- Result := '软件许可错误';
- end;
- end;
-
- // 调用DLL函数
- function FileToText(SourceFile, OutTextFile: string): Cardinal;
- var
- InUtf8Name, OutUtf8Name: RawByteString;
- begin
- // 文件名称要UTF-8编码
- InUtf8Name := Utf8Encode(SourceFile);
- OutUtf8Name := Utf8Encode(OutTextFile);
- Result := FToTextFile(PRawByteString(InUtf8Name), PRawByteString(OutUtf8Name));
- end;
-
- // 测试提取正文,保存到目标文本文件
- procedure FileToTextTest();
- var
- R: Cardinal;
- P: Pointer;
- S: RawByteString;
- Err, Err2: string;
- begin
- Writeln(TimeToStr(Now) + ' -- start');
- R := FileToText('D:\graccvs\files\简可信模板OCR识别工具帮助.docx', 'D:\graccvs\files\grcv001.txt');
- if R = 0 then
- Writeln(TimeToStr(Now) + ' -- end')
- else begin
- // 得到错误方式1: 根据R值调用函数ErrText得到具体错误信息, 此方式速度快
- Err := ErrText(R);
- Writeln(Err);
-
- // 方式2:调用DLL函数,得到具体错误信息, 此方式错误信息更加准确
- P := FLastError();
- S := RawByteString(PAnsiChar(P));
- Err2 := Utf8ToWideString(S);
- //
- Writeln(Err2)
- end;
- end;
-
- // 调用DLL函数
- function FileToString(SourceFile: string; var Err: string): string;
- var
- InUtf8Name, S: RawByteString;
- P: Pointer;
- Index, R: Integer;
- begin
- // 文件名称要UTF-8编码
- InUtf8Name := Utf8Encode(SourceFile);
- P := FToString(PRawByteString(InUtf8Name));
- if P <> nil then begin
- try
- S := PAnsiChar(P);
- Result := Utf8ToWideString(S);
- finally
- FFreeString(P);
- end;
-
- // 如果得到正文失败, 返回格式为 @ErrCode:x
- // x为整数,可以根据x值调用函数ErrText得到具体错误信息
- Index := Pos('@ErrCode:', Result);
- if Index > 0 then begin
- R := StrToIntDef(Copy(Result, Index + 9, 1), 0);
- Err := ErrText(R);
- end
- else
- Err := '';
- end
- else begin
- Result := '';
- Err := '得到文本失败';
- end;
- end;
-
- // 测试提取正文
- procedure ToStringText();
- var
- S, Err: string;
- begin
- Writeln(TimeToStr(Now) + ' -- start');
- //
- S := FileToString('D:\graccvs\files\Adobe Intro.ofd', Err);
- if Err <> '' then
- Writeln(Err)
- else
- Writeln(S);
- end;
-
- // Http文件提取文件正文
- function HttpToString(Url, FileExt: string; Timeout: Integer; var Err: string): string;
- var
- InUtf8Url, FileExtUtf8, S: RawByteString;
- P: Pointer;
- Index, R: Integer;
- begin
- // 文件名称要UTF-8编码
- InUtf8Url := Utf8Encode(Url);
- // 要提取的文件类型,比如 .docx, .ppt, .pdf, .html 等文件后缀
- FileExtUtf8 := Utf8Encode(FileExt);
- // 调用DLL中HttpToString函数, Timeout为超时设
原文:https://blog.csdn.net/kkyy2021/article/details/122647260