Question

I have spent several hours researching this and can't seem to locate the answer. I have downloaded and referenced itextsharp in my wpf .net application. (VB)

What I am doing is needing to turn off a specific layer (ocg object) in an exisiting .pdf that was created in Autocad that is defaulted on.

I have successfully opened and displayed the .pdf but i can't seem to use the setOCGstate control correctly pdf name is "random.pdf"

layer name that i can see once i open the .pdf is "Option 1"

where im getting stuck is i know the layer names are stored in an array inside the .pdf. i know the name of the layer i am trying to turn off, so how do i reference that layer and turn it off using the setocgstate.

example code

dim doc1 as New PdfReader("random.pdf")

PdfAction.SetOCGstate ("confused", False)

Was it helpful?

Solution

I've created an example that turns off the visibility of a specific layer. See ChangeOCG

The concept is really simple. You already have a PdfReader object and you want to apply a change to a file. As documented, you create a PdfStamper object. As you want to change an OCG layer, you use the getPdfLayers() method and you select the layer you want to change by name. (In my example, the layer I want to turn off is named "Nested layer 1"). You use the setOn() method to change its status, and you're done:

PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
Map<String, PdfLayer> layers = stamper.getPdfLayers();
PdfLayer layer = layers.get("Nested layer 1");
layer.setOn(false);
stamper.close();
reader.close();

This is Java code. Please read it as if it were pseudo-code and adapt it to your language of choice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top