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

c# & adobe acrobat SDK: file is still locked after SDK finished

New Here ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

I'm working with C# and adobe acrobat SDK. When the program throws an error due to the pdf already being compressed I want to move the pdf.

However, C# complains that the file is being used by another process and I know it has to do with the SDK and not another program. After some debugging I found out that compressPDFOperation.Execute is the culprit.

How can I close it so that I can move the file?

 

 try {

 // Initial setup, create credentials instance.
    Console.WriteLine(".json: " + Directory.GetCurrentDirectory() + "/pdftools-api-credentials.json");
  Credentials credentials = Credentials.ServiceAccountCredentialsBuilder()
                            .FromFile(Directory.GetCurrentDirectory() + "/pdftools-api- 
              credentials.json").Build();

            // Create an ExecutionContext using credentials and create a new operation instance.
            ExecutionContext executionContext = ExecutionContext.Create(credentials);
            CompressPDFOperation compressPDFOperation = 
            CompressPDFOperation.CreateNew();

            // Set operation input from a source file.
            FileRef sourceFileRef = FileRef.CreateFromLocalFile(directory + @"\" + 
            pdfname);
            compressPDFOperation.SetInput(sourceFileRef);

            // Execute the operation.
            FileRef result = compressPDFOperation.Execute(executionContext);

            // Save the result to the specified location.
            //if pdf is part of a group, the group directory name will be stored in 
            fileGroupDirectory
            string fileGroupDirectory = directory.Replace(sourceDir, "");
            
            result.SaveAs(finishedDir + fileGroupDirectory + pdfname);
} 
catch (ServiceApiException ex)
 {
            Console.WriteLine("Exception encountered while executing operation", 
             ex.Message);

            if (ex.Message.Contains("The input file is already compressed"))
            {
               File.Move(file, finishedDir + fileGroupDirectory + fileName);                   
            }
        }
TOPICS
Windows

Views

61

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
LEGEND ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

LATEST

This is nothing to do with the Acrobat SDK. Entirely different Adobe API not using Acrobat. 

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