Invoke code throwing error - File in use

Hey guys,

Requirement/workflow as below:

  1. Using read pdf with OCR activity to read a pdf and extract its text.
  2. Using invoke code activity to update metadata of that pdf with text extracted in step1.

Project language in json file - Visual Basic
Invoke code language - C#

Whenever i try to execute this, I get below exception thrown for invoke code:
Exception thrown by target of an invocation → Cannot access the file as file is in use by another program.

What can be the issue? I even tried to add delay(60 seconds) between 2 activities thinking may be file was not yet released by read pdf activity when invoke activity tried to use it. But still dint work.

How to resolve this?
@ppr @Yoichi


Hey guys,

I figured out what an error is. It is because in my code pdfReader is reading my file and my requirement is to update the metadata in the same file itself and not create new file. And now because it exists already and is being used, error is thrown while creating file with same name file.

How can I modify this script to fulfill my requirement?

in pdf, update the keywords section of metadata with in_pdfText.


Hey @sonaliaggarwal47

Any reason you don’t want to create a new file? As function holds the pdf container until you release, it will show that error.

I was in a similar situation while working on a rotate PDF page code.
I achieve the solution by creating a temp backup of file to process.

You can create a copy, extract data from that copied one and add metadata to the original one?

Hi @rahulsharma,

Yes I dont want to create a new file, just replace/update the original file.

I also dont want to go ahead with creating backup files as this is gonna create alot of files.

Files are in thousands and are on lan drive, so creating their copies might lead to performance issue as well.

Hence, I am trying to explore any better way to do this.


you don’t need to store backup file. you can delete as soon as the work is done, as soon as the metadata is attached.


Okay I get it.

You mean to say , create in this piece of code itself and once metadata is update in another file, delete here itself.

Are you able to provide me the update code the include these steps as I am no expert in writing this code?


No worries!

if that’s invoked in a UiPath workflow in loop, you can create and delete the backup file in the UiPath flow too. just create the copy of file and pass that as an argument, use that file to extract data and the original file to just attach metadata. After this code is done, you can add delete file activity to get rid of that file

Regarding script, below is the code to be added, after the 5th line in your screenshot, and make sure you add a “Temp” between file path and file name in PdfStamper arguments

string sourceFile = in_filePath + inFilename;  
string destinationFile = in_filePath + "Temp" +inFilename;  

File.Copy(sourceFile, destinationFile, true);  
File.Delete(Path.Combine(in_filePath,"Temp" +inFilename)); 

Hi @rahulsharma,

Thanks for your inputs, it really did help.

Here is the whole working script:

string sourceFile = in_filePath + in_fileName;
string destinationFile = in_filePath + “Temp_” +in_fileName;
System.IO.File.Copy(sourceFile, destinationFile, true);
PdfReader reader = new PdfReader(destinationFile);
PdfStamper stamper = new PdfStamper(reader, new FileStream(sourceFile, FileMode.Create));
var info = reader.Info;
info[“Keywords”] =in_pdfText;
stamper.MoreInfo = info;
stamper.FormFlattening = true;
out_insertedWordCount = info[“Keywords”].Length;
System.IO.File.Delete(Path.Combine(in_filePath,“Temp_” +in_fileName));


1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.