【发布时间】:2014-05-27 15:34:20
【问题描述】:
在这种方法中,我试图从一个 PDF 文档中获取输入字段,将它们粘贴到另一个文档中,然后将结果打印为 pdf 文件。结果将是一个新的 PDF 文件,其中包含第一个 PDF 的输入字段和第二个 PDF 的静态内容。
我编写了一些我认为可以执行此任务的代码,但每次执行“copier.close()”时都会遇到 StackOverflow 错误。这是它抛出的错误:
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
这是代码:
public static void AddFormFieldsFromSource(string sourcePath, string secondSourcePath, string targetPath) {
lock (syncLock) {
PdfReader.unethicalreading = true;
PdfReader readerMain = new PdfReader(sourcePath);
FileStream stream = new FileStream(targetPath, FileMode.Create, FileAccess.Write);
PdfCopyForms copier = new PdfCopyForms(stream);
PdfReader secondSourceReader = new PdfReader(secondSourcePath);
copier.AddDocument(secondSourceReader);
copier.CopyDocumentFields(readerMain);
copier.Close();
secondSourceReader.Close();
}
}
源路径是我获取输入字段的地方,第二个源路径是我获取静态内容的地方。
我用于 SourcePath 变量的 PDF 位于此处: https://www.dropbox.com/s/qcc6ug8oohqvmca/primarytwopages2.pdf
我用于 secondSourcePath 变量的 PDF 位于此处: https://www.dropbox.com/s/kx2rlhmizh46hl7/secondarytwopages.pdf
另外,另一方面,我使用的是 ITextSharp 5.5.0 版。
知道为什么会抛出 StackOverflow 错误吗?我没有在我的代码中进行任何递归调用。我的第一个猜测是我试图错误地执行此任务。另一种可能性是 ITextSharp 可能存在错误。
更新:我将源代码下载到 ITextSharp (5.5.1) 的最新版本,构建了一个 dll 以便我可以调试,然后在我的代码中引用了该 dll。此方法中的类 PdfIndirectReference 中出现堆栈溢出错误:
public class PdfIndirectReference : PdfObject {
....
internal PdfIndirectReference(int type, int number, int generation) : base(0, new StringBuilder().Append(number).Append(' ').Append(generation).Append(" R").ToString()) {
this.number = number;
this.generation = generation;
}
在dll代码的调用栈中,发现它在递归中一遍遍地调用一个方法
itextsharp.text.pdf.PdfCopyFieldsImp.Propagate()。
这一定是堆栈溢出发生的原因。
所以,它不会出现在我的代码中,而是出现在 dll 中。知道如何解决这个问题吗?
【问题讨论】:
-
你能提供有问题的pdf吗?
-
当然。给我几分钟,我会提供一个保管箱链接。
-
K,现在我将 Dropbox 链接放在问题文本中。他们导致pdf。感谢您的意见。
-
我明天看看。
标签: c# itextsharp stack-overflow