Watermark on any Navision Report

gvolkovgvolkov Member Posts: 196
edited 2011-01-05 in NAV Tips & Tricks
Normally, everyone whould think that its impossible to use Watermarks on Navision reports, however.... i thought i would share this with everyone. If you need to insert a watermark on any report without messing with PCL or PS code on your printer, here is what i have done. Oh and watermark is a variable.... so you can make it dynamic. I my case its field 50000 on Sales Header - "Order Type". So anytime you print a document it inserts Order Type accross the document. Check it out...

1. install PdfCreator
2. install adobe reader 8.0
3. Create this codeunit:
OBJECT Codeunit 50003 WaterMark
{
  OBJECT-PROPERTIES
  {
    Date=06/14/07;
    Time=[ 3:32:01 PM];
    Modified=Yes;
    Version List=;
  }
  PROPERTIES
  {
    OnRun=BEGIN
          END;

  }
  CODE
  {
    VAR
      FileDirectory@1000000012 : Text[100];
      FileName@1000000011 : Text[100];
      ReportID@1000000010 : Integer;
      Object@1000000009 : Record 2000000001;
      PDFCreator@1000000008 : Automation "{1CE9DC08-9FBC-45C6-8A7C-4FE1E208A613} 4.1:{3A619AE4-50EC-46C8-B19E-BE8F50DD2F22}:'PDFCreator'.clsPDFCreator" WITHEVENTS;
      PDFCreatorOption@1000000007 : Automation "{1CE9DC08-9FBC-45C6-8A7C-4FE1E208A613} 4.1:{F8F15298-30FD-427C-BDFA-55E9AB615632}:'PDFCreator'.clsPDFCreatorOptions";
      PDFCreatorError@1000000006 : Automation "{1CE9DC08-9FBC-45C6-8A7C-4FE1E208A613} 4.1:{082391C9-8188-4364-B4FD-66A1524B2097}:'PDFCreator'.clsPDFCreatorError";
      DefaultPrinter@1000000005 : Text[200];
      Window@1000000004 : Dialog;
      WindowisOpen@1000000003 : Boolean;
      FileDialog@1000000002 : Codeunit 412;
      watermark@1000000001 : Text[30];
      adobe@1000000000 : Automation "{E64169B3-3592-47D2-816E-602C5C13F328} 1.1:{72498821-3203-101B-B02E-04021C009402}:'Adobe Acrobat 8.0 Type Library'.AcroAVDoc";
      SalesHeader@1000000013 : Record 36;

    PROCEDURE makepdf@1000000000(rSpecifier@1000000000 : Code[10];OrderType@1000000001 : Code[10]);
    BEGIN
      FileDirectory := 'C:\';
      FileName := 'pdf_file.pdf';

      findsalesheader(rSpecifier);

      IF ISCLEAR(PDFCreator) THEN
        CREATE(PDFCreator);
      IF ISCLEAR(PDFCreatorError) THEN
        CREATE(PDFCreatorError);

      PDFCreatorError := PDFCreator.cError;

      IF PDFCreator.cStart('/NoProcessingAtStartup',TRUE) = FALSE THEN
           ERROR('Status: Error[' + FORMAT(PDFCreatorError.Number) + ']: ' + PDFCreatorError.Description);


      Window.OPEN('processing');
      WindowisOpen := TRUE;
      IF FileName = '' THEN
        ERROR('Please specify what the file should be saved as');

      //Object.GET(Object.Type::Report,'',ReportID);

      PDFCreatorOption :=  PDFCreator.cOptions;


      PDFCreatorOption.UseAutosave := 1;
      PDFCreatorOption.UseAutosaveDirectory := 1;
      PDFCreatorOption.AutosaveDirectory := FileDirectory;
      PDFCreatorOption.AutosaveFormat := 0;                       //PDF file, you can also save in other formats
      PDFCreatorOption.AutosaveFilename := FileName;

      PDFCreatorOption.StampFontname := 'Arial';
      PDFCreatorOption.StampFontsize := 150;
      PDFCreatorOption.StampUseOutlineFont := 1;
      PDFCreatorOption.StampOutlineFontthickness := 2;



      PDFCreatorOption.StampString := OrderType;


      PDFCreator.cOptions := PDFCreatorOption;
      PDFCreator.cClearCache();
      DefaultPrinter := PDFCreator.cDefaultPrinter;
      PDFCreator.cDefaultPrinter := 'PDFCreator';
      PDFCreator.cPrinterStop := FALSE;

      //REPORT.RUNMODAL(ReportID,FALSE,TRUE);
      REPORT.RUNMODAL(50020,FALSE,TRUE,SalesHeader);
    END;

    PROCEDURE printpdf@1000000001();
    BEGIN
      IF ISCLEAR(adobe) THEN
        CREATE(adobe);

      adobe.Open(FileDirectory + FileName,'');
      adobe.PrintPages(0,999,3,0,0);
      adobe.Close(0);
      CLEAR(adobe);

      CLEAR(PDFCreator);
      CLEAR(PDFCreatorError);
    END;

    PROCEDURE findsalesheader@1000000002(rSpecifier@1000000000 : Code[10]);
    BEGIN
      SalesHeader.RESET;
      SalesHeader.SETRANGE(SalesHeader."Document Type",SalesHeader."Document Type"::Order);
      SalesHeader.SETRANGE(SalesHeader."No.",rSpecifier);
    END;

    EVENT PDFCreator@1000000008::eReady@1();
    BEGIN
      PDFCreator.cPrinterStop := TRUE;
      PDFCreator.cDefaultPrinter := DefaultPrinter;
      PDFCreator.cClose();
      IF WindowisOpen THEN
        Window.CLOSE;
      WindowisOpen := FALSE;

      printpdf;
    END;

    EVENT PDFCreator@1000000008::eError@2();
    BEGIN
      ERROR('Status: Error[' + FORMAT(PDFCreatorError.Number) + ']: ' + PDFCreatorError.Description);
    END;

    BEGIN
    END.
  }
}

