Merger Pdf file in Single Pdf in Dynamics Ax

 Description:-

in this article we will see how to merge multiple pdf in to single pdf in Dynamics AX 2012. 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.

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

Code:-

static void MergePDF(Args _args)
{
    PdfSharp.Pdf.PdfDocument outPutPDFDocument = newPdfSharp.Pdf.PdfDocument();
    PdfSharp.Pdf.PdfDocument inputPDFDocument = newPdfSharp.Pdf.PdfDocument();
    PdfSharp.Pdf.PdfPagespdfPages;
    container con = ["c:\\file1.pdf","c:\\file2.pdf"]; 
    //This has been hardcoded for now – use winapi::findfirstfile and next to get pdf’s -folder
    inti, j, pageCount;
    FileName pdfFile;
    InteropPermission permission;
    str errorMessage;
    ;

    try
    {
        permission = newInteropPermission(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 = 0 ; 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);
    }   
}

Related Posts

Previous
Next Post »

2 comments

comments
Anonymous
April 7, 2016 at 9:57:00 AM GMT+5:30 delete

It's really good Post to Mearge two PDF file in X++. i really want this one. thanks

Reply
avatar

Thanks for comments.....