1

I'm using C#. I've been looking and searching for a way to do this. I've found plenty of examples on ADDING actions to PDF form fields using iTextSharp, but none about reading back existing actions.

I have existing PDF's with form validation (alpha, alphanumeric, etc.) I'm creating a program for filling out these forms, and I need to programmatically check what characters are valid (usually done with JavaScript and REGEX from what I've seen.)

Is there a way to GET this JavaScript so my program can validate the input?

Geostyx
  • 91
  • 1
  • 6
  • Have you tried looking inside the PDF using [iText RUPS](http://itextpdf.com/product/itext_rups). JavaScript can be attached as an action with the JS in a Stream, but you can also have named JavaScript in the Names tree (part of the Catalog). Looking under the hood of the PDF will help you to write code to extract JavaScript programmatically. – Bruno Lowagie Feb 21 '15 at 16:31
  • Brilliant! Using that I learned about XFA and using the method in the answer to [this question](http://stackoverflow.com/questions/14864669/itextsharp-xfa-document-in-c-sharp) I was able to read the full stream as XML! Now I can just parse that to get all the information I need. Thank you so much! – Geostyx Feb 21 '15 at 21:06
  • At iText, we use RUPS all the time. We wrote the tool because we got tired of debugging PDF in a text editor and having to write code to extract a compressed stream for examination :-) – Bruno Lowagie Feb 22 '15 at 05:48

1 Answers1

1

Turns out I need to be reading the full XFA stream to get all the details about form fields.

This code from this answer by this user will get the full XFA stream:

public string ReadXfa(PdfReader reader) {
  XfaForm xfa = new XfaForm(reader);
  XmlDocument doc = xfa.DomDocument;
  reader.Close();

  if (!string.IsNullOrEmpty(doc.DocumentElement.NamespaceURI)) {
    doc.DocumentElement.SetAttribute("xmlns", "");
    XmlDocument new_doc = new XmlDocument();
    new_doc.LoadXml(doc.OuterXml);
    doc = new_doc;
  }

  var sb = new StringBuilder(4000);
  var Xsettings = new XmlWriterSettings() {Indent = true};
  using (var writer = XmlWriter.Create(sb, Xsettings)) {
    doc.WriteTo(writer);
  }
  return sb.ToString();    
}
Community
  • 1
  • 1
Geostyx
  • 91
  • 1
  • 6