【发布时间】:2019-11-01 04:33:00
【问题描述】:
如果我使用给定的源代码创建 pdf,我会遇到无法打开 pdf 文档的问题,例如在“adobe-acrobat”中,因为它说它仍在被另一个进程使用。我的代码中缺少什么来释放锁?
var a = Guid.NewGuid();
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter writer2 = PdfWriter.GetInstance(pdfDoc, new FileStream(a.ToString()+".pdf", FileMode.Create));
writer2.SetFullCompression();
writer2.CloseStream = true;
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(AppContext.BaseDirectory + "Ehrungsantrag.png");
img.SetAbsolutePosition(0, 0);
img.ScaleAbsoluteHeight(pdfDoc.PageSize.Height);
img.ScaleAbsoluteWidth(pdfDoc.PageSize.Width);
pdfDoc.Open();
pdfDoc.NewPage();
pdfDoc.Add(img);
pdfDoc.Close();
writer2.Close();
【问题讨论】:
-
你确定这段代码可以正常运行吗?如果您收到错误消息,则不会关闭您的流,因为它们不在
using中。 -
IDisposable 对象应该被关闭和处理
-
@bradbury9 理论上是的,但
PdfWriter错误地实现了底层模式。
标签: c# asp.net-core-mvc itext