I am having a problem with the Aspose.PDF.Document. As seen in my code snippet below I am trying to delete the old file after I have removed a page from it. Also I have included the errors that I have logged from when this happens.
Is there a preferred way to do what I am trying to do? Is there a way to do this delete with out getting the file lock error?
Thanks
John
if ((pageEnd == 0) || (pageEnd < pageStart))
{
using (Document pdfInfo = new Document(OriginalFile))
{
pageEnd = pdfInfo.Pages.Count;
}
}
PdfFileEditor pdfEditor = new PdfFileEditor();
pdfEditor.Extract(OriginalFile, pageStart, pageEnd, documentPath);
if (!IncludeBarcodePage)
{
using (Document pdfDocument = new Document(documentPath))
{
// we will be extracting out our document to then remove the cover page
pdfEditor.CloseConcatenatedStreams = true;
pdfEditor.Delete(documentPath, barcodePage, NewFilePath);
pdfEditor = null;
}
// Do the delete
try
{
File.Delete(documentPath);
_logger.DebugFormat("Sucessfully deletes Pre Processing file, {0}", new FileInfo(documentPath).Name.ToString());
}
catch (Exception ex)
{
_logger.ErrorFormat("Failed to delete file in Pre Processing, {0}", ex);
}
}
else
{
NewFilePath = documentPath;
}
2017-03-15 16:19:55,584 [3116] ERROR
iAScan.Plugins.Abbyy.AsposeProcessing [(null)] - Failed to delete file in Pre
Processing, System.IO.IOException: The process cannot access the file
‘D:\Data\Processing\fd82ab3d-4ecd-4609-9f62-f824c4b5e79c_2.pdf’ because it is
being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String
maybeFullPath)
at System.IO.File.InternalDelete(String path, Boolean checkHost)
at System.IO.File.Delete(String path)
at iAScan.Plugins.Abbyy.AsposeProcessing.<>c__DisplayClass21_0.b__0(Object
o)
2017-03-15 16:19:55,678 [2172] ERROR iAScan.Plugins.Abbyy.AsposeProcessing
[(null)] - Failed to delete file in Pre Processing, System.IO.IOException: The
process cannot access the file
‘D:\Data\Processing\fd82ab3d-4ecd-4609-9f62-f824c4b5e79c_1.pdf’ because it is
being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String
maybeFullPath)
at System.IO.File.InternalDelete(String path, Boolean checkHost)
at System.IO.File.Delete(String path)
at
iAScan.Plugins.Abbyy.AsposeProcessing.<>c__DisplayClass21_0.b__0(Object
o)
2017-03-15 16:19:55,678 [3116] ERROR iAScan.Plugins.Abbyy.AsposeProcessing
[(null)] - Failed to delete file in Pre Processing, System.IO.IOException: The
process cannot access the file
‘D:\Data\Processing\fd82ab3d-4ecd-4609-9f62-f824c4b5e79c_3.pdf’ because it is
being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String
maybeFullPath)
at System.IO.File.InternalDelete(String path, Boolean checkHost)
at System.IO.File.Delete(String path)
at
iAScan.Plugins.Abbyy.AsposeProcessing.<>c__DisplayClass21_0.b__0(Object
o)
I have tried to test the scenario using the code snippet which you have shared but it seems that it is incomplete. There are some undefined variables in the code snippet i.e pageEnd, pageStart, IncludeBarcodePage. However I have somehow managed to run a test scenario with one of my sample PDF’s by following code snippet and was unable to replicate the issue which you have mentioned.
Furthermore there is a chance that issue can be in the above code lines which you have not shared. We will really appreciate if you please share a working code snippet along with the sample input file(s) so that we can try to reproduce the issue at our end and respond you accordingly. We are sorry for the inconvenience.
I’m sorry I forgot to include some more information about the files I am using and my test results. Please excuse the fact I forgot them in the original post.
Thanks for sharing more details. Please note that our forum supports upload feature up-to 25MB. You can upload your file to some public file hosting service and share the link here. Please share your input file with which you are facing the problem so that we can test the scenario in our environment and update you accordingly.
Thanks for your patience and sharing more details with us.
I have tested the whole scenario in my environment and I am afraid that I was unable to replicate the issue which you have mentioned. Moreover the code snippet which you have shared is not working as there are some undefined objects in the code still. I have attached and screenshot of how code behaves when I write/paste it in Visual Studio. Though I have somehow managed to execute the similar routine.
nqueue:
Just to be clear, I have multithreaded the above process.
Please note that Aspose.Pdf for .NET is multi-threaded safe as long as single thread works on a single document at a time. Which means access to single document by multiple threads at the same time is not supported. Please check the following code snippet in which I have managed to execute entire routine in a single thread and code generated and deleted the file(s) without throwing any exception.
System.Threading.ManualResetEvent FirstTier = new System.Threading.ManualResetEvent(false);
var th = new System.Threading.Thread(() => {
Facades.PdfFileEditor pdfEditor = new Facades.PdfFileEditor();
pdfEditor.Extract(dataDir + @"new enviro fail to process.pdf", 2, 3, dataDir + @"new enviro fail to process_out.pdf");
using (Document doc = new Document(dataDir + @"new enviro fail to process_out.pdf"))
{
pdfEditor.CloseConcatenatedStreams = true;
pdfEditor.Delete(dataDir + @"new enviro fail to process_out.pdf", new int[] { 1 }, dataDir + @"new enviro fail to process_outnew.pdf");
pdfEditor = null;
}
File.Delete(dataDir + @"new enviro fail to process_out.pdf");
Console.WriteLine("Deleted");
FirstTier.Set();
});
th.SetApartmentState(System.Threading.ApartmentState.STA);
th.Start();
I have also attached an output file generated by the above code for your reference. By looking at the exception which you have shared it seems that you are trying to access a document with more than one thread at same time. Please try to synchronize the threads in the code so that a document is only accessed by single thread at a time and if you still face any problem please share a sample console application so that we can try to test it in our environment and address it accordingly. We are sorry for the inconvenience.
It seems that you are only outputting pages 2 and 3. try to process the document with your code and only remove page 1 and output the rest of the document. this issue only happens when we do this on LARGE documents Ie. 600dpi and many hundred pages. I only want to remove page 1 and output the rest of the document.
pdfEditor.Extract(dataDir + @“new enviro fail to process.pdf”, 2, 3, dataDir + @“new enviro fail to process_out.pdf”);
Try this one:
pdfEditor.Extract(dataDir + @“new enviro fail to process.pdf”, 2, doc.Pages.Count, dataDir + @“new enviro fail to process_out.pdf”);
Thanks for sharing more details. I have again tested the scenario as per your requirement and noticed the exception which you have mentioned earlier. However when I tried to use new DOM (Document Object Model) approach to delete the particular page(s) and save the rest of document, the code executed fine and file was deleted successfully within the thread. Please note that Aspose.Pdf.Facades is an old approach whereas it is highly recommended to use new DOM Aspose.Pdf approach. Please check the following code snippet which I have used to achieve the functionality.
System.Threading.ManualResetEvent FirstTier = new System.Threading.ManualResetEvent(false);
var th = new System.Threading.Thread(() => {
Facades.PdfFileEditor pdfEditor = new Facades.PdfFileEditor();
pdfEditor.Extract(dataDir + @"new enviro fail to process.pdf", 2, 450, dataDir + @"new enviro fail to process_out.pdf");
using (Document doc = new Document(dataDir + @"new enviro fail to process_out.pdf"))
{
doc.Pages.Delete(1);
doc.Save(dataDir + @"new enviro fail to process_out.pdf");
}
File.Delete(dataDir + @"new enviro fail to process_out.pdf");
Console.WriteLine("Deleted");
FirstTier.Set();
});
th.SetApartmentState(System.Threading.ApartmentState.STA);
th.Start();
I have also highlighted the part of the code which I have modified to use DOM approach. Please try using new approach to achieve the functionality and in a case you still face any problem please feel free to contact us.