ITextSharp adds a pre-existing PDF as a layer to another PDF

I have reports that are converted to PDF files. Some of these reports lack information, simply because we do not track it. I created another PDF file with a report wrapper and placed input controls on it. I would like to know if there is a way to apply the shell PDF file to the converted PDF file so that users can enter information in these empty fields without having to print them out and write them manually? I did this manually through Adobe Acrobat Pro 9.3, applying the generated PDF file to the PDF wrapper as Layer . I have done a lot with iTextSharp regarding Layers , but I still have not found anything that worked.

Thank you for the advanced!

+4
source share
1 answer

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.

+2
source

All Articles