1) Layers will not work with fields. PDF layers are part of the page content. Form fields, like all annotations, float above the page.
2) Having said that, you can hide and show form fields using Acrobat / Reader JavaScript. The "doc" object is usually "this" at the entry points of the field and page, so to display this field is simple:
var fld = this.getField("fieldName"); fld.hidden = false;
There are several different places where you can add JS to PDF. Various field events, page events, and document events. You can also set the action of the layer to some javaScript. For example, you can set the bookmarks action as javascript instead of the "go there" action.
Note that the layers are called “Optional Content Groups” (OCGs) in Tech Tech. If you really want to create a layer, it looks like it will look something like this:
// layer implements PdfOCG PdfLayer layer = new PdfLayer("MyLayer", writer); PdfContentByte cb = getAContentByteFromSomewhere(); cb.beginLayer(layer); // takes PDFOCG object /* draw stuff to be part of that layer */ cb.endLayer();
There are several examples on the iText website corresponding to “iText In Action, 2nd edition” (they don’t pay me, the author is a friend). The above examples can be found here .
This is repeated: fields cannot be part of an OCG (layer). However, they can be written as they are.
source share