4. call this function from PRINT button on say.. sales order.
makepdf(rSpecifier : Code[10];OrderType : Code[10]).
pass these variables.
rspecifier is number of sales order
ordertype is field 50000 on salesheader
ordertype can be anything you want used as a watermark.
5. Make appropriate changes in the code to print your report number and make sure you have write permissions to FileDirectory and FileName

Just a note: portion of this code was taken from a previous post, talking about printing PDF files from Navision. I just extended it to Auto-Insert the watermark, and to Auto-Print it on your default printer.
Microsoft Certified Technology Specialist
Microsoft Certified Business Management Solutions Professional
Microsoft Certified Business Management Solutions Specialist

http://www.navisiontech.com

Comments

  • krikikriki Member, Moderator Posts: 9,094
    [Topic moved from Navision forum to Navision Tips & Tricks forum]
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • aliennavaliennav Member Posts: 449
    I have copied this code in a text format.
    changed it's extension to .fob
    imported it in navision
    now when i am trying to compile the code,it is giving an error--"could not load the selected type library".I have installed the PDF creator(Also verified its usage by converting reports in pdf ).

    i have also activated the parameters on the functions in this code unit.

    And the cursor points to the position where it is written
    ("ERROR('Status: Error: ' + PDFCreatorError.Description); ").
    And it is at the end of the codeunit.


    what to do now.
  • aliennavaliennav Member Posts: 449
    Probably step#4 is not clear to me.
    Explain it in detail.
    Thanx in advance
  • aliennavaliennav Member Posts: 449
    aliennav wrote:
    Probably step#4 is not clear to me.
    Explain it in detail.
    Thanx in advance


    No replies???
  • gvolkovgvolkov Member Posts: 196
    Please make sure you are installing the right PDFCreator from http://sourceforge.net/projects/pdfcreator/

    I have seen a couple different ones. This one is free, open source. Let me know if thats what you have.
    Microsoft Certified Technology Specialist
    Microsoft Certified Business Management Solutions Professional
    Microsoft Certified Business Management Solutions Specialist

    http://www.navisiontech.com
  • aliennavaliennav Member Posts: 449
    Please make sure you are installing the right PDFCreator from http://sourceforge.net/projects/pdfcreator/

    I have seen a couple different ones. This one is free, open source. Let me know if thats what you have.

    i have downloaded the PDF Creator from the same link but even then it's not working---the watermark thing.
  • gvolkovgvolkov Member Posts: 196
    I have tried to replicate your issue, but it compiled just fine. Only thing i can think of is version 0.9.3
    Make sure you have right Automation libraries setup.


    PDFCreator Automation 'PDFCreator'.clsPDFCreator
    PDFCreatorOption Automation 'PDFCreator'.clsPDFCreatorOptions
    PDFCreatorError Automation 'PDFCreator'.clsPDFCreatorError
    adobe Automation 'Adobe Acrobat 8.0 Type Library'.AcroAVDoc
    Microsoft Certified Technology Specialist
    Microsoft Certified Business Management Solutions Professional
    Microsoft Certified Business Management Solutions Specialist

    http://www.navisiontech.com
  • kirankumarkirankumar Member Posts: 29
    Hi gvolkov

    Pls Provide the Report 50020 to work Watermark

    Kiran
    Hi
  • ara3nara3n Member Posts: 9,255
    Somehow the code looks familier. 8)
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • yekedeyekede Member Posts: 96
    Hi all, i'm trying to implement watermark on my report using this method.
    I followed all the steps and called makepdf function from a print button with all the parameters but it just show me a processing window and nothing else. So i added the printpdf function after the makepdf and i get an error 'cannot create an instance of automation server. Check the automation server is correctly installed and registered'. The debugger stops on create(adobe) in printpdf function. But i have adobe reader 9.0 installed.
    Thanks for any tip in the right direction
  • ara3nara3n Member Posts: 9,255
    You need to install PDFCreator not Adobe.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • yekedeyekede Member Posts: 96
    Thanks for your reply.
    gvolkov wrote:
    1. install PdfCreator
    2. install adobe reader 8.0
    3. Create this codeunit:
    I have installed pdfcreator, adobe and i have created the codeunit but i get the scenario i described. Has anyone succefully tried this how to?
  • ara3nara3n Member Posts: 9,255
    you will need to select the automation subtype which is newer in your version. Design the object and change the global variable.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
Sign In or Register to comment.