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;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM