c# adobe acrobat SDK:文件在 SDK 退出后仍然被锁定

c# adobe acrobat SDK: file is still locked after SDK quit

我正在使用 C# 和 adobe acrobat SDK。 当程序由于 pdf 已经被压缩而抛出错误时,我想移动 pdf。

但是,C# 抱怨该文件正在被另一个进程使用,我知道它与 SDK 有关,而不是另一个程序。 经过一些调试后,我发现 compressPDFOperation.Execute 是罪魁祸首。

如何关闭它以便移动文件?

 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);                   
            }
        }

我找到了一个解决方案,这不是最佳做法,但我不知道其他方法。 我已经在 try catch 语句之前和 result.SaveAs(...) 之后声明了用于执行压缩的所有变量(sourceFileRef、compressPdfOperation,...),我将这些变量设置为 null 并将 运行 垃圾收集。

        compressPDFOperation = null;
        result = null;
        sourceFileRef = null;
        executionContext = null;
        credentials = null;
        GC.Collect();