开发者

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 o开发者_StackOverflowf 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