Aspose.Pdf open file handles in case of failure at save(zip.txt)

Hi,

I am trying to handle exceptions while converting files to pdf.

For example if I rename a zip-file "foo.zip" to "foo.txt" and try to convert "foo.txt" to "foo.pdf" with the code bellow, the save()-method will first create a zero-byte file "foo.pdf" and throw afterwords an exception. Now if I attempt to delete the created zero-byte file I become an exception that this file is in use and therefore can not be deleted. I would like to know if the file handles will be closed in case of failure?

Many thanks in advance

-----------------------Code-Begin-----------------------

tr = new System.IO.StreamReader("foo.txt");

pdf1 = new Aspose.Pdf.Pdf();

Aspose.Pdf.Section sec1 = pdf1.Sections.Add();

sec1.PageInfo.Margin.Left = 110;

sec1.PageInfo.Margin.Right = 120;

Aspose.Pdf.Text t2 = new Aspose.Pdf.Text(tr.ReadToEnd());

tr.Close();

sec1.Paragraphs.Add(t2);

pdf1.Save(pdfFilePath);

-----------------------Code-End-----------------------

Hello Vieux,

Thanks for considering Aspose.

I'm not sure why you are simply renaming the file from .ZIP to .TXT instead of extracting the contents of .ZIP file.

Please visit the following link for information on Converting text file to PDF

In case it does not resolve your problem or you have any further query, please feel free to contact.

Hi,

thanks for replying.

I am renaming the file to txt just because my application allows only the conversion of txt-files. In fact I am extracting the content of the file and trying to convert it to pdf, but zip-files can of course not be converted to pdf and this is the problem. Iam handling such exceptions since I can not ensure that the customers will only attempt to convert "Correct"-TXT-Files to pdf. As said before in this case the save()-Method throws an exception but after creating a zero-byte file, which I can not delete afterward without restarting the application. Now the question is why is it impossible to delete this file?

Thanks and Regards

Hello Vieux,

Please try using the following code snippet in which first all the .txt files in a particular folder are filtered out to be converted into PDF format and after the conversion the newly created PDF documents are deleted.

Please try using it and incase you face any problem, please share the resource documents that you are trying to convert.

[C#]

// Retrieve names of all the Text files in a particular Directory
string[] fileEntries = Directory.GetFiles(@"D:\pdftest\Text_Pdf\", "*.txt");

for (int counter = 0; counter < fileEntries.Length; counter++)
{
// read the contents of text file
System.IO.TextReader tr = new StreamReader(fileEntries[counter]);
//Instantiate Pdf pbject by calling its empty constructor
Aspose.Pdf.Pdf pdf1 = new Aspose.Pdf.Pdf();
//Create a new section in the Pdf object
Aspose.Pdf.Section sec1 = pdf1.Sections.Add();
//Create a new text paragraph and pass the text to its constructor as argument
Aspose.Pdf.Text t2 = new Aspose.Pdf.Text(tr.ReadToEnd());
// add the text to paragraphs collection of Section
sec1.Paragraphs.Add(t2);
// save the resultant PDF files with the name same as original input Text file
pdf1.Save(@""+fileEntries[counter].Substring(0,fileEntries[counter].Length-4)+ ".pdf");
}

MessageBox.Show("The converted PDF documents will be deleted when you press OK");
// This variable will hold the number count of PDF documents converted in previous step
int i=0;

// Start deleting all Pdf docuemnts
do{
try{
// Delete the PDF documents One by One
File.Delete(fileEntries[i].Substring(0, fileEntries[i].Length - 4)+".pdf");
}catch(Exception excep)
{
Console.WriteLine("{0} Exception caught.", excep);
}
i++;
// keep on iterating until the variable count is not equal to the lenght of String array
}while(i<=fileEntries.Length);

Hi,

thanks again for this example but my problem is not to handle the logic but to delete the zero-byte-file created bei the Save()-method in case of failure:

try {
[...]
Aspose.Pdf.Pdf.Save("foo.pdf"); // <--- Throws Exception because invalid text file content
} catch (Exception e) {
File.Delete("foo.pdf"); // <--- FAILS, because File is in use
}

"foo.pdf" remains undeletable until I recall the method with a valid txt-content i.e. save(ValidFile.pdf) or restart the application!?

I think that the save()-method opens a file-handle for foo.pdf at its begin and does not close this in case of failure, that is the file-handle is closed only at the end of the save()-method and not in the exception-blocks?

Hello Vieux,

Yes you are correct. Aspose.Pdf closes the file handler if everything is successful during the process when PDF is generated. In case if in the middle of the operation, an issue is occurred the normal execution is interrupted and Aspose.Pdf could not release the handler. So in order to delete the file, you have to stop the application.

Moreover, as you've noticed, if a correct file format is provided, Aspose.Pdf generates the PDF document, and you can delete that file without any problem, even the application is running.

If still it does not satisfy your query, please feel free to share.

PS, I'm still not sure, why don't you filter out the correct .txt files and use them for conversion instead of renaming the files.

HI Vieux,

Aspose.Pdf should release the file handler. But I can’t reproduce your error. Can you please share your complete project that can reproduce this error?

As a workaround, you can save the PDF into a memory stream and save the memory stream into file when the first step success.