Draw Shapes / Text objects on PDF

<< Click to Display Table of Contents >>

Navigation:  Tasks >

Draw Shapes / Text objects on PDF

WPViewPDF offers the unique feature of "Draw Objects".

 

This objects can be text and circle shapes with different color and transparency. Also possible are single text lines and images.

It is possible to move the objects under program control or the user can move and resize the object.

 

New: With WPViewPDF V4 the objects can also be created to belong to the "document" instead of a certain PDF file. (Currently images cannot be document dependent). With WPViewPDF Plus is is possible to save the objects to XML and load in this format.  (command COMPDF_DrawObjects_XML)

 

This makes it possible to load a different PDF file with the objects stay at the same place on a certain page number!

 

The draw objects are always rendered independently of the PDF page contents - this avoids flickering and slow downs while the user move the objects around, even on PDF pages which are rendered slowly.

 

Please note: You need to call

  Command(COMPDF_RenderDrawobjects, 8 + 32 + 64 )

to render the objects into the PDF file when you need to save them into the PDF files.

 

 

New: It is also possible to trigger an mail-merge event for text objects - so they can be updated on each time they are painted!

 

WPViewPDF PLUS: Draw objects can be "rendered to the page". This applies postscript code which becomes part of the page contents. The draw objects are not automatically deleted and can be moved around and rendered a a different place -or- if they belong to the "document", be applied to a different PDF file!

 

The modified PDF file can also be saved as a new PDF file.

 

You can use this feature to highlight certain areas on the PDF file, for display or print.

 

To create a shape this commands can be used:

COMPDF_AddDrawObject       = 518

COMPDF_MouseAddDrawObject  = 520

COMPDF_MouseAddOneDrawObject=521

 

Hint: Using COMPDF_DrawObjectLocateAtXY it is possible to get the name of the object under the mouse pointer. This makes it possible to create sensible areas on a PDF page, i.e. buttons.

 

"AddDrawObject" will immediately add a shape to a certain page. "MouseAddDrawObject" will switch the cursor in a special mode which lets the user draw a rectangle. After the rectangle has been drawn, the shape will be created. The user can then draw another object, unless "MouseAddOneDrawObject" was used, then the mouse switches into selection mode.

 

This method can be used to create objects. They wrap the call of the command.

 

procedure AddDrawObject(

 Mode : TWPAddDrawObjectMode;

 Name : WideString;

 var Param : TPDFDrawObjectRec;

 data : TMemoryStream = nil;

 StrParam : WideString = '');

 

Mode can have this values:

    wpAddNow - Add a new object at once

    wpDrawAndAdd - select the object draw mode. The user can draw a rect and a new object will be created

    wpDrawAndAddOne - like wpDrawAndAdd but only one object will be created. The viewer goes then in select mode

    wpMoveExistingObj- Don't add an object. Adds to the X,Y W and H properties.

    wpModifyExistingObj- Don't add an object. Modifies the named object according to the bitfield "fields"

 

Name is optional. It is the name of the shape which makes it possible to access it later.

 

Param is a record of type TPDFDrawObjectRec. It should hold the required values for color and type, but no attached data.

 

data is reserved.

 

StrParam is used for text.

 

The overloaded method allows the data top be passed as pointer:

 

procedure AddDrawObject(Mode : TWPAddDrawObjectMode; Name : WideString; var Param : TPDFDrawObjectRec; StrParam : WideString; data : PAnsiChar=nil; datalen : Integer = 0); overload;

 

 

Example:

This Delphi dialog will let the user select an image file. Now he or she may draw a rectangle on the page where the image will be displayed:

 

procedure TMetafileOverlay.DrawJPEGClick(Sender: TObject);

var

  t: TPDFDrawObjectRec;

  i: Integer;

begin

  if OpenPictureDialog1.Execute then

  begin

    i := WPViewPDF1.Plus.AddImage(OpenPictureDialog1.FileName);

    if i > 0 then

    begin

      FillChar(t, SizeOf(t), 0);

      t.grtyp := 20; // Image

      t.typparam := i;  // Image ID

      t.ColorBrush := $B0B0B0; // gray

      t.ObjectOptions := OBJGR_KEEP_ASPECTRATIO+ OBJGR_OPAQUE;

      t.Padding := 100; // Padding in 1/10 Point

      //t.Angle := 30;

      ShowMessage('Please draw rectangle ...');

      WPViewPDF1.CommandStrEx(COMPDF_MouseAddOneDrawObject, '', Cardinal(@t));

    end

    else

      ShowMessage('Cannot load image');

  end;

end;

 

 

This code creates a text field  

 

procedure TCertificatePrint.InsertFieldClick(Sender: TObject);

var Param: TPDFDrawObjectRec;

begin

  FillChar(Param, SizeOf(Param), 0);

  Param.PageNo := WPViewPDF1.Page-1

  Param.grtyp := 100;

 

  Param.w := 100;

  Param.h := 30;

  Param.ObjectOptions := OBJGR_KEEP_ASPECTRATIO + OBJGR_STRETCH+ OBJGR_CENTER + OBJGR_MERGE;

 

  Param.CreateOptions := 8192 * 8; 

 

  WPViewPDF1.AddDrawObject(wpAddNow, cbField.Text, Param, '***' );

end;