Friday, March 21, 2014

Merging Two pdf file in AX 2012 through Code

// Merging Two pdf file in AX 2012 through Code

static void MergePDFs(Args _args)
{
    PdfSharp.Pdf.PdfDocument outPutPDFDocument = new PdfSharp.Pdf.PdfDocument(); // First u add pdfsharp.dll to AOT-->References , it can download from http://www.4shared.com/get/1LJNZy7i/pdfsharp.html?sd=true#
    PdfSharp.Pdf.PdfDocument inputPDFDocument = new PdfSharp.Pdf.PdfDocument();
    PdfSharp.Pdf.PdfPages pdfPages;
    container con = ["c:\\QUANTITATIVE APTITUDE.pdf","c:\\R S aggarwal verbal reasoning.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:\\mergedFile.pdf");
        CodeAccessPermission::revertAssert();
    }
    catch(Exception::CLRError)
    {
        // Get the CLR error before any other CLR operation
        errorMessage = AifUtil::getClrErrorMessage();
        CodeAccessPermission::revertAssert();
        throw error(errorMessage);
    }

}

No comments:

Post a Comment

Copy Markup charges while posting purchase invoice using X++

 Copy Markup charges while posting purchase invoice using X++ Class: Important: Code logic is just for Reference.  New class => Duplicate...