We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose.PDF.Document is not letting go of the file after it has finished

Hi,

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

<span style="color:green;">// Do the delete</span>
<span style="color:blue;">try</span>
{
	<span style="color:#2b91af;">File</span>.Delete(documentPath);
	_logger.DebugFormat(<span style="color:#a31515;">"Sucessfully deletes Pre Processing file, {0}"</span>, <span style="color:blue;">new</span> <span style="color:#2b91af;">FileInfo</span>(documentPath).Name.ToString());
}
<span style="color:blue;">catch</span> (<span style="color:#2b91af;">Exception</span> ex)
{
	_logger.ErrorFormat(<span style="color:#a31515;">"Failed to delete file in Pre Processing, {0}"</span>, 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)<o:p></o:p>

Hi There,


Thanks for contacting support.

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.

Facades.PdfFileEditor pdfEditor = new Facades.PdfFileEditor();<o:p></o:p>

pdfEditor.Extract(dataDir + @"sample.pdf", 2, 3, dataDir + @"sample_out.pdf");

using (Document doc = new Document(dataDir + @"sample_out.pdf"))

{

pdfEditor.CloseConcatenatedStreams = true;

pdfEditor.Delete(dataDir + @"sample_out.pdf", new int[] { 1 }, dataDir + @"sample_outnew.pdf");

pdfEditor = null;

}

File.Delete(dataDir + @"sample_out.pdf");


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.


Best Regards,

Hi,

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.

300 dpi 600 pages @ 160mb = Successful
400 dpi 350 pages @ 200mb = Successful
600 dpi 450 pages @ 375mg = fails consistently

Please retry with large documents at a high dpi setting.

Thanks
John

In addition I can post my entire routine if you like, or can upload the document if you like but it is 375mb. Let me know.

Thanks
John

Hi John,


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.

We are sorry for the inconvenience.


Best Regards,

Hi Asad,



OK here is the file that will fail. https://www.dropbox.com/s/b4owilyyqt2rg23/new%20enviro%20fail%20to%20process.pdf?dl=0



What I am trying to do is remove the first page. Here is the entire routine that is failing.

The routine is attached as a txt file

Just to be clear, I have multithreaded the above process.

Hi Richard,


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);<o:p></o:p>

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.


Best Regards,

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

Hi John,


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);<o:p></o:p>

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.


Best Regards,

Hi Asad,

Thank you. I will make the changes and have our QA team test the changes. I should have word from them next week.

Thanks again,
John

Hi John,


Thanks for your feedback. Please take your time to implement the changes in the code and in case of any issue, please let us know.


Best Regards,