📄 pdfimages.pas
字号:
{*
* << P o w e r P d f >> -- PdfImages.pas
*
* Copyright (c) 1999-2001 Takezou. <takeshi_kanno@est.hi-ho.ne.jp>
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Library General Public License as published
* by the Free Software Foundation; either version 2 of the License, or any
* later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library.
*
* Create 2001.03.14
*
*}
unit PdfImages;
interface
uses
SysUtils, Classes, Graphics, PdfTypes, PdfDoc;
type
TPdfBitmapImage = class(TPdfImageCreator)
public
function CreateImage(AXref: TPdfXref; AImage: TGraphic): TPdfXObject; override;
end;
implementation
function TPdfBitmapImage.CreateImage(AXref: TPdfXref; AImage: TGraphic): TPdfXObject;
var
ABitmap: TBitmap;
x, y: integer;
pb: PByteArray;
b: Byte;
begin
// check whether specified graphic is valid image.
if not (AImage is TBitmap) then
raise EPdfInvalidValue.Create('only bitmap is allowed.');
result := TPdfXObject.CreateStream(AXref);
with result do
try
with Attributes do
begin
AddItem('Type', TPdfName.CreateName('XObject'));
AddItem('Subtype', TPdfName.CreateName('Image'));
AddItem('Width', TPdfNumber.CreateNumber(0));
AddItem('Height', TPdfNumber.CreateNumber(0));
AddItem('BitsPerComponent', TPdfNumber.CreateNumber(0));
AddItem('ColorSpace', TPdfName.CreateName('DeviceRGB'));
end;
ABitmap := TBitmap.Create;
try
ABitmap.Assign(AImage);
ABitmap.PixelFormat := pf24Bit;
// translate TBitmap object to pdf image format.
for y := 0 to ABitmap.Height-1 do
begin
pb := ABitmap.ScanLine[y];
x := 0;
while x < ABitmap.Width * 3 - 1 do
begin
b := pb[x];
pb[x] := pb[x+2];
pb[x+2] := b;
x := x + 3;
end;
Stream.Write(pb^, ABitmap.Width*3);
end;
with Attributes do
begin
PdfNumberByName('Width').Value := ABitmap.Width;
PdfNumberByName('Height').Value := ABitmap.Height;
PdfNumberByName('BitsPerComponent').Value := 8;
if USE_ZLIB then
PdfArrayByName('Filter').AddItem(TPdfName.CreateName('FlateDecode'));
end;
finally
ABitmap.Free;
end;
except
result.Free;
raise;
end;
end;
initialization
RegisterClassAlias(TPdfBitmapImage, 'Pdf-Bitmap');
finalization
UnRegisterClass(TPdfBitmapImage);
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -