Monday, November 17, 2025

Number of Pdf files merging into single file

Number of Pdf files merging into single file 

 using PdfSharp; 

using System.IO; 

internal final class PFDRun 

{ 

    /// <summary> 

    /// Class entry point. The system will call this method when a designated menu  

    /// is selected or when execution starts and this class is set as the startup class. 

    /// </summary> 

    /// <param name = "_args">The specified arguments.</param> 

    public static void main(Args _args) 

    { 

        System.IO.MemoryStream rundialog() 

        { 

            System.IO.Stream                            stream; 

            DialogGroup                                 dlgUploadGroup; 

            FileUploadBuild                             fileUploadBuild; 

            FormBuildControl                            formBuildControl; 

            Dialog                                      dialog = new Dialog("ImportDataFromPDF"); 

  

  

  

            dlgUploadGroup          = dialog.addGroup("@SYS54759"); 

            formBuildControl        = dialog.formBuildDesign().control(dlgUploadGroup.name()); 

            fileUploadBuild         = formBuildControl.addControlEx(classstr(FileUpload), "Upload"); 

  

  

  

            fileUploadBuild.style(FileUploadStyle::MinimalWithFilename); 

            fileUploadBuild.fileTypesAccepted('.pdf'); 

  

  

  

            if (dialog.run() && dialog.closedOk()) 

            { 

                FileUpload                          fileUploadControl   = dialog.formRun().control(dialog.formRun().controlId("Upload")); 

                FileUploadTemporaryStorageResult    fileUploadResult    = fileUploadControl.getFileUploadResult(); 

  

  

  

                if (fileUploadResult != null && fileUploadResult.getUploadStatus()) 

                { 

                    stream = fileUploadResult.openResult(); 

                } 

            } 

  

  

  

            return stream; 

        } 

  

  

  

        //VendTable       vendTable; 

        //ContactPerson   contactPerson; 

        //DirPartyTable   dirPartyTable; 

        //LogisticsElectronicAddress  logisticsElectronicAddress; 

        //vendTable = VendTable::find('100002');//Vendor Account Number 

        //contactPerson = ContactPerson::find(vendTable.ContactPersonId); 

        //dirPartyTable =DirPartyTable::findRec(contactPerson.Party,false,DirPartyType::Person); 

        //logisticsElectronicAddress = LogisticsElectronicAddress::findRecId(dirPartyTable.PrimaryContactEmail); 

        //info(strFmt("Name: %1,EmailAddress: %2 ",dirPartyTable.Name,logisticsElectronicAddress.Locator)); 

  

  

  

        PdfSharp.Pdf.PdfDocument outPutPDFDocument = new PdfSharp.Pdf.PdfDocument(); 

  

        PdfSharp.Pdf.PdfDocument inputPDFDocument = new PdfSharp.Pdf.PdfDocument();  

  

        PdfSharp.Pdf.PdfPages pdfPagesoutPdfPages; 

  

        container con = ['D:\\Test1.pdf','D:\\Test2.pdf', 'D:\\Test3.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 <= 2; i++) 

            { 

                System.IO.MemoryStream pdfStream = rundialog(); 

  

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

  

                inputPDFDocument = PdfSharp.Pdf.IO.PdfReader::Open(pdfStreamPdfSharp.Pdf.IO.PdfDocumentOpenMode::Import); 

  

  

                //inputPDFDocument = PdfSharp.Pdf.IO.PdfReader::Open(pdfFilePdfSharp.Pdf.IO.PdfDocumentOpenMode::Import); 

                //PdfSharp.Pdf.IO.PdfReader::TestPdfFile(pdfFile); 

  

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

                        } 

                    } 

                } 

            } 

  

            //  outPdfPages = outputPDFDocument.get_Pages(); 

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

            // System.IO.Stream                            stream1 =  new System.IO.Stream(); 

  

            outputPDFDocument.Close(); 

            outputPDFDocument.Save('D:\\Output\\mergedFile.pdf'); 

            outputPDFDocument.Save(stream1, false); 

  

            file::SendFileToUser(stream1, 'mergedFile.pdf'); 

  

  

            if (SysEmailParameters::find().SMTPUserName) 

            { 

                var builder = new SysMailerMessageBuilder(); 

  

                builder.setFrom(SysEmailParameters::find().SMTPUserName); 

                builder.addTo('ratna.p@daxarch.com'); 

  

                builder.setSubject('subject'); 

                builder.setBody('messageBody'); 

                if(stream1 != null) 

                { 

                    builder.addAttachment(stream1, 'mergedFile.pdf'); 

                } 

                boolean ret; 

                 ret = SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(builder.getMessage()); 

            } 

            //var builder = new SysMailerMessageBuilder(); 

  

  

  

            //builder.addTo("kishore.m@daxarch.com"); 

            //builder.setFrom("kishore.m@daxarch.com"); 

  

            //builder.setSubject("Subject"); 

            //builder.setBody("Body"); 

            //builder.addAttachment(stream1, 'mergedFile.pdf'); 

  

  

  

            //var message = builder.getMessage(); 

  

  

  

            //SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(message); 

  

  

  

            //Loads the byte array in PdfLoadedDocument 

            //PdfSharp.Pdf.IO.PdfLoadedDocument ldoc = new PdfSharp.Pdf.IO.PdfLoadedDocument(stream1.ToArray()); 

            ////Saves the document and dispose it. 

            //ldoc.save("sample.pdf"); 

            //ldoc.Close(); 

  

  

  

            //System.IO.StreamReader sReader = new System.IO.StreamReader(stream1); 

            //str fileContent = sReader.ReadToEnd(); 

            //File::SendStringAsFileToUser(fileContent, 'mergeReport.pdf'); 

  

  

  

  

            //  System.IO.MemoryStream stream1;// = new System.IO.MemoryStream(); 

  

  

  

            //  outPdfPages.Contents.Save(stream1); 

  

  

  

            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