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;