When using the PdfStamper (in append mode) to fill in a form in a certificate-protected pdf file, I get an object reference error with the following stack trace:
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=itextsharp
StackTrace:
at iTextSharp.text.pdf.PdfEncryption.CreateInfoId(Byte[] id, Boolean modified)
at iTextSharp.text.pdf.PdfEncryption.GetFileID(Boolean modified)
at iTextSharp.text.pdf.PdfStamperImp.Close(PdfIndirectReference info, Int32 skipInfo)
at iTextSharp.text.pdf.PdfStamperImp.Close(IDictionary`2 moreInfo)
at iTextSharp.text.pdf.PdfStamper.Close()
at iTextSharp.text.pdf.PdfStamper.Dispose()
at ConsoleApplication1.Program.TestExecute(String inputFileName, String outputFileName, String certFileName, String certPassword) in D:\Temp\ConsoleApplication5\ConsoleApplication1\Program.cs:line 26
at ConsoleApplication1.Program.Main(String[] args) in D:\Temp\ConsoleApplication5\ConsoleApplication1\Program.cs:line 7
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
The error was generated by the following piece of test code:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TestExecute(@"D:\Temp\ConsoleApplication5\Resources\CertProtected.pdf", @"D:\Temp\ConsoleApplication5\Resources\CertProtected_Filled.pdf",
@"D:\Temp\ConsoleApplication5\Resources\John Smith.pfx", "secret");
}
public static void TestExecute(string inputFileName, string outputFileName, string certFileName, string certPassword)
{
iTextSharp.text.pdf.PdfReader reader = null;
try
{
var inputCertificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(certFileName, certPassword, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.Exportable);
var inputBouncyCertficate = Org.BouncyCastle.Security.DotNetUtilities.FromX509Certificate(inputCertificate);
var inputKeyPair = Org.BouncyCastle.Security.DotNetUtilities.GetKeyPair(inputCertificate.PrivateKey);
reader = new iTextSharp.text.pdf.PdfReader(inputFileName, inputBouncyCertficate, inputKeyPair.Private);
using (var outputStream = new System.IO.FileStream(outputFileName, System.IO.FileMode.Create))
using (var pdfStamper = new iTextSharp.text.pdf.PdfStamper(reader, outputStream, reader.PdfVersion, true))
{
var fields = pdfStamper.AcroFields;
fields.SetField("Surname", "Smith");
}
}
finally
{
if (reader != null)
reader.Dispose();
}
}
}
}
I'm using iTextSharp 5.5.10 and have also tested against the latest version of the code in GitHub. The certificate does have a private key and I can successfully open (and populate) the pdf file in Acrobat DC. My issue looks very similar to this previously logged issue, but it doesn't look like it was ever answered. Any help would be appreciated.