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

Advertisements

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

  1. Kjetil Says:

    Hi,
    Thanks for an excellent tips, just in time 🙂
    I had to change the inner for-loop to the following:
    if (pageCount > 0)
    {
    for (j = 0 ; j < pageCount; j++)
    {
    outputPDFDocument.AddPage(pdfPages.get_Item(j));
    }
    }

  2. gangadhar Says:

    Hi,

    I have an issue while printing reports for multiple sales orders.
    When we post two sales order packing slips together(i.e., select 2 open orders and open the packing slip for those orders..check print packing slip report button and print it). we can generate the reports simultaneously. Even I have to print my reports along with those reports. But in my case, one report is prinetd and then second is printed only if the first report is closed.(The same is happening with standard Shipping labels reports in Posting form). I have to print my multiple reports simultaneously.

    I have no clue how to print them like packing slips
    can anyone help me in this

    Thanking you

    • Sreenath Reddy Says:

      Gangadhar,

      This is standard feature. I believe you have to modify the settings the way you need it in the intermediate salesEditLines form,
      The Summary update for field determines how multiple orders can be summarised into one document.
      There are 4 options:

      i) None: No summary updates are processed. One order will be created for every SO order selected [Multiple orders]

      ii) Invoice account: Use this to summarize/consolidate multiple Sales order into one sales order update.

      iii) Order: A range of orders can be selected to consolidate into one Sales order.

      iv) Automatic summary: Select this option to summarize multiple orders according to the criteria that is specified in the Summary update parameters.

      Thanks
      Sreenath


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: