c# - 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 的可能解决方案:进程无法访问文件 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, 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, 0, 0);
document.SetPageSize(pageSize);
document.NewPage();
WriteLine(writer, pageSize, File0, _lines0, PreviewLineIndex);
WriteLine(writer, pageSize, File1, _lines1, PreviewLineIndex);
WriteLine(writer, pageSize, File2, _lines2, PreviewLineIndex);
WriteLine(writer, pageSize, File3, _lines3, PreviewLineIndex);
WriteLine(writer, pageSize, File4, _lines4, PreviewLineIndex);
stream.Flush(true);
document.Close();
writer.Dispose();
stream.Dispose();
document.Dispose();
}
catch (Exception Er)
{
}
return generatedFilePath;
}
解决方案
推荐阅读
- python - 如何从 Appium 中的外部 excel 或 YAML 文件向文本框输入值?
- python - PyUSB Linux USB 设备被内核回收
- php - php中的stream_socket_*和socket有什么区别?
- ios - 如何在 iPhone X 中通过 *ngIf 显示隐藏按钮
- php - 在现有 Drupal 7 中找不到管理 URL
- pandas - 如何从示例数据帧的 created_time 列中获取小时数并将其计数为另一个数据帧
- javascript - 将嵌套数组覆盖到同一个对象中
- python - 如何将 pandas.core.series.Series 和 numpy.int32 合并到 pandas.core.frame.DataFrame?
- react-native - 可以这样堆叠搜索结果吗?
- python - 如何在 python 中正确填充 4d 数组/矩阵?