• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Error response received for the request: The given key was not present in the dictionary:

New Here ,
Jan 26, 2022 Jan 26, 2022

Copy link to clipboard

Copied

I'm using the C# Adobe.PdfServicesSDK Nuget package to extract Json contents out of some PDF files. However the PDF files are over 200 pages long hence. Due to the limitation on the size file for exraction, I'm splitting the files into 20 pages files and then extracting the Json contents out of thos split files. So the process is simply: 1. split each file. 2. extract Json out of each split file 3. split the next big file and so on ...

 

It works perfectly on the first big file. I can split and then extract. However, when I move onto the next big file it fails when trying to split. So the exact same code, that split the first file now throws an exception when trying to run the split operation on the next file. It throws a ServiceApiException with the message: "Error response received for the request: The given key was not present in the dictionary". See attached screenshot for stack trace. 

 

I have tried creating a new Execution context with each call to to the API and I've tried creaing a single ExecutionContext reusing it on every call. It doesn't make any difference.

 

If I run the app with only a single file, let the app close, and then run the app on the next file, it works perfectly. So it would seem that either the C# SDK is caching something that breaks on the next file, or Adobe Services breaks on your servers due to some session state or something like that.

 

Lastly, I've tested running the split on multiple files without extracting them and it works perfectly without errors. But if I split, then extract out of the split files, then try and split the next big file, it throws the above error.

 

Here's my code that performs the split:

 

        public string SplitPdfFile(int pageCountPerSplitFile, string parentOutputDirectory, bool skipIfOutputDirectoryExists)

        {

            ValidateIsValidPdfFile(_filePath);

            string outputDirectoryName = Path.GetFileNameWithoutExtension(_filePath);

            string outputDirectoryPath = Path.Combine(parentOutputDirectory, outputDirectoryName);

            if (Directory.Exists(outputDirectoryPath))

            {

                if (skipIfOutputDirectoryExists)

                {

                    LogWarning($"{outputDirectoryPath} already exists. Skipping PDF split operation on PDF file {_filePath}.", LoggingLevel.Normal);

                    return outputDirectoryPath;

                }

                DeleteArtifactDirectory(outputDirectoryPath, false);

            }

            LogProgressAudit($"Splitting PDF File into {pageCountPerSplitFile} page files: {_filePath} ...", LoggingLevel.Normal);

            ExecutionContext executionContext = GetExecutionContext(false);

            SplitPDFOperation splitPDFOperation = SplitPDFOperation.CreateNew();

            FileRef inputFileReference = FileRef.CreateFromLocalFile(_filePath);

            splitPDFOperation.SetInput(inputFileReference);

            splitPDFOperation.SetPageCount(pageCountPerSplitFile);

            List<FileRef> splitOutputFileReferences = null;

            splitOutputFileReferences = splitPDFOperation.Execute(executionContext);

            for (int i = 0; i < splitOutputFileReferences.Count; i++)

            {

                FileRef fileReference = splitOutputFileReferences[i];

                string outputFileName = $"{ outputDirectoryName }_{ i}.pdf";

                string outputFilePath = Path.Combine(outputDirectoryPath, outputFileName);

                string adobeOutputFilePath = parentOutputDirectory + $"/{outputDirectoryName}/{outputFileName}";

                fileReference.SaveAs(adobeOutputFilePath);

                FileSystemHelper.ValidateFileExists(outputFilePath);

                LogProgressAudit($"Completed saving split PDF File: {outputFilePath}.", LoggingLevel.Maximum);

            }

            LogSuccessAudit($"Completed splitting PDF file to: {outputDirectoryPath}.", LoggingLevel.Minimum);

            return outputDirectoryPath;

        }

 

Here's my code that performs that extracts the Json out of a single split PDF file:

 

        private string ExtractSplitPdfFile(

            string pdfSplitInputFilePath,

            string splitFilesDirectoryPath,

            string parentInputPdfFileNameWithoutExtension,

            string parentOutputDirectory)

        {

            FileSystemHelper.ValidateFileExists(pdfSplitInputFilePath);

            string pdfSplitInputFileName = Path.GetFileName(pdfSplitInputFilePath);

 

            string zipJsonFileName = Path.ChangeExtension(pdfSplitInputFileName, "zip"); //Zip file containing Json file with the extracted text from the pdfSplitInputFilePath.

            string zipJsonFilePath = Path.Combine(parentOutputDirectory, Path.Combine(parentInputPdfFileNameWithoutExtension, zipJsonFileName));

            string zipJsonFileExtractedDirectoryPath = Path.Combine(parentOutputDirectory, Path.Combine(parentInputPdfFileNameWithoutExtension, Path.GetFileNameWithoutExtension(pdfSplitInputFilePath)));

 

            string adobeZipRelativeFilePath = $"/{parentInputPdfFileNameWithoutExtension}/{zipJsonFileName}";

            string adobeZipFullFilePath = parentOutputDirectory + adobeZipRelativeFilePath;

 

            LogProgressAudit($"Extracting Json from PDF: {pdfSplitInputFilePath} ...", LoggingLevel.Normal);

 

            ExecutionContext executionContext = GetExecutionContext(false);

            ExtractPDFOperation extractPdfOperation = ExtractPDFOperation.CreateNew();

 

            FileRef sourceFileRef = FileRef.CreateFromLocalFile(pdfSplitInputFilePath);

            extractPdfOperation.SetInputFile(sourceFileRef);

 

            //ExtractPDFOptions extractPdfOptions = ExtractPDFOptions.ExtractPdfOptionsBuilder().AddElementsToExtract(new List<ExtractElementType>(new[] { ExtractElementType.TEXT })).build(); //Adobe 2.1

            ExtractPDFOptions extractPdfOptions = ExtractPDFOptions.ExtractPDFOptionsBuilder().AddElementsToExtract(new List<ExtractElementType>(new[] { ExtractElementType.TEXT })).Build(); //Adobe 2.2

            extractPdfOperation.SetOptions(extractPdfOptions);

 

            FileRef result = extractPdfOperation.Execute(executionContext);

            if (File.Exists(zipJsonFilePath))

            {

                DeleteArtifactFile(zipJsonFilePath, true);

            }

            result.SaveAs(adobeZipFullFilePath);

 

            string jsonTextFilePath = ExtractPdfJsonFileFromZipFile(pdfSplitInputFileName, zipJsonFilePath, zipJsonFileExtractedDirectoryPath, splitFilesDirectoryPath);

            LogSuccessAudit($"Completed extracting Json from PDF: {jsonTextFilePath}.", LoggingLevel.Normal);

            return jsonTextFilePath;

        }

Views

197

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Feb 06, 2022 Feb 06, 2022

Copy link to clipboard

Copied

Hi @Paul Kolozsvari

 

Thanks for reporting this issue. We have identified this issue as a bug, and our team is working on a fix. We will keep you posted as there are any new updates. Thank you!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 07, 2022 Feb 07, 2022

Copy link to clipboard

Copied

LATEST

Thanks for the reply. Looking forward to getting new updates on it.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources