Monday, August 8, 2016

Merge PDF Files using Dynamics NAV C/AL Code

Hello Friends,

I want to share the code to merge 2 or more PDF files using the Dynamics NAV C/AL Code using External DLL files.

Some time we have got the requirement form client to Merge General Terms & Condition files to Document Print and create one combine PDF File.

DLL file is downloaded from mibuso: http://mibuso.com/downloads/navpdfmerge

C/AL Code to merge PDF Files:

1) Following code will first create the PDF file for Purchase Order Report (405) & after it will merge with terms and condition pdf file located in shared directory.




















------- Merge PDF Document -------------()

POandTermsPrint_gFnc(PurchHdr_iRec : Record "Purchase Header")
IF PurchHdr_iRec.Status <> PurchHdr_iRec.Status :: Released THEN
  ERROR(Text50000_gCtx,PurchHdr_iRec."No.");

PH_lRec.RESET;
PH_lRec.SETRANGE("Document Type",PurchHdr_iRec."Document Type");
PH_lRec.SETRANGE("No.",PurchHdr_iRec."No.");
CLEAR(R50183);
R50183.SETTABLEVIEW(PH_lRec);
R50183.RUNMODAL;
R50183.InitializeRequestPDF_gFnc(NewNoOfCopies,NewShowInternalInfo,NewArchiveDocument,NewLogInteraction,ExciseInclusive_iBln,ExciseAtActual_iBln,VATInclusive_iBln,VATAtActual_iBln,BOMprint_iBln);

CLEAR(R405);
R405.InitializeRequestPDF_gFnc(NewNoOfCopies,NewShowInternalInfo,NewArchiveDocument,NewLogInteraction,ExciseInclusive_iBln,ExciseAtActual_iBln,VATInclusive_iBln,VATAtActual_iBln,BOMprint_iBln);
R405.USEREQUESTPAGE(FALSE);
R405.SETTABLEVIEW(PH_lRec);


PurchSetup.GET;
PurchSetup.TESTFIELD("PDF Merge Shared Folder");

POCode_lCod := DELCHR(PurchHdr_iRec."No.",'=','#%&*:<>?\/{|}~');

ServerSharedPath_lTxt := PurchSetup."PDF Merge Shared Folder";
UpdatePath_gFnc(ServerSharedPath_lTxt);
UniqueString_lTxt := POCode_lCod + FORMAT(TODAY,0,'<Day,2><Month,2><Year4>') + '_' + FORMAT(TIME,0,'<Hours24,2><Minutes,2><Seconds,2>');

ServerFileName_lTxt := ServerSharedPath_lTxt + UniqueString_lTxt + '.pdf';  //Need to erase
R405.SAVEASPDF(ServerFileName_lTxt);

POTermFile_lTxt := ServerSharedPath_lTxt + 'GTC.pdf';
IF NOT EXISTS(POTermFile_lTxt) THEN
  ERROR('File does not exists - %1',POTermFile_lTxt);

OutputFilePath_lTxt := ServerSharedPath_lTxt + UniqueString_lTxt + '_' + '.pdf';  //Need to erase
CMDCommand_lTxt := STRSUBSTNO(Text000,ServerSharedPath_lTxt,OutputFilePath_lTxt,ServerFileName_lTxt,POTermFile_lTxt);

MergePDFFile_gFnc(CMDCommand_lTxt);
SLEEP(3000);

IF EXISTS(OutputFilePath_lTxt) THEN
  ExportAttToClientFile_gFnc(OutputFilePath_lTxt,POCode_lCod + '.pdf')
ELSE BEGIN
  SLEEP(6000);
  IF EXISTS(OutputFilePath_lTxt) THEN
    ExportAttToClientFile_gFnc(OutputFilePath_lTxt,POCode_lCod + '.pdf');
END;

IF EXISTS(ServerFileName_lTxt) THEN
  ERASE(ServerFileName_lTxt);

IF EXISTS(OutputFilePath_lTxt) THEN
  ERASE(OutputFilePath_lTxt);

UpdatePath_gFnc(VAR Path_vTxt : Text[250])
IF COPYSTR(Path_vTxt,STRLEN(Path_vTxt),1) <> '\' THEN
  Path_vTxt := Path_vTxt + '\';

ExportAttToClientFile_gFnc(VAR ExportToFile : Text;ImportFileName_iTxt : Text[250]) : Boolean
FileExtension_lTxt := FileMgmt_lCdu.GetExtension(ExportToFile);
FileFilter_lTxt := UPPERCASE(FileExtension_lTxt) + ' (*.' + FileExtension_lTxt + ')|*.' + FileExtension_lTxt;

DOWNLOAD(ExportToFile,Text024_gTxt,'',FileFilter_lTxt,ImportFileName_iTxt);

2) Following routine will execute code in command prompt and create merge PDF files











MergePDFFile_gFnc(CMDCmd_iTxt : Text)
CLEAR(WShell_gAut);
DummyInt_gInt := 0;  //0 = to Hide CMD Screen and 1 to display that
WaitOnReturn_gBln := FALSE;
CREATE(WShell_gAut,FALSE,TRUE);
WShell_gAut.Run(CMDCmd_iTxt,DummyInt_gInt,WaitOnReturn_gBln);

CLEAR(WShell_gAut);

3) New Action added on Purchase Order page to generate Merge PDF files


























Download the sample objects & DLL files form here:
https://1drv.ms/u/s!An8nhANnLJmtiSun2bUJGD6i_-UA

Thank you for reading.
I wish it will help you in future.

Please write a comment here if you need any help.

Keep Sharing...Keep Growing....

No comments:

Post a Comment