<< Click to Display Table of Contents >> Navigation: Tasks > Draw Shapes / Text objects on PDF > Record TPDFDrawObjectRec |
The commands to create a shape require as parameter a pointer to the record TPDFDrawObjectRec.
It structure has this basis elements:
structsize - should be initialized as structsize = SizeOf(TPDFDrawObjectRec)
PageNo - the page number the shape should be created on (0 = first)
x,y,w,h - for COMPDF_AddDrawObject - the position from the upper left corner in points (1/72 inch). Note: A different resolution can be selected using the parameter units_xywh.
ColorBrush - the RGB color of the background
ColorPen | - the RGB color for the outline |
ColorText | - the RGB color for the text |
PenWidth | - the width of the outline in pt*100 |
Alpha | - the transparency in the range 0..255. 255 and 0 are solid. |
Angle | - the angle, used for text only |
Padding | - padding inside the bounds - used for images. |
FontSize | - the font size *100. Set to 0 when you need stretched text |
grtyp - select the shape type.
0 | : default highlight (alpha=120) |
1 | : rectangle |
2 | : circle |
3 | : ellipse |
20 | : Image. Use typparam as ID of the image. (add JPEG image with COMPDF_AddJPEG) |
100 | : Text. |
ObjectOptions - this is a bitfield to change attributes of object
1 | : Keep aspect ratio when adapting the size of image JPEG to the bounding box. New: This now also works for text objects. |
2 | : Stretch text to fill the rectangle. The FontSize should be 0 in this case. |
4 | : Center text horizontally in the box |
8 | : Used for text and JPEGs . Draw Background in selected Brush Color and Pen. |
16 | : Apply ColorBrush after painting the object using color multiplication to create highlight rectangles. |
This mode is only effective on screen, when rendering to PDF regular transparency will be used. |
The Alpha property should be also used. |
32 | : Once the object was created it cannot be moved anymore |
64 | : The size of the object cannot be changed by the user |
2048 | : Right align text horizontally in the box |
16384 | : New: Trigger merge event MSGPDF_DRAWOBJECT_GETTEXT for text objects. |
CreateOptions - how should the object be created. The following bits can be set
1 | : Place the object UNDER the Page |
2 | : Place at the Right Border of the page (ignore X) |
4 | : Place at the Bottom Border of the page (ignore Y) |
8 | : Scale the object to the page horizontally (uses X as right and left margin) |
16 | : Scale the object to the page vertically (uses Y as right and left margin) |
32 | : Create the object and select it (clear selection) |
64 | : Create the object and add it to the selction (do not clear selection) |
Offset Modes
128 | : Page_Center_Y = add y to page height / 2, subtract height / 2 |
256 | : Page_Bottom_Y = add y to page height, subtract height |
512 | : Page_Center_X = add x to page width /2, subtract width / 2 |
1024 | : Page_Right_X = add x to page width, subtract width |
Change meaning of W and H
2048 | : Width and Height are measured in % of Page Width and Height |
Various:
8192*2 | : Do NOT refresh the screen |
8192*4 | : Rotate the object boundaries backwards according to Page rotation |
This can be combined with Angle := PageRotation[PageNumber-1] to
Make sure a drawn object appears inside the drawn frame.
8192*8 | : New: Added to WPViewPDF Version 4: Place the object in the global document layer |
HRad, VRad - the vertical and horizontal radius to make rectangles round. (always Uses 720 dpi)
Other elements are either reserved or used only for certain objects.
At the end of the structure binary or text data can be stored. The offset to the data and the length has to be provided using this parameters. If you use the API AddDrawObject() You do not have to worry about this.
textoff - the offset to the text data - this must be unicode text
textlen - the length of the text data.
NameOff - the offset to the name using wide characters.
NameLen - the length of the name.
DataOff, Datalen - various data. DataTyp tells which:
1 = ANSI Text
2 = Unicode Text
The API AddDrawObject simplifies the use of the record since it copies the extra data.
In the VCL it is implemented like this:
procedure TWPViewPDF.AddDrawObject(Mode : TWPAddDrawObjectMode; Name : WideString;
var Param : TPDFDrawObjectRec; StrParam : WideString;
data : PAnsiChar=nil; datalen : Integer = 0);
var t : PPDFDrawObjectRec; tl, i : Integer;
p : PByte;
begin
tl := SizeOf(TPDFDrawObjectRec);
if Name<>'' then inc(tl, Length(Name)*SizeOf(WideChar));
if StrParam<>'' then inc(tl, Length(StrParam)*SizeOf(WideChar));
if data<>nil then
begin
if datalen=0 then datalen := StrLen(Data);
inc(tl, datalen);
end;
GetMem(t, tl);
t^ := Param;
t.structsize := tl;
try
p := PByte(t);
i := SizeOf(TPDFDrawObjectRec);
inc(p, i);
if Name<>'' then
begin
t.NameOff := i;
t.NameLen := Length(Name);
Move(Name[1], p^, t.NameLen*SizeOf(WideChar) );
inc(i, t.NameLen*SizeOf(WideChar) );
inc(p, t.NameLen*SizeOf(WideChar) );
end else t.NameOff := 0;
if StrParam<>'' then
begin
t.textoff := i;
t.textlen := Length(StrParam);
Move(StrParam[1], p^, t.textlen*SizeOf(WideChar) );
inc(i, t.textlen*SizeOf(WideChar) );
inc(p, t.textlen*SizeOf(WideChar) );
end else t.textoff := 0;
if data<>nil then
begin
t.DataOff := i;
t.Datalen := datalen;
Move(data^, p^, datalen );
end else t.DataOff := 0;
case Mode of
wpAddNow: CommandStrEx(COMPDF_AddHighlightRect, Name, Cardinal(t));
wpDrawAndAdd: CommandStrEx(COMPDF_MouseAddDrawObject, Name, Cardinal(t));
wpDrawAndAddOne: CommandStrEx(COMPDF_MouseAddOneDrawObject, Name, Cardinal(t));
wpMoveExistingObj: CommandStrEx(COMPDF_ModifyDrawObjectPos, Name, Cardinal(t));
wpModifyExistingObj: CommandStrEx(COMPDF_SetDrawObjectProp, Name, Cardinal(t));
end;
finally
FreeMem(t);
end;
end;
The .NET assembly also defines this AddDrawObject. It takes a structure of type TPDFDrawObjectRec as parameter.
public void AddDrawObject(AddDrawObjectMode Mode, string Name, TPDFDrawObjectRec Param, string Text)
See examples here....