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
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.