如何解决System.IO.IOException 使用 itext 编写 PDF 文件
我使用的是 iText v5.5.5、Visual Studio 2013、.NET Framework 4.5、Windows 7 x64
我已经检查了这个问题 System.IO.IOException: 'The process cannot access the file '@.txt' because it is being used by another process.' 以及其他许多问题,试图找到解决持久性 System.IO.IOException: The process cannot access the file VDP.Temp.1.pdf强>
到目前为止,我尝试了这个,但仍然在以下位置收到 IO 错误:
"File.Delete(generatedFilePath)"
任何可能的解决方案?
private static string GetTemporaryFilePath()
{
var appDirectoryPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var generatedFilePath = Path.Combine(appDirectoryPath,"VDP.Temp.1.pdf");
if (File.Exists(generatedFilePath))
{
FileStream Stream3 = null;
try
{
Stream3 = File.Open(generatedFilePath,FileMode.Open,FileAccess.Read,FileShare.None);
}
catch (IOException Er)
{
string message = Er.Message;
string title = "";
MessageBoxButtons buttons = MessageBoxButtons.OK;
MessageBox.Show(message,title,buttons,MessageBoxIcon.Warning);
}
finally
{
Stream3.Close();
Stream3.Dispose();
File.Delete(generatedFilePath);
}
}
return generatedFilePath;
}
拼图的另一个部分,同样的错误也持续存在:
document.Close();
private string GeneratePreviewPdfFileFromBlank()
{
// compute temporary file location
var generatedFilePath = GetTemporaryFilePath();
try
{
// then generate the temporary file
var pageSize = new iTextSharp.text.Rectangle(0,SourcePdfPageSize.Width,SourcePdfPageSize.Height);
var document = new Document(pageSize);
var stream = File.OpenWrite(generatedFilePath);
var writer = PdfWriter.GetInstance(document,stream);
document.Open();
document.SetMargins(0,0);
document.SetPageSize(pageSize);
document.NewPage();
WriteLine(writer,pageSize,File0,_lines0,PreviewLineIndex);
WriteLine(writer,File1,_lines1,File2,_lines2,File3,_lines3,File4,_lines4,PreviewLineIndex);
stream.Flush(true);
document.Close();
writer.Dispose();
stream.Dispose();
document.Dispose();
}
catch (Exception Er)
{
}
return generatedFilePath;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。