delphi PDFium 常用功能

PDFium 常用功能

属性和方法

TPdf.Active

property Active: Boolean;

打开或关闭选定的PDF文档。

TPdf.FileName

property FileName: string;

PDF文件的名称(包含路径)。

TPdf.Password

property Password: string;

PDF文件的密码。

TPdf.PageNumber

property PageNumber: Integer;

当前选择的页面。

PageNumber值必须是1到PageCount之间。

TPdf.PageCount

property PageCount: Integer;

文档中的总页数。只读属性。

TPdf.LoadDocument

procedure LoadDocument(Data: TMemoryStream);

从内存中打开并加载PDF文档。

参数

Data 内存数据。当文档打开时,内存数据必须保持有效。

TPdf.RenderPage

function RenderPage(Left, Top, Width, Height: Integer; Rotation: TRotation = ro0; Options: TRenderOptions = []; Color: TColor = clWhite): TBitmap;

将页面内容转换为位图。

参数
LeftTop 左上角坐标

WidthHeight 宽度和高度

Rotation 页面旋转。

Options 页面呈现标志。

Color 背景色。

返回值
转换后的位图

TRotation

type TRotation = (ro0, ro90, ro180,  ro270);

PDF 页面旋转。 页面旋转顺时针旋转。

  • ro0 无页面旋转
  • ro90 页面顺时针旋转 90 度
  • ro180 页面顺时针旋转 180 度
  • ro270 页面顺时针旋转 270 度

TRenderOptions

type TRenderOptions = set of TRenderOption;

页面呈现标志。

  • reAnnotations 渲染注解
  • reLcd 为 LCD 显示优化的文本渲染
  • reNoNativeText 不要使用某些平台上提供的本机文本输出
  • reGrayscale 灰度输出
  • reDebugInfo 设置是否要获取一些调试信息
  • reNoCatchException 设置是否要不捕获异常
  • reLimitCache 限制图像缓存大小
  • reHalftone 始终使用半色调进行图像拉伸
  • rePrinting 渲染打印
  • reReverseByteOrder 以相反的字节顺序呈现; 此标志仅在渲染到位图时启用
  • reNoSmoothText 禁用文本上的抗锯齿
  • reNoSmoothImage 禁用图像上的抗锯齿
  • reNoSmoothPath 禁用路径上的抗锯齿

TPdfView.Active

property Active: Boolean;

打开或关闭选定的 PDF 页面。

TPdfView.PageNumber

property PageNumber: Integer;

当前选择的页面。

PageNumber值必须是1到PageCount之间。

TPdfView.PageCount

property PageCount: Integer;

文档中的总页数。只读属性。

例子

浏览PDF文件

在窗体上放置TPdfView组件PdfView1TPdf组件Pdf1,并设置PdfView1Pdf属性指向Pdf1

procedure TForm1.Button1Click(Sender: TObject);
begin
  //关闭已打开的PDF文件
  Pdf1.Active := False;
  //设置打开的PDF文件路径
  Pdf1.FileName := 'C:\LargeFile.pdf';
  //设置PDF文件密码
  Pdf1.Password := '';
  //设置打开的页数
  PdfView1.PageNumber := 1;
  //显示PDF
  PdfView1.Active := True;
end;

加载流中的PDF文件

在窗体上放置TPdfView组件PdfView1TPdf组件Pdf1,并设置PdfView1Pdf属性指向Pdf1

procedure TForm1.Button2Click(Sender: TObject);
var
  Memory: TMemoryStream;
begin
  //关闭已打开的PDF文件
  Pdf1.Active := False;
  //加载PDF文件到流中
  Memory := TMemoryStream.Create;
  Memory.LoadFromFile('C:\LargeFile.pdf');
  //设置PDF文件密码
  Pdf1.Password := '';
  //设置打开的页数
  PdfView1.PageNumber := 1;
  //加载流中的PDF
  Pdf1.LoadDocument(Memory);
  //显示PDF
  PdfView1.Active := True;
end;

打印PDF

在窗体上放置TPdf组件Pdf1,引用uses Printers;

procedure TForm1.Button3Click(Sender: TObject);
var
  Page: Integer;
  Bitmap: TBitmap;
begin
  try
    //读取pdf文件
    Pdf1.FileName := 'C:\LargeFile.pdf';
    Pdf1.Active := True;
    //开始打印
    Printer.BeginDoc;
    try
      //循环所有PDF页面
      for Page := 1 to Pdf1.PageCount do
      begin
        if Page > 1 then
          Printer.NewPage;
        //选择PDF文档的页面
        Pdf1.PageNumber := Page;
        //将页面内容转换为bmp
        Bitmap := Pdf1.RenderPage(0, 0, Printer.PageWidth, Printer.PageHeight, ro0, [rePrinting]);
        //打印bmp
        Printer.Canvas.StretchDraw(Printer.Canvas.ClipRect, Bitmap);
      end;
    finally
      Printer.EndDoc;
    end;
  finally
    Pdf1.Active := False;
  end;
end;

转换为图片

在窗体上放置TPdf组件Pdf1

procedure TForm1.Button4Click(Sender: TObject);
var
  Page: Integer;
  Bitmap: TBitmap;
begin
  try
    //读取pdf文件
    Pdf1.FileName := 'C:\LargeFile.pdf';
    Pdf1.Active := True;
    //循环所有PDF页面
    for Page := 1 to Pdf1.PageCount do
    begin
      //选择PDF文档的页面
      Pdf1.PageNumber := Page;
      //转换成bmp(pdf中的屏幕像素数是72)
      Bitmap := Pdf1.RenderPage(0, 0, Round(Screen.PixelsPerInch * Pdf1.PageWidth / 72.0), Round(Screen.PixelsPerInch * Pdf1.PageHeight / 72.0));
      try
        Bitmap.SaveToFile('C:\bmp' + '_' + IntToStr(Page) + '.bmp');
      finally
        Bitmap.Free;
      end;
    end;
  finally
    Pdf1.Active := False;
  end;
end;
原文地址:https://www.cnblogs.com/txgh/p/15771575.html