1. 程式人生 > 其它 >delphi SynPDF 常用功能

delphi SynPDF 常用功能

SynPDF 常用功能

屬性和方法

TPdfDocument.Create

constructor Create(AUseOutlines: Boolean=false; ACodePage: integer=0;
  APDFA1: boolean=false; AEncryption: TPdfEncryption=nil);

使用 VCL Canvas 屬性建立 PDF 文件例項。

引數

AUseOutlines 用於指定PDF文件是否將使用大綱。

ACodePage 用於指定PDF文件文字編碼的字符集; 預設情況下(ACodePage = 0),使用當前系統字符集。

APDFA1 是否建立相容 PDF/A 的文件。

AEncryption 設定加密。

預設 A4 紙張大小。

只有 Win-Ansi 編碼允許使用嵌入的標準字型。

TPdfDocumentGDI.AddPage

function AddPage: TPdfPage;

在當前PDF文件中新增一個頁面。

TPdfDocument.SaveToFile

function SaveToFile(const aFileName: TFileName): boolean;

將PDF檔案內容儲存到指定的檔案中。

引數

aFileName PDF文件的名稱。

返回值

在任何寫入錯誤時返回 False(例如,如果檔案在PDF閱讀器中開啟)。

TPdfDocumentGDI.VCLCanvas

property VCLCanvas: TCanvas;

當前頁面的 VCL Canvas。

TPdfDocument.DefaultPaperSize

property DefaultPaperSize: TPDFPaperSize;

預設的頁面大小,用於每個新頁面的建立(例如AddPage方法呼叫)。

寫入此屬性會將預設紙張方向重置為縱向:如果需要,必須顯式地將DefaultPageLandscape設定為true。

TPdfDocument.DefaultPageLandscape

property DefaultPageLandscape: boolean;

預設的頁面方向。

如果方向不正確,呼叫此屬性將交換預設頁面寬度和高度。

TPdfPage.PageWidth

property PageWidth: integer;

當前頁面寬度,以PDF座標1/72英寸表示。

TPdfPage.PageHeight

property PageHeight: integer;

當前頁面高度,以PDF座標1/72英寸表示。

TPdfDocument.Info

property Info: TPdfInfo;

有關PDF文件的資訊。

TPdfInfo.Author

property Author: string;

指定生成文件中的作者。

TPdfInfo.Creator

property Creator: string;

指定生成文件中的生成器。

TPdfInfo.Keywords

property Keywords: string;

指定生成文件中的關鍵字。

TPdfInfo.Subject

property Subject: string;

指定生成文件的主題。

TPdfInfo.Title

property Title: string;

指定生成文件的標題。

TPDFPaperSize

可用的已知紙張大小。

unit

SynPdf

TPDFPaperSize = (
  psA4, psA5, psA3, psA2, psA1, psA0, psLetter, psLegal, psUserDefined);

例子

建立文件

uses SynPdf;

procedure TForm1.Button1Click(Sender: TObject);
var
  Pdf: TPdfDocumentGDI;
begin
  //建立PDF文件
  Pdf := TPdfDocumentGDI.Create;
  try
    //新增頁面
    Pdf.AddPage;
    //向Canvas輸出內容
    with Pdf.VCLCanvas do
    begin
      Pen.Color := clRed;
      Pen.Width := 2;
      Brush.Color := clInfoBk;
      Rectangle(100, 100, 400, 200);
      Font.Name := '宋體';
      Font.Size := 20;
      TextOut(200, 120, '測試內容');
      Pen.Color := clYellow;
      Pen.Width := 5;
      MoveTo(100, 250);
      LineTo(400, 250);
    end;
    //新增新頁面,Canvas指向新增頁面
    Pdf.AddPage;
    with Pdf.VCLCanvas do
    begin
      Font.Name := '宋體';
      Font.Size := 20;
      TextOut(200, 120, '新增頁面');
    end;
    //儲存檔案
    Pdf.SaveToFile('C:\Users\Administrator\Desktop\ceshi.pdf');
  finally
    Pdf.Free;
  end;
end;

新增頁面

uses SynPdf;

procedure TForm1.Button2Click(Sender: TObject);
var
  Pdf: TPdfDocumentGDI;
  Page: TPdfPage;
begin
  //建立PDF文件
  Pdf := TPdfDocumentGDI.Create;
  try
    //設定預設頁面大小
    Pdf.DefaultPaperSize := psA4;
    //新增頁面
    Pdf.AddPage;
    Page := Pdf.AddPage;
    //設定指定頁面大小
    Page.PageWidth := 400;
    Page.PageHeight := 300;
    Pdf.SaveToFile('C:\Users\Administrator\Desktop\ceshi.pdf');
  finally
    Pdf.Free;
  end;
end;

新增文件資訊

uses SynPdf;

procedure TForm1.Button3Click(Sender: TObject);
var
  Pdf: TPdfDocumentGDI;
begin
  //建立PDF文件
  Pdf := TPdfDocumentGDI.Create;
  try
    Pdf.AddPage;
    //設定文件的資訊
    with Pdf.Info do
    begin
      Title := '文件標題';
      Subject := '文件主題';
      Author := '文件作者';
      Creator := '文件生成器';
      Keywords := '文件關鍵字';
    end;
    Pdf.SaveToFile('C:\Users\Administrator\Desktop\ceshi.pdf');
  finally
    Pdf.Free;
  end;
end;