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、VB六、VB.NET、VBScript、PHP、PowerBASIC 等,使用 ActiveX、DLL、LIB 或 Delphi 版本的庫php

 

官方幫助文檔:https://www.debenu.com/docs/pdf_library_reference/FunctionGroups.phphtml

能夠參考(提取文本和圖像並插入新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文件便可安裝控件庫,安裝過程當中會要求輸入安裝目錄,選擇合適的目錄完成安裝。app

 

 


文件GettingStarted.pdf介紹了在使用該控件庫以前須要作的一些準備工做。首先以管理員身份運行命令提示符並切換到安裝目錄下,而後輸入如下命令完成控件的註冊。(我這裏安裝了兩個版本因此有1131的版本編輯器

 

接着把DebenuPDFLibraryDLL1111.dll、DebenuPDFLibraryDLL1111.pas 添加到Delphi項目中函數

 

 

 實例程序

程序記得uses DebenuPDFLibraryDLL1111工具

unit Unit1;

interface

uses
  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 := 0;
  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 <> 1 then
  begin
    Result := 'The library cannot be loaded or unlocked fails';
    Exit;
  end;
  try
    rPdf.LoadFromFile(Trim(fileName), '');
    // 以直接訪問模式打開文件並存儲文件句柄
    FH := rPdf.DAOpenFile(fileName, '');
    for i := 1 to rPdf.DAGetPageCount(FH) do
    begin
      rPdf.SelectPage(i); // 選區頁
      text := text + rPdf.GetPageText(8); // 獲取文本 8:更準確的文本提取算法
      if Trim(saveImagePath) <> '' then
      begin
        imageCount := rPdf.GetPageImageList(0); // 獲取圖片
        for j := 1 to rPdf.GetImageListCount(imageCount) do // 遍歷當前頁中的全部圖片
        begin
          rPdf.SaveImageListItemDataToFile(imageCount, j, 0,
            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 = 1 then
      begin
        num := wPdf.AddTrueTypeSubsettedFont('FangSong', text, 0);
        wPdf.SelectFont(num);
        wPdf.DrawWrappedText(50, 750, 500, 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.

運行:開發工具

提取的:ui

 

本來pdf:

相關文章
相關標籤/搜索