0

I don't know if I'm missing something (I'm no iTextSharp expert by any means), but everything I've read about iTextSharp's PdfStamper class says that upon calling of the Close() method, it should write whatever stream (so a FileStream should then get written to disk). The below using statements should automatically close those methods I believe, but even when I've manually called PdfStamper.Close() inside the using statements, my new file doesn't get written to disk. Could someone please tell me why? Thanks in advance.

        var outputStream = new FileStream("SamplePDFs\\SampleOutput.pdf", FileMode.Create);

        using (var pdfReader = new PdfReader("SamplePDFs\\SampleInput.pdf"))
        using (var pdfStamper = new PdfStamper(pdfReader, outputStream))
        {
            foreach (var field in pdfReader.AcroForm.Fields)
            {
                Console.WriteLine("Name: " + field.Name);

                if(field.Name == "SomeField")
                {
                    var stamperFields = pdfStamper.AcroFields;
                    stamperFields.SetField(field.Name, "TESTING");
                }
            }
        }
KSwift87
  • 1,843
  • 5
  • 23
  • 40
  • Is there any exception thrown? I don't know C#, but I know iText quite well and I know that what you're trying to do has been done successfully by thousands of developers. – Bruno Lowagie Mar 16 '15 at 07:36
  • 1
    There's nothing wrong with the code. Do you even get an empty file? Is it writting where you think it is? – Paulo Soares Mar 16 '15 at 09:59
  • I don't even get an empty file. And yes, it's writing where it should because I can verify that it's picking up the sample input file (the input and output both use the same directory). And if an exception were being thrown the debugger should vomit and I'm not getting that either. I gotta be missing something really stupid and obvious. I'll check it again this evening when I get home from work. – KSwift87 Mar 16 '15 at 10:33
  • @PauloSoares -- WOW; after looking further into this issue, then reconsidering your comment, I realized that the folder I was looking at wasn't in the freaking bin directory. Good grief. After finding the same-named folder in my bin directory and running my test, my modified PDF file appeared fine. If you state your comment as an answer, I'll happily accept it. Thank you! – KSwift87 Mar 17 '15 at 00:51

0 Answers0