llPDFLib 文档设置
属性和方法
TPDFDocument.OutputStream
property OutputStream: TStream;
设置了此属性,则生成文档的输出在流中,而不是在文件中。
TPDFDocument.OnePass
property OnePass: Boolean;
直接创建文档。
创建大型文档时建议使用此属性。当创建下一个页面时,画布的内容将被直接写入输出流。与此相关的是无法更改
CurrentPageIndex
。如果指定了
OutputStream
,该值将被忽略。
TPDFDocument.Compression
property Compression: TCompressionType;
指定是否对PDF文档中画布的内容使用压缩。
TPDFDocument.NonEmbeddedFonts
property NonEmbeddedFonts: TStringList;
设置不被嵌入到文档中的TTF字体列表。
TPDFDocument.Printing
property Printing: Boolean;
判断组件是否正在创建新文档。
TPDFDocument.Abort
procedure Abort;
停止创建PDF文档。发送到文件的所有数据都将丢失。
TCompressionType
指定页面内容压缩。
unit
llPDFTypes
TCompressionType = (
ctNone,
ctFlate
);
-
ctNone 不压缩
-
ctFlate 使用平面压缩进行压缩
例子
输出到流
uses llPDFDocument;
procedure TForm1.Button12Click(Sender: TObject);
var
Pdf: TPDFDocument;
Stream: TMemoryStream;
begin
Pdf := TPDFDocument.Create(nil);
Stream := TMemoryStream.Create;
try
//设置文档输出流
Pdf.OutputStream := Stream;
Pdf.BeginDoc;
Pdf.EndDoc;
//将流中的内容输出到文件
Stream.SaveToFile('C:\Users\Administrator\Desktop\ceshi.pdf');
finally
Stream.Free;
Pdf.Free;
end;
end;
设置不嵌入字体
procedure TForm1.Button13Click(Sender: TObject);
var
Pdf: TPDFDocument;
begin
Pdf := TPDFDocument.Create(nil);
try
//创建PDF文档
Pdf.AutoLaunch := True;
Pdf.FileName := 'C:\Users\Administrator\Desktop\ceshi.pdf';
//设置不嵌入文档的字体(只能设置TTF字体,可以减小文档的大小)
Pdf.NonEmbeddedFonts.Add('Tahoma');
Pdf.NonEmbeddedFonts.Add('Arial');
Pdf.BeginDoc;
with Pdf.Canvas do
begin
Font.Name := 'Tahoma';
Font.Size := 20;
TextOut(100, 100, 'Tahoma');
Font.Name := 'Arial';
Font.Size := 20;
TextOut(100, 150, 'Arial');
end;
Pdf.EndDoc;
finally
Pdf.Free;
end;
end;
停止创建文档
procedure TForm1.Button14Click(Sender: TObject);
var
Pdf: TPDFDocument;
I: Integer;
begin
Pdf := TPDFDocument.Create(nil);
try
//创建PDF文档
Pdf.AutoLaunch := True;
Pdf.FileName := 'C:\Users\Administrator\Desktop\ceshi.pdf';
Pdf.BeginDoc;
with Pdf.Canvas do
begin
for I := 0 to 50 do
TextOut(100, 50 + I * 20, IntToStr(I));
end;
//停止创建文档
if Pdf.Printing then Pdf.Abort;
finally
Pdf.Free;
end;
end;
创建大文件
uses llPDFDocument, llPDFTypes;
procedure TForm1.Button15Click(Sender: TObject);
var
Pdf: TPDFDocument;
I, J: Integer;
begin
Pdf := TPDFDocument.Create(nil);
try
//创建PDF文档
Pdf.AutoLaunch := True;
Pdf.FileName := 'C:\Users\Administrator\Desktop\ceshi.pdf';
//直接创建文档,创建大型文档时使用(设置OutputStream后不起作用)
Pdf.OnePass := True;
//压缩内容
Pdf.Compression := ctFlate;
Pdf.BeginDoc;
for I := 1 to 1000 do
begin
if I <> 1 then
Pdf.NewPage;
with Pdf.Canvas do
begin
Font.Name := '宋体';
Font.Size := 11;
for J := 1 to 50 do
TextOut(20, J * 20, IntToStr(I) + '页' + IntToStr(J) + '行 *******************');
end;
end;
Pdf.EndDoc;
finally
Pdf.Free;
end;
end;