正在处理 PdfStamper
PdfStamper being disposed
我传递给此方法的 PdfStamper 在该方法结束时被处理 - 为什么,我该如何停止它?我正在尝试从模板创建一个页面对象,然后我可以将其添加到 PdfStamper X 次。
//real code
public void DoSpecialAction(PdfStamper pdfStamper)
{
using (var pdfTemplate = new PdfReader(_extraPageTemplatePath))
using (var pdfReader = new PdfReader(pdfTemplate))
{
PdfImportedPage page = pdfStamper.GetImportedPage(pdfReader, 1);
pdfStamper.InsertPage(3, pdfReader.GetPageSize(1));
PdfContentByte pb = pdfStamper.GetUnderContent(3);
pb.AddTemplate(page, 0, 0);
}
}
程序结构如下:
//psuedocode
class PrintFieldsToPdf {
foreach (normalfield) {
PrintNormalFields();
}
foreach (specialaction) {
DoSpecialAction(pdfStamper);
}
pdfStamper.Close(); //at this point the object has been deallocated
}
抛出以下异常:
An exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll but was not handled in user code
Additional information: Cannot access a closed file.
OP 最终评论:
I have a hunch it may be that the page object never actually gets copied until the PdfStamper
calls Close
and writes the file, and therefore the PdfReader
I'm using to read the extra page template is causing the issue, as it is disposed of at the end of my method, before PdfStamper
is closed.
他的预感是正确的:至少原始页面的某些部分的复制会延迟到 PdfStamper
关闭。这允许在不同调用中导入来自同一 PdfReader
实例的多个页面的情况下进行某些优化。
从许多不同 PdfReaders
导入的用例也一直在 iText(Sharp) 开发人员的脑海中。因此,他们提供了一种方法来告诉 PdfStamper
在用户确定他不会从中复制任何其他内容时从给定的 PdfReader
复制所需的所有内容:
public void DoSpecialAction(PdfStamper pdfStamper)
{
using (var pdfTemplate = new PdfReader(_extraPageTemplatePath))
using (var pdfReader = new PdfReader(pdfTemplate))
{
PdfImportedPage page = pdfStamper.GetImportedPage(pdfReader, 1);
pdfStamper.InsertPage(3, pdfReader.GetPageSize(1));
PdfContentByte pb = pdfStamper.GetUnderContent(3);
pb.AddTemplate(page, 0, 0);
// Copy everything required from the PdfReader
pdfStamper.Writer.FreeReader(pdfReader);
}
}
我传递给此方法的 PdfStamper 在该方法结束时被处理 - 为什么,我该如何停止它?我正在尝试从模板创建一个页面对象,然后我可以将其添加到 PdfStamper X 次。
//real code
public void DoSpecialAction(PdfStamper pdfStamper)
{
using (var pdfTemplate = new PdfReader(_extraPageTemplatePath))
using (var pdfReader = new PdfReader(pdfTemplate))
{
PdfImportedPage page = pdfStamper.GetImportedPage(pdfReader, 1);
pdfStamper.InsertPage(3, pdfReader.GetPageSize(1));
PdfContentByte pb = pdfStamper.GetUnderContent(3);
pb.AddTemplate(page, 0, 0);
}
}
程序结构如下:
//psuedocode
class PrintFieldsToPdf {
foreach (normalfield) {
PrintNormalFields();
}
foreach (specialaction) {
DoSpecialAction(pdfStamper);
}
pdfStamper.Close(); //at this point the object has been deallocated
}
抛出以下异常:
An exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll but was not handled in user code
Additional information: Cannot access a closed file.
OP 最终评论:
I have a hunch it may be that the page object never actually gets copied until the
PdfStamper
callsClose
and writes the file, and therefore thePdfReader
I'm using to read the extra page template is causing the issue, as it is disposed of at the end of my method, beforePdfStamper
is closed.
他的预感是正确的:至少原始页面的某些部分的复制会延迟到 PdfStamper
关闭。这允许在不同调用中导入来自同一 PdfReader
实例的多个页面的情况下进行某些优化。
从许多不同 PdfReaders
导入的用例也一直在 iText(Sharp) 开发人员的脑海中。因此,他们提供了一种方法来告诉 PdfStamper
在用户确定他不会从中复制任何其他内容时从给定的 PdfReader
复制所需的所有内容:
public void DoSpecialAction(PdfStamper pdfStamper)
{
using (var pdfTemplate = new PdfReader(_extraPageTemplatePath))
using (var pdfReader = new PdfReader(pdfTemplate))
{
PdfImportedPage page = pdfStamper.GetImportedPage(pdfReader, 1);
pdfStamper.InsertPage(3, pdfReader.GetPageSize(1));
PdfContentByte pb = pdfStamper.GetUnderContent(3);
pb.AddTemplate(page, 0, 0);
// Copy everything required from the PdfReader
pdfStamper.Writer.FreeReader(pdfReader);
}
}