Search code examples
kofax

Rename PDF file


I would like to create a custom module that renames the generated PDF files by taking a generated filename string containg some field values from index fields, batch fields etc.

So when it comes to batch processing i could go for this (setupTransformator contains the parsed values from the custom storage strings)

    public void ProcessBatch(IBatch batch)
    {
        IACDataElement batchElement = GetBatchElementFromBatch(batch);
        IACDataElementCollection currentDocuments = GetDocumentsFromBatchElement(batchElement);
        IACDataElement customStorageStrings = GetCustomStorageStringsFromBatch(batch);
        IACDataElementCollection batchFields = GetElementsByName(batchElement, ResourcesKofax.BATCH_FIELDS, ResourcesKofax.BATCH_FIELD);

        setupTransformator = new SetupTransformator(customStorageStrings);

        for (int i = 0; i < currentDocuments.Count; i++)
        {
            int currentDocumentIndex = i + 1;
            IACDataElement currentDocument = currentDocuments[currentDocumentIndex];
            IACDataElementCollection indexFields = GetElementsByName(currentDocument, ResourcesKofax.INDEX_FIELDS, ResourcesKofax.INDEX_FIELD);

            string targetFilename = setupTransformator.GetFilename(batchElement, currentDocument, batchFields, indexFields);

            string documentFilePath = currentDocument[ResourcesKofax.PDF_GENERATION_FILE_NAME];

            // rename the PDF file
        }

        batch.BatchClose(KfxDbState.KfxDbBatchReady, KfxDbQueue.KfxDbQueueNext, 0, string.Empty);
    }

    private IACDataElement GetBatchElementFromBatch(IBatch batch)
    {
        IACDataElement rootElement = batch.ExtractRuntimeACDataElement(0);
        return rootElement.FindChildElementByName(ResourcesKofax.BATCH);
    }

    private IACDataElementCollection GetDocumentsFromBatchElement(IACDataElement batchElement)
    {
        return GetElementsByName(batchElement, ResourcesKofax.DOCUMENTS, ResourcesKofax.DOCUMENT);
    }

    private IACDataElement GetCustomStorageStringsFromBatch(IBatch batch)
    {
        IACDataElement setupElement = batch.ExtractSetupACDataElement(0);
        IACDataElementCollection batchClasses = GetElementsByName(setupElement, ResourcesKofax.BATCH_CLASSES, ResourcesKofax.BATCH_CLASS);
        IACDataElement batchClass = batchClasses[1];
        return batchClass.FindChildElementByName(ResourcesKofax.BATCH_CLASS_CUSTOM_STORAGE_STRINGS);
    }

    private IACDataElementCollection GetElementsByName(IACDataElement dataElement, string rootName, string targetName)
    {
        return dataElement.FindChildElementByName(rootName).FindChildElementsByName(targetName);
    }

Do I have to use the File.Move method or is there a method from the Kofax library I can use?


Solution

  • File names should be handled by Export Connectors only. As long as the batch is in the system you shouldn't have their names changed as this could lead to data loss and corruption.

    This especially applies when using field values for a PDF's name - since values are subject to change as long as the batch is in the system, how would you accommodate this? Nothing is stopping your users from processing a batch in your custom module and set the batch back to validation and change one or more fields.

    Speaking of the Export Connector and its API:

    By default, Kofax offers two methods to export a PDF - both on the ReleaseData object (this is taken from the API docs):

    • CopyKofaxPDFFile: Copy the PDF file that belongs to a document into the export PDF path that is defined during export setup.
    • CopyKofaxPDFFileToPath: Copy the PDF file that belongs to a document into a specified path (the path is a string input argument for this method).

    Both methods make use of something that you could define during Setup - for example, CopyKofaxPDFFile makes use of the KofaxPDFPath property. I am not sure if there's a property reserved for the file name.

    I usually stick with the KofaxPDFProperty exposed during runtime and perform a File.Copy operation. I wouldn't recommend moving the file or deleting it as this is something that KC automatically handles once the batch was exported successfully (in theory, there could be another export, or the export might just fail).

    Use the ReleaseData object to access field values, and string interpolation to define the PDF's final name.