从发布数据对象访问ImageFiles

时间:2019-01-17 11:08:34

标签: kofax

我想转换kofax版本中的图像文件。我以此为参考。

convert Kofax Release document to binary

并创建了此代码

    public KfxReturnValue ReleaseDoc()
    {
        try
        {
            documentData.ImageFiles.Copy(Path.GetTempPath(), -1);
            string tmpFile = Path.Combine(Path.GetTempPath(), documentData.UniqueDocumentID.ToString("X8")) + Path.GetExtension(documentData.ImageFiles[0].FileName);
            string fileName = Path.GetFileName(tmpFile);
            byte[] binaryFile = null;

            if (File.Exists(documentData.KofaxPDFFileName)) // PDF
            {
                binaryFile = File.ReadAllBytes(documentData.KofaxPDFFileName);
            }
            else if (File.Exists(tmpFile)) // TIFF
            {
                binaryFile = File.ReadAllBytes(tmpFile);
                File.Delete(tmpFile);
            }
            else
            {
                // throw an error
                return KfxReturnValue.KFX_REL_ERROR;
            }

            // use fileName and binaryFile

            return KfxReturnValue.KFX_REL_SUCCESS;
        }
        catch (Exception e)
        {
            // log the error
            return KfxReturnValue.KFX_REL_ERROR;
        }
    }

当我执行这一行

string tmpFile = Path.Combine(Path.GetTempPath(), documentData.UniqueDocumentID.ToString("X8")) + Path.GetExtension(documentData.ImageFiles[0].FileName);

它总是跳到catch语句中并抛出在集合异常中找不到的项目。

出什么问题了,我该如何解决?

1 个答案:

答案 0 :(得分:1)

我尝试通过使用

解决问题
string fileName;
foreach (ImageFile img in documentData.ImageFiles)
{
    fileName = img.FileName;
    break;
}

代替

documentData.ImageFiles[0]

我知道此解决方案确实很难看,我将尝试找到更好的解决方案。我刚刚发现

string tempImgFileName = documentData.ImageFiles.ReleasedDirectory;

也可以。最后我用这段代码

    public KfxReturnValue ReleaseDoc()
    {
        try
        {
            string tempPath = Path.GetTempPath();
            documentData.ImageFiles.Copy(tempPath, -1);

            string tempImgFileName = documentData.ImageFiles.ReleasedDirectory;
            string fileExtension = Path.GetExtension(tempImgFileName);
            string documentId = documentData.UniqueDocumentID.ToString("X8");
            string filePath = Path.Combine(tempPath, documentId);
            string tmpTargetFile = filePath + fileExtension;
            string fileToRead = string.Empty;

            if (File.Exists(documentData.KofaxPDFFileName)) // try to create a PDF binary
            {
                fileToRead = documentData.KofaxPDFFileName;
            }
            else if (File.Exists(tmpTargetFile)) // try to create a TIFF binary if PDF is not possible
            {
                fileToRead = tmpTargetFile;
            }
            else
            {
                // handle error
                return KfxReturnValue.KFX_REL_ERROR;
            }

            string fileName = Path.GetFileName(tmpTargetFile);
            byte[] binaryFile = File.ReadAllBytes(fileToRead);
            File.Delete(tmpTargetFile);

            // use fileName and binaryFile;

            return KfxReturnValue.KFX_REL_SUCCESS;
        }
        catch (Exception e)
        {
            // handle error
            return KfxReturnValue.KFX_REL_ERROR;
        }
    }