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.

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

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);

}

 

}

Advertisements

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