How to get the count of number of pages printed on to SSRS report [AX 2012 , X++]

Friends,

Recently, I came across an interesting requirement on the reports in AX 2012. If the total number of pages that gets rendered to the report is more than some X pages, the report should get saved to pdf, if not send it as an email.

Tricky part is how do I get the number of pages [page count] after the report got rendered. I tried to get the number of pages before rendering the report but was unsuccessful by using the adapter class and getNumberOfPages() method.

Finally, I found a solution. On Controller classes there is a method by name “reportViewerRefreshComplete” which can be overridden. This method gets triggered once the data has been rendered on to report viewer.

Override the “reportViewerRefreshComplete” method on any controller class and add the below code. This will help to get the count of printed physical pages on to report.

public void reportViewerRefreshComplete(SRSReportExecutionInfo _executionInfo)

{

    int page;

    super(_executionInfo);

 

    page = this.getReportContract().parmReportExecutionInfo().parmPrintedPhysicalPages();

    info("Total number of pages:" + int2str(page));

}

Below is the infolog with the counter of pages, after the report data got rendered on to report viewer.

image

Happy Dax6’ng.

Sreenath Reddy

sree

Advertisements

Merge multiple pdf’s in to single pdf report [using X++ in Dynamics AX 2012]

Friends,

It’s been long time since I have posted, as you all know work always keeps us busy.

Few days back, there was a requirement to merge multiple pdf’s in to single pdf in Dynamics AX 2012.[Still works for 5.0/2009]

This article will help you how to achieve this. Thanks to open source dll “pdfSharp”. It made my life easier to convert the C# code to X++ by using the dll.

The dll can be downloaded from the below link or you can google and get the dll from other sources

http://pdfsharp.codeplex.com/

Once the dll has been downloaded, you need to add the dll in Client\Bin folder as shown below.

image

Then go to AOT >> References >> Add the pdfSharp.dll by using Add reference option and browse till the pdfSharp.dll in the bin folder.

image

That’s it. we are good now to merge the pdf files. Below is the code snippet.

static void SR_MergePDFs(Args _args)

{

    PdfSharp.Pdf.PdfDocument outPutPDFDocument = new PdfSharp.Pdf.PdfDocument();

    PdfSharp.Pdf.PdfDocument inputPDFDocument = new PdfSharp.Pdf.PdfDocument();

    PdfSharp.Pdf.PdfPages pdfPages;

    container con = ["c:\\file1.pdf","c:\\file2.pdf"]; // This has been hardcoded for now – use winapi::findfirstfile and next to get pdf’s -folder

    int i, j, pageCount;

    FileName pdfFile;

    InteropPermission permission;

    str errorMessage;

    ;

 

    try

    {

        permission = new InteropPermission(InteropKind::ClrInterop);

        permission.assert();

        for (i = 1; i <= conLen(con); i++)

        {

            pdfFile = conPeek(con,i);

            inputPDFDocument = PdfSharp.Pdf.IO.PdfReader::Open(pdfFile, PdfSharp.Pdf.IO.PdfDocumentOpenMode::Import);

            outputPDFDocument.set_Version(inputPDFDocument.get_Version());

            pageCount = inputPDFDocument.get_PageCount();

            pdfPages = inputPDFDocument.get_Pages();

       

            if (pageCount > 0)

            {

                for (j = 1 ; j <= pageCount; j++)

                {

                    outputPDFDocument.AddPage(pdfPages.get_Item(j));

                }  

            }

        }

        outputPDFDocument.Save("c:\\sgx\\mergedFile.pdf");

        CodeAccessPermission::revertAssert();

    }

    catch(Exception::CLRError)

    {

        // Get the CLR error before any other CLR operation

        errorMessage = AifUtil::getClrErrorMessage();

        CodeAccessPermission::revertAssert();

        throw error(errorMessage);

    }

   

}

Happy Dax6’ng

Sreenath Reddy

sree