Wednesday, 19 July 2017

Combine PDF's

 List           pdfList      = new List(Types::String);
    CombinePDF     CombinePDF   = new CombinePDF();
pdfList = this.getFilesFromDirectory(OGOParameters::find().PackingSlipPDFFile, _contract.parmPrintPriority());

        if(pdfList.elements() > 0)
        {
            // merge all pdfs to a single pdf
            this.mergePDFs(pdfList,OGOParameters::find().PackingSlipPDFFile +'\\' + enum2str(_contract.parmPrintPriority()) + '-' + date2str(systemDateGet(), 321,DateDay::Digits2,
                            DateSeparator::Hyphen,
                            DateMonth::Digits2,
                            DateSeparator::Hyphen,
                            DateYear::Digits4,DateFlags::None
                            )+ time2str(timeNow(),TimeSeparator::Space, TimeFormat::Hour24)+'_Combine.pdf');

            //delete all files from folder
            this.deleteFiles(pdfList);

=====================================================================

void mergePDFs(List _pdfList, FileName _file)
{

    PdfSharp.Pdf.PdfDocument  outPutPDFDocument = new PdfSharp.Pdf.PdfDocument();
    PdfSharp.Pdf.PdfDocument  inputPDFDocument  = new PdfSharp.Pdf.PdfDocument();
    PdfSharp.Pdf.PdfPages     pdfPages;
    int                       i, j, pageCount;
    FileName                  pdfFile;
    InteropPermission         permission;
    str                       errorMessage;
    //ListIterator              literator;
    Enumerator                en;
    //List    mergeListPdf    = new List(Types::String);
    ;

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

        permission.assert();
        en = _pdfList.getEnumerator();
        //literator = new ListIterator(_pdfList);

        while(en.moveNext())
        {
            pdfFile = en.current();
            if (!pdfFile)
            {
                continue;
            }

            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(_file);
        CodeAccessPermission::revertAssert();
    }
    catch(Exception::CLRError)
    {
        // Get the CLR error before any other CLR operation
        errorMessage = AifUtil::getClrErrorMessage();

        CodeAccessPermission::revertAssert();

        throw error(errorMessage);
    }
}



1 comment:

  1. I was surfing net and fortunately came across this site and found very interesting stuff here. Its really fun to read. I enjoyed a lot. Thanks for sharing this wonderful information. view

    ReplyDelete