Dynamics AX : How to Merge Multiple PDF files into a single PDF file
This Bolg will help you to merge Multiple PDF into a single PDF file. Thanks to open source dll “pdfSharp”. It made my life easier to convert the C# code to X++ by using the dll.
static void MergePDFFiles(Args _args)
{
PdfSharp.Pdf.PdfDocument outPutPDFDocument = new PdfSharp.Pdf.PdfDocument();
PdfSharp.Pdf.PdfDocument inputPDFDocument = new PdfSharp.Pdf.PdfDocument();
PdfSharp.Pdf.PdfPages pdfPages;
container con = MergePdf::findMatchingFiles(@'C:\TEMP', '*.pdf');
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 = 0 ; j < pageCount; j++)
{
outputPDFDocument.AddPage(pdfPages.get_Item(j));
}
}
}
outputPDFDocument.Save("c:\\TEMP\\mergedFile.pdf");
CodeAccessPermission::revertAssert();
}
catch(Exception::CLRError)
{
// Get the CLR error before any other CLR operation
errorMessage = AifUtil::getClrErrorMessage();
CodeAccessPermission::revertAssert();
throw error(errorMessage);
}
}
The dll can be downloaded from the below link or you can google and get the dll from other sources
The pdfsharp.dll file can also be downloaded from the link below:
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.
Now for this purpose I created a class as below:
class MergePdf
{
}
Added a static method in this class as below:
static container findMatchingFiles(
str _folderPath
, str _filePattern = '*.*')
{
System.IO.DirectoryInfo directory;
System.IO.FileInfo[] files;
System.IO.FileInfo file;
InteropPermission permission;
str fileName;
counter filesCount;
counter loop;
container mathchingFiles;
;
permission = new InteropPermission(InteropKind::ClrInterop);
permission.assert();
directory = new System.IO.DirectoryInfo(_folderPath);
files = directory.GetFiles(_filePattern);
filesCount = files.get_Length();
for (loop = 0; loop < filesCount; loop++)
{
file = files.GetValue(loop);
fileName = file.get_FullName();
mathchingFiles = conins(mathchingFiles, conlen(mathchingFiles) + 1, fileName);
}
CodeAccessPermission::revertAssert();
return mathchingFiles;
}
Then i created a X++ Job to achieve the desired target to merge multiple PDF files into a single file:
{
PdfSharp.Pdf.PdfDocument outPutPDFDocument = new PdfSharp.Pdf.PdfDocument();
PdfSharp.Pdf.PdfDocument inputPDFDocument = new PdfSharp.Pdf.PdfDocument();
PdfSharp.Pdf.PdfPages pdfPages;
container con = MergePdf::findMatchingFiles(@'C:\TEMP', '*.pdf');
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 = 0 ; j < pageCount; j++)
{
outputPDFDocument.AddPage(pdfPages.get_Item(j));
}
}
}
outputPDFDocument.Save("c:\\TEMP\\mergedFile.pdf");
CodeAccessPermission::revertAssert();
}
catch(Exception::CLRError)
{
// Get the CLR error before any other CLR operation
errorMessage = AifUtil::getClrErrorMessage();
CodeAccessPermission::revertAssert();
throw error(errorMessage);
}
}
Comments
Post a Comment