0

I have editable pdf with fields name, roll number , DOB and city, I want to validate the fields like, for example when I enter the value in roll number field, if name is empty then it should alert "name is empty" and similar to roll number and DOB and city. DOB - Need to validate mm/dd/yyyy format. I am able to get fields and values using below iTextSharp code, but how can validate and and show alert to user.

iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(filename);
        using (MemoryStream outputStream = new MemoryStream())
        {
            //PdfStamper stamper = new PdfStamper(reader, );
            using (PdfStamper stamper = new PdfStamper(reader, outputStream, '\0', true))
            {

                PdfWriter writer = stamper.Writer;

                AcroFields acroFields = reader.AcroFields;
                AcroFields.Item dateField = acroFields.GetFieldItem("D");
                iTextSharp.text.pdf.PdfAction pdfAction = iTextSharp.text.pdf.PdfAction.JavaScript("app.alert('hello');", writer);

                iTextSharp.text.pdf.PdfDictionary widgetRefDict = (iTextSharp.text.pdf.PdfDictionary)iTextSharp.text.pdf.PdfReader.GetPdfObject(dateField.GetWidgetRef(0));
                iTextSharp.text.pdf.PdfDictionary actionDict = widgetRefDict.GetAsDict(iTextSharp.text.pdf.PdfName.AA);
                if (actionDict == null)
                {
                    actionDict = new iTextSharp.text.pdf.PdfDictionary();
                    // add the newly created action dict
                    widgetRefDict.Put(iTextSharp.text.pdf.PdfName.AA, actionDict);

                }
                actionDict.Put(iTextSharp.text.pdf.PdfName.V, pdfAction);

                stamper.Close();
                reader.Close();
            }
            byte[] content = outputStream.ToArray();

            // Write out PDF from memory stream.
            using (FileStream fs = File.Create("d:\\Page1-output.pdf"))
            {
                fs.Write(content, 0, (int)content.Length);
            }

        }
mail2vguna
  • 25
  • 2
  • 9
  • 2
    I can't see any reason why this is tagged with Ghostscript. So I've removed that tag. – KenS Mar 09 '22 at 10:01
  • 1
    Do you want to validate the field values with iText, i.e. when the form is already filled and you have the PDF, or do you want to show alerts to the user in the process of filling out a form? – Alexey Subach Mar 14 '22 at 19:27
  • Yes, I want to validate the field values with iText or any other open source. do you want to show alerts to the user in the process of filling out a form? - YES. Thanks – mail2vguna Mar 17 '22 at 05:34
  • @AlexeySubach Hi, Do you have any idea to go with show alerts to the user in the process of filling out a form. – mail2vguna Mar 26 '22 at 06:35
  • @mail2vguna@mail2vguna There is probably some misunderstanding going on. One option is to add some JS validation at the stage when user is filling out the form in their PDF viewing tools (such as Adobe Reader). Then you can modify your document with iText to add that JS validation. See https://stackoverflow.com/questions/32905756/adding-javascript-validation-to-existing-acroform-field-in-pdf-with-itext for more details. Another option is to validate values that user has filled in on the server side. You have code for that already more or less. Can you edit your question with more details? – Alexey Subach Mar 29 '22 at 09:40
  • Hi @AlexeySubach Thanks for your response. I have update my question. Kindly check and update me. Thanks – mail2vguna Mar 30 '22 at 16:39
  • Hi @alexeySubach, I have updated the code but still i did not get alert message while filling pdf file. – mail2vguna Apr 18 '22 at 07:52

0 Answers0