
正文
delphi使用Foxit Quick PDF Library读写pdf文本和图片
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
简介:
Debenu Quick PDF Library(PDF编程开发工具)提供一套全方位的 PDF API 函数,帮助您快速简便地处理 PDF 文件。从文档属性的基本操作到创建您自己的 PDF 查看器和 PDF 编辑器,这款软件满足您的所有需求。Quick PDF Library是一款供 PDF 开发人员使用的 SDK,功能强大、无需版税,其中包括超过500个函数,可用于 Delphi、C、C#、C++、ASP、VB6、VB.NET、VBScript、PHP、PowerBASIC 等,使用 ActiveX、DLL、LIB 或 Delphi 版本的库
官方帮助文档:https://www.debenu.com/docs/pdf_library_reference/FunctionGroups.php
可以参考(提取文本和图像并插入新PDF):http://quickpdf.org/forum/extract-text-and-images-and-insert-into-new-pdf_topic1308.html
安装:
首先到官网下载该库,官网地址为:http://www.debenu.com/。本文所使用的版本为11.11,下载后得到一个exe文件:foxit_quick_pdf_library_en.exe。双击exe文件即可安装控件库,安装过程中会要求输入安装目录,选择合适的目录完成安装。

文件GettingStarted.pdf介绍了在使用该控件库之前需要做的一些准备工作。首先以管理员身份运行命令提示符并切换到安装目录下,然后输入以下命令完成控件的注册。(我这里安装了两个版本所以有1131的版本)

接着把DebenuPDFLibraryDLL1111.dll、DebenuPDFLibraryDLL1111.pas 添加到Delphi项目中

实例程序
程序记得uses DebenuPDFLibraryDLL1111
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, SynPdf, StdCtrls, DebenuPDFLibraryDLL1111;type
TForm1 = class(TForm)
btn1: TButton;
edt1: TEdit;
edt2: TEdit;
lbl1: TLabel;
procedure btn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}// 读取pdf文本内容以及图片
function ReadPdf(const fileName, saveImagePath: string; var text: string;
var imageFiles: string): string;
var
rPdf: TDebenuPDFLibraryDLL1111;
imageCount, i, j, num, keyStatus, FH, PR: Integer;
begin
Result := '';
num := ;
if Trim(fileName) = '' then
begin
Result := 'Path cannot be empty';
Exit;
end;
if (Trim(saveImagePath) <> '') and (not DirectoryExists(saveImagePath)) then
begin
ForceDirectories(saveImagePath); // 创建目录
end; rPdf := TDebenuPDFLibraryDLL1111.Create('DebenuPDFLibraryDLL1111.dll'); // 库
keyStatus := rPdf.UnlockKey('**********'); // 密钥 秘钥可以购买或者找我要
if keyStatus <> then
begin
Result := 'The library cannot be loaded or unlocked fails';
Exit;
end;
try
rPdf.LoadFromFile(Trim(fileName), '');
// 以直接访问模式打开文件并存储文件句柄
FH := rPdf.DAOpenFile(fileName, '');
for i := to rPdf.DAGetPageCount(FH) do
begin
rPdf.SelectPage(i); // 选区页
text := text + rPdf.GetPageText(); // 获取文本 8:更准确的文本提取算法
if Trim(saveImagePath) <> '' then
begin
imageCount := rPdf.GetPageImageList(); // 获取图片
for j := to rPdf.GetImageListCount(imageCount) do // 遍历当前页中的所有图片
begin
rPdf.SaveImageListItemDataToFile(imageCount, j, ,
saveImagePath + '\' + IntToStr(num) + '.png');
imageFiles := imageFiles + saveImagePath + '\' + IntToStr(num)
+ '.png ; ';
inc(num);
end;
end;
end;
finally
rPdf.Free;
end;
end;// 写pdf
function WritePdf(const fileName, text: string): string;
var
wPdf: TDebenuPDFLibraryDLL1111;
num, wStatus: Integer;
begin
Result := '';
if Trim(fileName) = '' then
begin
Result := 'Path cannot be empty';
Exit;
end;
try
wPdf := TDebenuPDFLibraryDLL1111.Create('DebenuPDFLibraryDLL1111.dll'); // 库
try
wStatus := wPdf.UnlockKey('*************'); // 密钥
if wStatus = then
begin
num := wPdf.AddTrueTypeSubsettedFont('FangSong', text, );
wPdf.SelectFont(num);
wPdf.DrawWrappedText(, , , text);
wPdf.SaveToFile(fileName);
end
else
begin
Result := 'The library cannot be loaded or unlocked fails';
end;
finally
wPdf.Free;
end;
except
on e: Exception do
Result := e.Message;
end;
end;procedure TForm1.btn1Click(Sender: TObject);
var
text, imageFiles: string;
begin
text := '';
imageFiles := '';
// showmessage(WritePdf(edt1.Text,edt2.Text));
ShowMessage(ReadPdf(edt1.text, edt2.text, text, imageFiles));
lbl1.Caption := text;
ShowMessage(text);
ShowMessage(imageFiles);
end;procedure TForm1.FormCreate(Sender: TObject);
begin
//readAndWritePDf();
end;end.
运行:
提取的:

原本pdf:









