Thursday, October 26, 2023

Merge multiple pdf’s into a single pdf using x++ in D365FO

 Got the requirement to Merge multiple pdf’s into a single pdf in D365FO.



To achieve this need to download the (pdfsharp)3rd party DLL from the below link 

https://www.dllme.com/dll/files/pdfsharp#dl.

once DLL is downloaded you have to add at the project reference node.


Write the below code.

using PdfSharp;
internal final class PFDRun
{
    public static void main(Args _args)
    { 
	PdfSharp.Pdf.PdfDocument 	outPutPDFDocument = new PdfSharp.Pdf.PdfDocument();
	PdfSharp.Pdf.PdfDocument 	inputPDFDocument  = new PdfSharp.Pdf.PdfDocument(); 
        PdfSharp.Pdf.PdfPages 		pdfPages;

        container con = ['D:\\Test1.pdf','D:\\Test2.pdf', 'D:\\Test3.pdf'];

        int i, j, pageCount;

	FileName 		pdfFile;
        InteropPermission 	permission;
        str 			errorMessage;

        try
        {
            permission = new InteropPermission(InteropKind::ClrInterop);

            permission.assert();

            for (i = 1; i <= 2; i++)
            {
		// I have written custom class to get the file stream from blob.
                System.IO.MemoryStream pdfStream = MyClass::GetStream(i);

                // pdfStream.Seek(0); // Ensure the stream is at the beginning

		// intialize With stream
                inputPDFDocument = PdfSharp.Pdf.IO.PdfReader::Open(pdfStream, PdfSharp.Pdf.IO.PdfDocumentOpenMode::Import);
				
		// intialize With file name(File to be stored in local folder)
		// 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)
                {
                    if (pageCount == 1)
                    {
                        outputPDFDocument.AddPage(pdfPages.get_Item(0));
                    }
                    else
                    {
                        for (j = 1 ; j <= pageCount; j++)
                        {
                            outputPDFDocument.AddPage(pdfPages.get_Item(j-1));
                        }
                    }
                }
            }

            System.IO.MemoryStream outPutStream= new System.IO.MemoryStream();

            outputPDFDocument.Close();
            outputPDFDocument.Save('D:\\Output\\mergedFile.pdf'); // To save in local folder.
            outputPDFDocument.Save(outPutStream, false);

            file::SendFileToUser(outPutStream, 'mergedFile.pdf'); // To download

            CodeAccessPermission::revertAssert();
        }
        catch(Exception::CLRError)
        {
            errorMessage = AifUtil::getClrErrorMessage();

            CodeAccessPermission::revertAssert();

            throw error(errorMessage);
        }
    
    }

}

No comments:

Post a Comment