我正在尝试使用命令行实用程序(使用dos命令行中的测试)从我的Delphi代码将PDF转储到文本。
这是我的代码
if fileexists(ExtractFilePath(Application.ExeName) + 'pdftotext.exe') then
begin
ShellExecute(H,'open', 'pdftotext.exe', PWideChar(fFileName), nil, SW_SHOWNORMAL);
if fileExists(changeFileExt(fFileName, '.txt')) then
Lines.LoadFromFile(changeFileExt(fFileName, '.txt'))
else
ShowMessage('File Not found');
end;
在代码中放置断点并单步执行时,它会转到
if fileExists(changeFileExt(fFileName, '.txt')) then
但是返回false,因此调用了Shellexecute,但是没有转储任何文件
我做错了什么?
答案 0 :(得分:7)
ShellExecute
不等待被调用的程序完成运行。您可能过早检查文件。该文件尚未创建。
运行程序并等待它终止,然后再检查输出文件。 ShellExecute
没有为您提供足够的信息,因此您应该尝试使用CreateProcess
。有几个例子说明如何做到这一点。试试这个:
答案 1 :(得分:1)
事实证明,向fillatble添加填充路径使其工作正常
uses
Forms, ShellAPI, SysConst, SysUtils;
procedure Pdf2Text(const fFileName: string; const Lines: TStrings);
var
H: HWND;
PdfToTextPathName: string;
ReturnValue: Integer;
TxtFileName: string;
begin
H := 0;
PdfToTextPathName := ExtractFilePath(Application.ExeName) + 'pdftotext.exe'; // full path
if FileExists(PdfToTextPathName) then
begin
ReturnValue := ShellExecute(0,'open', PWideChar(PdfToTextPathName), PWideChar(fFileName), nil, SW_SHOWNORMAL);
if ReturnValue <= 32 then
RaiseLastOsError();
// note: the code below this line will crash when pdftotext.exe does not finish soon enough; you should actually wait for pdftotext.exe completion
TxtFileName := ChangeFileExt(fFileName, '.txt');
if FileExists(TxtFileName) then
Lines.LoadFromFile(TxtFileName)
else
raise EFileNotFoundException.CreateRes(@SFileNotFound);
end;
end;
编辑:一些代码清理有助于及早发现错误,特别是在测试概念证明时。