1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
| unit PrintPDF;
interface
uses SysUtils, Graphics, hpdf, hpdf_types, hpdf_consts, Math, StrUtils;
type
TFonts = record
Name :string;
Size :HPDF_REAL;
end;
TPrintPDF = class
private
FCurPos :THPDF_Point;
FFonts :array of TFonts;
Doc :HPDF_Doc;
font :HPDF_Font;
page :HPDF_Page;
FMargins :THPDF_Rect;
FTitle: string;
FAuthor: string;
FSubject: string;
FCreator: string;
FKeywords: string;
function GetCurPos: THPDF_Point;
procedure SetCurPos(const Value: THPDF_Point);
procedure SetMargins(const Value: THPDF_Rect);
function GetPageSize: THPDF_Point;
function GetInnerPageSize: THPDF_Point;
function GetFontSize: HPDF_REAL;
procedure SetTitle(const Value: string);
procedure SetAuthor(const Value: string);
procedure SetCreator(const Value: string);
procedure SetKeywords(const Value: string);
procedure SetSubject(const Value: string);
public
procedure NewPage;
procedure AddFont(aName :string; aSize :HPDF_REAL);
procedure SelectFont(aIndex :integer);
procedure IncCurPosY(aValue :HPDF_REAL);
procedure IncCurPosX(aValue :HPDF_REAL);
procedure SaveToFile(aFileName :TFileName);
procedure Write(aText :string; aFontIndex :integer = -1);
procedure WriteLn(aText :string = ''; aFontIndex :integer = -1);
procedure TextOut(X, Y: HPDF_REAL; aText :string; aFontIndex :integer = -1);
procedure WriteLink(aText, aURL :string);
procedure WriteLinkLn(aText, aURL :string);
procedure DrawImage(aFileName :TFileName; X, Y, W, H :HPDF_REAL);
function TextWidth(aText :string) :HPDF_REAL;
procedure Rectangle(aLeft, aTop, aWidth, aHeight :HPDF_REAL; aLineWidth: HPDF_REAL = 0.3; aLineColor :TColor = $000000; aFillColor :TColor = $FFFFFF);
constructor Create(aFontName :string = 'Helvetica'; aFontSize :HPDF_REAL = 10);
destructor Destroy; override;
published
property PageSize :THPDF_Point read GetPageSize;
property FontSize :HPDF_REAL read GetFontSize;
property InnerPageSize :THPDF_Point read GetInnerPageSize;
property Margins :THPDF_Rect read FMargins write SetMargins;
property CurPos :THPDF_Point read GetCurPos write SetCurPos;
property Title :string read FTitle write SetTitle;
property Author :string read FAuthor write SetAuthor;
property Creator :string read FCreator write SetCreator;
property Subject :string read FSubject write SetSubject;
property Keywords :string read FKeywords write SetKeywords;
end;
function Pointmm(X, Y :HPDF_REAL) :THPDF_Point;
function Rectmm(aLeft, aTop, aRight, aBottom :HPDF_REAL) :THPDF_Rect;
function Incmm(var aValue :HPDF_REAL; aInc :HPDF_REAL) :HPDF_REAL;
implementation
const
DefaultFontSize :HPDF_REAL = 10;
function Pointmm(X, Y :HPDF_REAL) :THPDF_Point;
begin
Result.X := X;
Result.Y := Y;
end;
function Rectmm(aLeft, aTop, aRight, aBottom :HPDF_REAL) :THPDF_Rect;
begin
Result.Left := aLeft;
Result.Top := aTop;
Result.Right := aRight;
Result.Bottom := aBottom;
end;
function Incmm(var aValue :HPDF_REAL; aInc :HPDF_REAL) :HPDF_REAL;
begin
aValue := aValue +aInc;
Result := aValue;
end;
procedure error_handler (error_no: HPDF_STATUS; detail_no: HPDF_STATUS; user_data: Pointer); stdcall;
begin
raise Exception.Create(Format('Error: %d - %d', [Error_No, Detail_No]));
end;
{ TPrintPDF }
procedure TPrintPDF.AddFont(aName: string; aSize: HPDF_REAL);
var
i :integer;
begin
i := High(FFonts) +1;
SetLength(FFonts, i +1);
FFonts[i].Name := aName;
FFonts[i].Size := aSize;
end;
constructor TPrintPDF.Create(aFontName :string; aFontSize :HPDF_REAL);
begin
Doc := HPDF_New(@error_handler, nil);
HPDF_SetCompressionMode(Doc, HPDF_COMP_ALL);
font := HPDF_GetFont(Doc, PChar(aFontName), 'WinAnsiEncoding');
Margins := Rectmm(5, 5, 5, 5);
NewPage;
AddFont(aFontName, aFontSize);
SelectFont(0);
end;
destructor TPrintPDF.Destroy;
begin
HPDF_Free(Doc);
inherited;
end;
procedure TPrintPDF.DrawImage(aFileName: TFileName; X, Y, W, H: HPDF_REAL);
var
Image :HPDF_Image;
begin
case AnsiIndexText(ExtractFileExt(aFileName), ['.png', '.jpg']) of
0 : image := HPDF_LoadPngImageFromFile(Doc, PAnsiChar(aFileName));
1 : image := HPDF_LoadJpegImageFromFile(Doc, PAnsiChar(aFileName));
end;
HPDF_Page_DrawImage (page, image, X, PageSize.Y -Y -H, W, H);
end;
function TPrintPDF.GetCurPos: THPDF_Point;
begin
Result.X := FCurPos.X;
Result.Y := HPDF_Page_GetHeight(page) -FCurPos.Y;
end;
function TPrintPDF.GetFontSize: HPDF_REAL;
begin
Result := HPDF_Page_GetCurrentFontSize(page);
end;
function TPrintPDF.GetInnerPageSize: THPDF_Point;
begin
Result.x := PageSize.x -Margins.left -Margins.right;
Result.y := PageSize.y -Margins.Top -Margins.Bottom;
end;
function TPrintPDF.GetPageSize: THPDF_Point;
begin
Result.x := HPDF_Page_GetWidth(page);
Result.y := HPDF_Page_GetHeight(page);
end;
procedure TPrintPDF.IncCurPosX(aValue: HPDF_REAL);
begin
FCurPos.X := FCurPos.X +aValue;
end;
procedure TPrintPDF.IncCurPosY(aValue: HPDF_REAL);
begin
FCurPos.Y := FCurPos.Y -aValue;
end;
procedure TPrintPDF.NewPage;
var
Ofs :HPDF_REAL;
begin
page := HPDF_AddPage(Doc);
HPDF_Page_SetSize(Page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);
FCurPos.X := Margins.Left;
if IsZero(HPDF_Page_GetCurrentFontSize(page), 0.01)
then Ofs := DefaultFontSize
else Ofs := HPDF_Page_GetCurrentFontSize(page);
FCurPos.Y := HPDF_Page_GetHeight(page) -Margins.Top -Ofs;
end;
procedure TPrintPDF.Rectangle(aLeft, aTop, aWidth, aHeight :HPDF_REAL;
aLineWidth: HPDF_REAL; aLineColor, aFillColor: TColor);
var
R,G,B :byte;
begin
HPDF_Page_SetLineWidth (page, aLineWidth);
R := aLineColor and $FF;
G := (aLineColor shr 8) and $FF;
B := (aLineColor shr 16) and $FF;
HPDF_Page_SetRGBStroke (page, R/255, G/255, B/255);
R := aLineColor and $FF;
G := (aLineColor shr 8) and $FF;
B := (aLineColor shr 16) and $FF;
HPDF_Page_SetRGBFill (page, R/255, G/255, B/255);
HPDF_Page_Rectangle(page, aleft, HPDF_Page_GetHeight(page) -aTop +FontSize, aWidth, -aHeight);
// HPDF_Page_Rectangle(page, aleft, aTop +FontSize, aWidth, -aHeight);
HPDF_Page_Stroke(page);
end;
procedure TPrintPDF.SaveToFile(aFileName: TFileName);
begin
HPDF_SaveToFile(Doc, PAnsiChar(aFileName));
end;
procedure TPrintPDF.SelectFont(aIndex: integer);
begin
aIndex := EnsureRange(aIndex, 0, High(aIndex));
font := HPDF_GetFont(Doc, PChar(FFonts[aIndex].Name), 'WinAnsiEncoding');
HPDF_Page_SetFontAndSize(page, font, FFonts[aIndex].Size);
end;
procedure TPrintPDF.SetAuthor(const Value: string);
begin
FAuthor := Value;
HPDF_SetInfoAttr(Doc, HPDF_INFO_AUTHOR, PAnsiChar(FAuthor));
end;
procedure TPrintPDF.SetCreator(const Value: string);
begin
FCreator := Value;
HPDF_SetInfoAttr(Doc, HPDF_INFO_CREATOR, PAnsiChar(FCreator));
end;
procedure TPrintPDF.SetCurPos(const Value: THPDF_Point);
begin
FCurPos.X := Value.X;
FCurPos.Y := HPDF_Page_GetHeight(page) -Value.Y;
end;
procedure TPrintPDF.SetKeywords(const Value: string);
begin
FKeywords := Value;
HPDF_SetInfoAttr(Doc, HPDF_INFO_KEYWORDS, PAnsiChar(FKeywords));
end;
procedure TPrintPDF.SetMargins(const Value: THPDF_Rect);
begin
FMargins := Value;
CurPos := Pointmm(Max(CurPos.X, FMargins.Left), Max(CurPos.Y, FMargins.Top));
end;
procedure TPrintPDF.SetSubject(const Value: string);
begin
FSubject := Value;
HPDF_SetInfoAttr(Doc, HPDF_INFO_SUBJECT, PAnsiChar(FSubject));
end;
procedure TPrintPDF.SetTitle(const Value: string);
begin
FTitle := Value;
HPDF_SetInfoAttr(Doc, HPDF_INFO_TITLE, PAnsiChar(FTitle));
end;
procedure TPrintPDF.TextOut(X, Y: HPDF_REAL; aText: string; aFontIndex: integer);
var
OldCurPos :THPDF_Point;
begin
OldCurPos := CurPos;
CurPos := Pointmm(X, Y);
Write(aText, aFontIndex);
CurPos := OldCurPos;
end;
function TPrintPDF.TextWidth(aText: string): HPDF_REAL;
begin
Result := HPDF_Page_TextWidth(page, PAnsiChar(aText));
end;
procedure TPrintPDF.Write(aText: string; aFontIndex: integer);
begin
if aFontIndex > -1 then
SelectFont(aFontIndex);
HPDF_Page_BeginText(page);
HPDF_Page_TextOut(page, FCurPos.X, FCurPos.Y, PAnsiChar(aText));
HPDF_Page_EndText (page);
end;
procedure TPrintPDF.WriteLn(aText :string; aFontIndex :integer);
begin
Write(aText, aFontIndex);
IncCurPosY(HPDF_Page_GetCurrentFontSize(page));
end;
procedure TPrintPDF.WriteLink(aText, aURL: string);
var
Rect :THPDF_Rect;
Annot :HPDF_Annotation;
begin
Rect.Left := FCurPos.X;
Rect.top := FCurPos.Y +HPDF_Page_GetCurrentFontSize(page);
Rect.Right := Rect.Left +TextWidth(aText); //HPDF_Page_TextWidth(page, PAnsiChar(aText));
Rect.Bottom := FCurPos.Y -3;
Write(aText);
Annot := HPDF_Page_CreateURILinkAnnot(page, Rect, PAnsiChar(aURL));
HPDF_LinkAnnot_SetBorderStyle (annot, 0, 0, 0);
end;
procedure TPrintPDF.WriteLinkLn(aText, aURL: string);
begin
WriteLink(aText, aURL);
IncCurPosY(HPDF_Page_GetCurrentFontSize(page));
end;
end. |
Partager