Your problem can not be reproduced. Please look at the AddOCG to find out how I tried to reproduce the problem and failed. Which is a good thing, because it means that there is no problem.
I took the example from my book OptionalContentExample.cs and I changed it so that it doesn't create a new document from scratch, but instead adds optional content to an existing document.
This involves using PdfStamper and using the getPdfLayers() method:
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
Map<String, PdfLayer> layers = stamper.getPdfLayers();
PdfWriter writer = stamper.getWriter();
PdfContentByte cb = stamper.getOverContent(1);
PdfLayer group = PdfLayer.createTitle("Grouped layers", writer);
PdfLayer layer1 = new PdfLayer("Group: layer 1", writer);
PdfLayer layer2 = new PdfLayer("Group: layer 2", writer);
group.addChild(layer1);
group.addChild(layer2);
cb.beginLayer(layer1);
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
"layer 1 in the group"), 50, 700, 0);
cb.endLayer();
cb.beginLayer(layer2);
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
"layer 2 in the group"), 50, 675, 0);
cb.endLayer();
stamper.close();
reader.close();
The resulting PDF hello_OCG.pdf has all the layers as expected and all layers work the same way the layers work in the book example. That is: the layers work when viewed in PDF Viewers that support this functionality. Some PDF viewers completely ignore OCG.