I've got a similar problem. I want to add new layer to a PDF document, already containing several layers. The most obvious way would be something like this:
PdfReader reader = new PdfReader(new RandomAccessFileOrArray("c:\\books\\new book.pdf"), null); PdfStamper stamper = new PdfStamper(reader,new FileOutputStream("c:\\books\\new book2.pdf")); PdfContentByte cbl = stamper.getOverContent(1); cbl.beginLayer(new PdfLayer("additional layer", stamper.getWriter())); /* here i add some content */ cbl.endLayer(); stamper.close(); But the result PDF contains only one layer (yep, only "additional layer" i've just added). After some random tries i found, that if i call stamper.getPdfLayers() before adding new layer and after it, all the layers (old and newly added) are in the map, returned after second call. But if i call getPdfLayers() after adding "additional layer", returned map contains only this "additional layer". Quite glad with myself for that finding, i call getPdfLayers(), then add new layer, then call close() on PdfStamper object i use... and get this same exception: Exception in thread "main" java.lang.NullPointerException at com.itextpdf.text.pdf.PdfWriter.fillOCProperties(PdfWriter.java:2585) at com.itextpdf.text.pdf.PdfStamperImp.close(PdfStamperImp.java:189) at com.itextpdf.text.pdf.PdfStamper.close(PdfStamper.java:189) at pdftest.Main.main(Main.java:118) Line 2585 reads like this: if (OCGRadioGroup.size() > 0) ... and yes, OCGRadioGroup was null. After searching the iText code i found the problem is with method readOCProperties() in PdfStamperImp class (it is called first thing in getPdfLayers() in the same class), which ends with documentOCG.addAll(ocgmap.values()); OCGRadioGroup = d.getAsArray(PdfName.RBGROUPS); OCGLocked = d.getAsArray(PdfName.LOCKED); if (OCGLocked == null) OCGLocked = new PdfArray(); And sure thing, if PDF does not contain any optional content groups with radio button behavior, field OCGRadioGroup becomes null. I added if (OCGRadioGroup == null) OCGRadioGroup = new PdfArray(); after line 1606 in PdfStamperImp, rebuilt iText with ant and the problem was resolved. Can't say if it'll affect anything else... P.S. I used iText 5.0.6, downloaded from svn about a week ago (last wednesday to be exact). -- View this message in context: http://itext-general.2136553.n4.nabble.com/remove-watermark-layer-tp2264258p3234481.html Sent from the iText - General mailing list archive at Nabble.com. ------------------------------------------------------------------------------ Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)! Finally, a world-class log management solution at an even better price-free! Download using promo code Free_Logger_4_Dev2Dev. Offer expires February 28th, so secure your free ArcSight Logger TODAY! http://p.sf.net/sfu/arcsight-sfd2d _______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/itext-questions Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/ Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php