简体   繁体   English

Delphi 5:pdf中的总页数

[英]Delphi 5 : Total number of page in pdf

I am maintaining an old application that is built with Delphi 5. I need to determine the total number of pages in a given pdf file.我正在维护一个用 Delphi 5 构建的旧应用程序。我需要确定给定 pdf 文件中的总页数。

I guess I could translate any solution for plain C also.我想我也可以为纯 C 翻译任何解决方案。

The current solution I have is forking a pdftk process, and parse its output.我目前的解决方案是分叉 pdftk 进程,并解析其输出。 But this is quite slow, so I was wondering if I could find a good open-source pdf parser library for delphi (5...)... And it seems it doesn't exist.但这很慢,所以我想知道我是否可以为delphi(5 ...)找到一个好的开源pdf解析器库......而且它似乎不存在。

So I tried to implement things like looking into the file's raw content for occurences of "/Type /Page" or "/Count" or "Linearized ... /N".所以我试图实现一些事情,比如查看文件的原始内容是否出现“/Type /Page”或“/Count”或“Linearized ... /N”。 But none of these -nor a combination of them- work in every case.但是这些方法——也不是它们的组合——在任何情况下都不起作用。

So I wondered if I could find an open-source DLL that I could make use of from Delphi 5. But I couldn't find any neither.所以我想知道我是否能找到一个我可以从 Delphi 5 中使用的开源 DLL。但我两个都找不到。 I stumbled upon iTextSharp but it is for .NET and I don't understand howto use it in a plain delphi5 program...我偶然发现了 iTextSharp,但它适用于 .NET,我不明白如何在普通的 delphi5 程序中使用它...

So my final thought is this : is there any change I could find the source code for -say- pdftk and compile it as a DLL?所以我最后的想法是:我能找到 -say-pdftk 的源代码并将其编译为 DLL 有什么变化吗? Could anyone point me to the right direction?有人能指出我正确的方向吗?

Is there any solution I am missing?有没有我缺少的解决方案?

I thank you in advance for your help!我预先感谢您的帮助!

Did you try to use the PDFLib dll ?您是否尝试使用PDFLib dll

It is a very efficient library, and they have binding for Delphi, either via COM or via their dll.这是一个非常高效的库,并且它们通过 COM 或通过它们的 dll 绑定了 Delphi。 I guess you could use Delphi 5 with this library.我想你可以在这个库中使用 Delphi 5。

Did you try to run the free pdfinfo tool?您是否尝试运行免费的pdfinfo工具?

Download it from http://www.foolabs.com/xpdfhttp://www.foolabs.com/xpdf下载

It returns such information:它返回这样的信息:

Title:          Optimizing software in C++
Keywords:       software C++ optimization compiler
Author:         Agner Fog
Creator:        Microsoft® Word 2013
Producer:       Microsoft® Word 2013
CreationDate:   12/15/14 14:25:13
ModDate:        12/15/14 14:25:13
Tagged:         yes
Form:           none
Pages:          37
Encrypted:      no
Page size:      595.32 x 841.92 pts (A4) (rotated 0 degrees)
File size:      531693 bytes
Optimized:      no
PDF version:    1.5

So here you have your number of pages.所以这里有你的页数。 And it is very fast.而且速度非常快。

If you install adobe acrobat (not reader) on user's PC then you can use OLE automation.如果您在用户的 PC 上安装了 adobe acrobat(不是阅读器),那么您可以使用 OLE 自动化。

Here example how to open file and read pages count:下面是如何打开文件和读取页数的示例:

    uses
      ComObj, ActiveX;

    function TmyForm.IsOLEObjectInstalled(Name: String): Boolean;
    var
      ClassID: TCLSID;
      Rez: HRESULT;
    begin
      Rez := CLSIDFromProgID(PWideChar(WideString(Name)), ClassID);

      Result := (Rez = S_OK);
    end;

    procedure TmyForm.ButtonCheckPagesClick(Sender: TObject);
    var
      doc: OleVariant;
      pagesCount: Integer;
    begin
      if IsOLEObjectInstalled('AcroExch.PDDoc') then
      begin
        doc := CreateOleObject('AcroExch.PDDoc');

        if doc.Open('C:\test\test.pdf') then
        begin
          pagesCount := doc.GetNumPages;
          ShowMessageFmt('pages count = %d', [pagesCount]);
        end
        else
        begin
          ShowMessage('Can''t open file');
        end;
      end
      else
      begin
        ShowMessage('You must install adobe acrobat for that feature');
      end;
    end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM