WPCubed Product Support

Watermarking example?

support

I now posted a new build which includes a new PDFStamp demo. It also lets you preview the changed PDF file before the cube (demo protection) is added.

#9 ♥ 0
14.1k Views 1 links 2 users read 7 min
support

The cube which is displayed on all pages is actually the demo DLL protection. All modified files will get this stamp if they are created with the demo of WPViewPDF.

Before the objects are saved with a PDF file they need to be rendered into the file using:

// Render the Draw Objects into the file
WPPDF.CommandEx(COMPDF_RenderDrawobjects, 4 + 8 + 32 + 64); // Render
WPPDF.ClearDrawObject(-1, -1); // and Clear all

With this code it show a visible stamp - however the stamping collides the with the demo protection code (the cube) and the watermark is displayed near that cube.

I posted the modified project here
[](http://www.wpcubed.com/ftp2/ApplyWatermarkDemo.zip)

#8 ♥ 0
rackerson

Sorry for replying so late, I was away for a couple of days.

About the error I get when using your MetafileOverlay demo, I will email that to you now.

In the meantime I tried to push on with my own prototype and I have a couple of more questions.

I am using Delphi 5 and the following logic to loop through all pages in my PDF to apply a watermark to specified pages:

type
  TWatermarkPage = (wpAll, wpFirst, wpLast, wpEven, wpOdd);
// ------------------------------------------------------------
// PageNeedsWatermark determines by page number if a given page is
// supposed to have a watermark applied.
// Returns True if watermark is needed, False otherwise
// ------------------------------------------------------------
function TForm3.PageNeedsWatermark(aSpec: TWatermarkPage; const PageCount, CurrentPage: Integer): Boolean;
var
  IsEvenPage: Boolean;
begin
  IsEvenPage := CurrentPage mod 2 = 0;
  Result := (aSpec = wpAll) or
            ((aSpec = wpFirst) and (CurrentPage = 1)) or
            ((aSpec = wpLast) and (CurrentPage = PageCount)) or
            ((aSpec = wpEven) and IsEvenPage) or
            ((aSpec = wpOdd) and not IsEvenPage);
end;

// ------------------------------------------------------------
// WatermarkPDFUsingWPViewPDF loops through all of the pages of a PDF
// file and applies a text watermark as needed.
// ------------------------------------------------------------
procedure TForm3.WatermarkPDFUsingWPViewPDF;
var
  WPPDF: TWPViewPDF;
  T: TPDFDrawObjectRec;
  cnt: Integer;
begin
  // Get the Watermark text from edit control on screen...
  FWatermarkText := ReadWatermarkText;
  // Get the transparency from edit control on screen...
  FTransparencyPercent := ReadTransparencyPercent;
  // Get the angle of rotation from edit control on screen...
  FRotationAngle := ReadRotationAngleDetails;
  // Get the enumeration for pages to apply watermark to from edit control on screen...
  FTargetPages := ReadTargetPagesForWatermark;
  // create WPViewPDF instance
  WPPDF := TWPViewPDF.Create(Self);
  WPPDF.Parent := Self;
  // load a PDF file
  WPPDF.LoadFromFile(frmTesterMain.InputFile);

  // loop through all pages of the PDF
  for cnt := 0 to WPPDF.PageCount-1 do
  begin
    // FTargetPages holds the pages I want to watermark.
    // PageNeedsWatermark compares the cur
    if PageNeedsWatermark(FTargetPages, WPPDF.PageCount, cnt+1) then
    begin
      FillChar(T, SizeOf(T), 0);
      T.PageNo := cnt;
      T.grtyp := 100;
      T.ColorText := clRed;
      T.Alpha := FTransparencyPercent;
      T.Angle := FRotationAngle;
      T.ObjectOptions := 64;
      T.units_xywh := 10; // 720 dpi
      T.x := Round( 2/2.54 * 720); // 2 cm
      T.y := Round( 3/2.54 * 720); // 3 cm
      T.w := Round( 5/2.54 * 720);
      T.h := Round( 1/2.54 * 720);
      WPPDF.AddDrawObject(wpAddNow, WideString('TEXTOBJECT'), T, nil, WideString(FWatermarkText));
    end;
  end;
  // red the output file name from edit control on screen...
  FOutputFileName := ReadOutputFileName;
  // save the watermarked PDF...
  WPPDF.Plus.SaveToFile(FOutputFileName);
  // Free the WPViewPDF instance...
  WPPDF.Free;
end;

My questions:

  1. in the above code I am trying to insert a text watermark but I continually get a PDF with a large 3D rectangular image drawn instead.
  2. The loop in the code above correctly prevents processing of certain pages of the PDF, which prevents the AddDrawObject method from being called. Despite this, every page of my PDF always gets a watermark. So obviously I am doing something very wrong. I just don't see what.

What do I need to change to the code above to make it print a text watermark and to have it not put the watermark on every page?

#7 ♥ 0
rackerson

Thanks Julian.

I thought there might be something Delphi 5-related going on here.

I applied your type casts and the demo now starts up (after I manually copy the wPDFViewDemo03.dll into the folder containing the demo's EXE file). Unfortunately, it doesn't allow me to load a PDF to work with. I have tried 4 different PDF files, ranging in complexity from a single page ReadMe document with just text to a multi-page document with graphics and text (no form stuff though).

When I click the OPEN toolbar button, I see my File Open dialog and I select a PDF. When I close the dialog, I assume I am supposed to see the PDF loaded in the viewed in the demo. Nothing ever shows up. If I drag the vertical scrollbar at this time I get a "List Index out of Bounds (-1)" error. When I close the demo after trying to load a PDF I get the exception:
"Exception EAccessViolation in module wPDFViewDemo03.dll at 002A6EC5.
Access violation at address 04416EC5 in module 'wPDFViewDemo03.dll'. Read of address 00000000."

I tried adding a breakpoint to trace execution to see if I could tell where something was failing but nothing obvious presented itself. Do you have any ideas of what might be causing my problem?

#5 ♥ 0
support

Hi,

sorry about this - it is a Delphi 5 Problem. That compiler cannot differentiate between strings and widestrings in function calls very well.

It works with 3 added type casts:

WPViewPDF1.AddDrawObject(wpAddNow, WideString(''), t, WideString('This is a test'), PAnsiChar('Font=Arial'),0);

The Demo is for V3, WPViewPDF V2 did by far not have the possibility nor the rendering quality of V3.

WPViewPDF is using a DLL built in the engine. It would not be possible to compile the code required for WPViewPDF with Delphi 5.

The "outer" part is provided as source, also in the demo.

#4 ♥ 0
rackerson

Thank you for the reply. Your comments helped a lot.

I noticed the MetafileOverlay demo but was not able to use it because all attempts to compile or run it resulted in errors with the parameters in the AddDrawObject method ("There is no overloaded version of 'AddDrawObject' that can be called with these arguments").

I didn't know if the demo was written to support WPViewPDF 2 and I was using 3 or if the problem was something to do with my old Delphi version. Either way, once I hit those errors I just moved on and looked for something else to test with.

One thing I do notice in that demo is the loading of a DLL. Is that a requirement of WPViewPDF Plus?

#3 ♥ 0
support

There is a demo called MetafileOverlay. It shows how to use the "DrawObjects" to create objects on the page.

The DrawObjects are always only created on one page, not on a range of pages. It would be necessary to add them in a loop.

This sample creates a colored highlight:

var  t: TPDFDrawObjectRec;begin  FillChar(t, SizeOf(t), 0);  t.PageNo := 0; // Page 1  t.ColorBrush := clYellow; // clYellow;  t.Alpha := 100; // transparent  t.grtyp := 1; // Rectangle  t.ObjectOptions := 16+64; // +32; // Apply Highlight color, dont move  // Position, 720 dpi  t.units_xywh := 10; // 720 dpi  t.x := Round( 2/2.54 * 720); // 2 cm  t.y := Round( 3/2.54 * 720); // 3 cm  t.w := Round( 5/2.54 * 720);  t.h := Round( 1/2.54 * 720);  WPViewPDF1.AddDrawObject(wpAddNow, 'YELLOW', t, nil, '');  WPViewPDF1.CommandEx(COMPDF_SelectMode, wpmouse_SelectObject)end;

WPViewPDF1.CommandEx(COMPDF_SelectMode, wpmouse_SelectObject) is required to make the object movable.

This different types ( t.grtyp ) are supported:

// 0=default highlight (alpha=120)
// 1=rectangle
// 2=circle
// 3=ellipse
// 20= Image
// 100= Text

To add the text "DRAFT" You can use this:

FillChar(t, SizeOf(t), 0);
  t.PageNo := 0; // Page 1
  t.grtyp := 100; // Rectangle
  t.Angle := 45;
  t.ColorText := clRed;
  t.ObjectOptions := 64; // dont move
  // Position, 720 dpi
  t.units_xywh := 10; // 720 dpi
  t.x := Round( 2/2.54 * 720); // 2 cm
  t.y := Round( 3/2.54 * 720); // 3 cm
  t.w := Round( 5/2.54 * 720);
  t.h := Round( 1/2.54 * 720);
  WPViewPDF1.AddDrawObject(wpAddNow, 'TEXTOBJECT', t, nil, '**DRAFT**');
#2 ♥ 0
rackerson

Hello,
I am evaluating WPViewPDF Plus to add PDF watermarking abilities to my Delphi 5 application. I downloaded the demo and experimented a bit but I'm still not sure how to handle what I need to do.
My requirements. The user must be able to define:

  1. Text to insert as a watermark
  2. Watermark font type, style, color, and size
  3. Watermark opacity
  4. Watermark location (page top/middle/bottom, left/center/right side of page, or even X/Y coordinates on page)
  5. Page range to apply watermark to (all, even only, odd only, specific page numbers, etc)
  6. Watermark orientation (horizontal, vertical)
  7. Watermark rotation

Is all of the following available from WPViewPDF Plus? Also, is there any sample code that demonstrates how to do at least some of the above?

Thank you.
Rich

#1 ♥ 0