Watermarking example?

  • 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

    • Offizieller Beitrag

    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:

    Code
    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:

  • 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?

    • Offizieller Beitrag

    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.

  • 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?

  • 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:

    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?

    • Offizieller Beitrag

    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
    https://www.wpcubed.com/ftp2/ApplyWatermarkDemo.zip