zoukankan      html  css  js  c++  java
  • 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;
    
    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.

    运行:

    提取的:

    原本pdf:

  • 相关阅读:
    C语言寒假大作战03
    C语言寒假大作战02
    C语言寒假大作战01
    助教学期总结
    第十六周助教总结
    C语言I博客作业11
    第十五周助教总结
    C语言I博客作业10
    第十四周助教总结
    C语言寒假大作战03
  • 原文地址:https://www.cnblogs.com/hhmm99/p/10850069.html
Copyright © 2011-2022 走看看