简体   繁体   中英

iTextSharp add pre-existing PDF as a layer to another PDF

I have reports that are being converted into PDFs. Some of these reports have information missing simply because we don't track it. I have created another PDF with the shell of the report and placed input fields controls on it. I would like to know if there is a way to apply the shell PDF to the converted PDF so users can enter information in those blank fields without having to print them out and hand write them? I have done this manually through Adobe Acrobat Pro 9.3 by applying the generated PDF to the shell PDF as a Layer . I have done as much poking around with iTextSharp concerning Layers , but I still haven't found anything that has worked.

Thank you in advanced!

1) Layers won't work with fields. PDF Layers are a part of the page contents. Form fields, as with all annotations, float above the page.

2) Having said that, you can hide and reveal form fields using Acrobat/Reader JavaScript. The "doc" object is usually "this" in field and page entry points, so to show a given field, it's just:

var fld = this.getField("fieldName");
fld.hidden = false;

There are quite a few different places you can add JS to a PDF. Various field events, page events, and document events. You can also set a layer's action to some javaScript. Heck you can set a bookmark's action to be javascript instead of a "go over there" action.

Note that layers are called "Optional Content Groups" (OCGs) in PDF tech-speak. If you really want to create a layer, it looks like it would go 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 a number of examples on the iText site corresponding to "iText In Action, 2nd edition" (I don't get paid, the author is a friend). The aforementioned examples can be found here .

This bears repeating: Fields cannot be part of an OCG (layer). They can however be scripted to act like they are.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM