2

I am required to replace a word in an existing PDF AcroField with another word. I am using PDFStamper of iTEXTSHARP to do the same and it is working fine. But, in doing so it is required to create a new PDF and i would like the change to be reflected in the existing PDF itself. If I am setting the destination filename same as the original filename then no change is being reflected.I am new to iTextSharp , is there anything I am doing wrong? Please help.. I am providing the piece of code I am using

  private void ListFieldNames(string s)
    {
        try
        {
            string pdfTemplate = @"z:\TEMP\PDF\PassportApplicationForm_Main_English_V1.0.pdf";
            string newFile = @"z:\TEMP\PDF\PassportApplicationForm_Main_English_V1.0.pdf";
            PdfReader pdfReader = new PdfReader(pdfTemplate);

            for (int page = 1; page <= pdfReader.NumberOfPages; page++)
            {
                PdfReader reader = new PdfReader((string)pdfTemplate);
                using (PdfStamper stamper = new PdfStamper(reader, new FileStream(newFile, FileMode.Create, FileAccess.ReadWrite)))
                {
                    AcroFields form = stamper.AcroFields;
                    var fieldKeys = form.Fields.Keys;
                    foreach (string fieldKey in fieldKeys)
                    {
                        //Replace Address Form field with my custom data
                        if (fieldKey.Contains("Address"))
                        {
                            form.SetField(fieldKey, s);
                        }    
                    }
                    stamper.FormFlattening = true;
                    stamper.Close();

                }

            }
        }
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
tk2013
  • 31
  • 2
  • 6

1 Answers1

5

As documented in my book iText in Action, you can't read a file and write to it simultaneously. Think of how Word works: you can't open a Word document and write directly to it. Word always creates a temporary file, writes the changes to it, then replaces the original file with it and then throws away the temporary file.

You can do that too:

  • read the original file with PdfReader,
  • create a temporary file for PdfStamper, and when you're done,
  • replace the original file with the temporary file.

Or:

  • read the original file into a byte[],
  • create PdfReader with this byte[], and
  • use the path to the original file for PdfStamper.

This second option is more dangerous, as you'll lose the original file if you do something that causes an exception in PdfStamper.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • I have followed your first approach and created a temp file using stamper,that was later used to replace the original file. But, as the PDF is published from SDL Tridion and stored in the Tridion file system , we are not able to make any changes to the original- edit/ delete it. Evrytime I try to delete the original the following error is given : e:\Web\Compliance\NonPCI\SDL\wwwroot\xyzrights\updatePdf.aspx:line 150Access to the path 'E:\Web\Compliance\NonPCI\SDL\wwwroot\xyzrights\Images\ListPrograming_Part1_AcroForm.pdf' is denied. Please help.. – tk2013 May 07 '13 at 11:16
  • 1
    If you don't have permission to delete the file, you probably don't have permission to update it either. Its permissions are probably set to read-only . You need to contact the sysadmin. This is no longer an iText question but a question about file permissions. – Bruno Lowagie May 07 '13 at 11:26
  • @BrunoLowagie could you explain this a little bit more please? =>** use the path to the original file for PdfStamper.** – jason Apr 12 '17 at 14:33
  • 1
    If the existing file is `"C:/somefolder/some.pdf"`, you can read `some.pdf` into a `byte[]`, create the `PdfReader` with that `byte[]`, and write the new PDF to `"C:/somefolder/some.pdf"` (which is the path to the original file). However, only a mad programmer would take that approach, a cautious developer would use the first approach. – Bruno Lowagie Apr 12 '17 at 14:38