2 years ago

#267452

test-img

mail2vguna

Field validation in editable pdf while filling data

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);
            }

        }

itext

itext7

aspose.pdf

0 Answers

Your Answer

Accepted video resources