Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.
Thu Sep 14, 2017 6:39 am
Hi Support,
I'm trying to create a function to change the password of pdf files but I get the error saying "The file is being used by another process". The file is not opened anywhere else so I suspect the spire.pdf itself that locks the file the first time when the password is added. Here is the code:
- Code: Select all
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(filePath, "Kanteen779");
doc.Security.OwnerPassword = "Kanteen779";
doc.Security.UserPassword = "Password1";
//doc.Security.Permissions = 0;
doc.SaveToFile(filePath);
doc.Close();
doc.Dispose();
Any idea how to fix this?
-
ran.li
-
- Posts: 2
- Joined: Thu Aug 31, 2017 8:34 am
Thu Sep 14, 2017 8:25 am
Dear ran.li,
Thanks for your inquiry.
Please check again if another process opens the file.
I created a sample file manually and then used the latest
Spire.PDF Pack(Hot Fix) Version:3.9.285 to have a test, everything worked fine. So I firstly suggest using this version. If the issue still happens, please provide the input file for further investigation.
Sincerely,
Betsy
E-iceblue support team
-
Betsy.jiang
-
- Posts: 3099
- Joined: Tue Sep 06, 2016 8:30 am
Thu Sep 14, 2017 8:53 am
Hi Betsy,
I have resolved this issue by using file stream instead. The code is here just in case anyone else comes across with this issue.
- Code: Select all
using (FileStream stream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
PdfDocument doc = new PdfDocument();
doc.LoadFromStream(stream, "Kanteen779");
doc.Security.OwnerPassword = "Kanteen779";
doc.Security.UserPassword = "Password1";
//doc.Security.Permissions = 0;
doc.SaveToStream(stream);
doc.Close();
doc.Dispose();
stream.Close();
stream.Dispose();
}
Thanks,
Ran
-
ran.li
-
- Posts: 2
- Joined: Thu Aug 31, 2017 8:34 am
Thu Sep 14, 2017 8:55 am
Dear Ran,
Thanks for sharing.
Please feel free to contact us if you have any question
Sincerely,
Betsy
E-iceblue support team
-
Betsy.jiang
-
- Posts: 3099
- Joined: Tue Sep 06, 2016 8:30 am