Hide and re-display the layers of a PDF in the Acrobat panel

0

I am using iTextSharp 5.5.10.0.

I have a PDF with several layers resulting from having joined several files. When I open it with Acrobat Reader, the layers and the original names of the files that I joined appear.

First problem:
If I use this code once, in the Acrobat panel, the layers disappear and the names of the files remain attached:

Public Shared Sub OcultarLayers(ByVal source As String, ByVal destination As String)
    Using reader = New PdfReader(source)
        reader.ConsolidateNamedDestinations()
        reader.RemoveUnusedObjects()
        Using stamper As PdfStamper = New PdfStamper(reader, New FileStream(destination, FileMode.Create, FileAccess.Write, FileShare.None))
            Dim capas = From cada In stamper.GetPdfLayers
            For Each capa In capas
                capa.Value.OnPanel = False
            Next
        End Using
    End Using
End Sub

I have to use it twice so that the names of the files do not appear:

Public Shared Sub OcultarLayers(ByVal source As String, ByVal destination As String)
 Using memo As MemoryStream = New MemoryStream
    Using readerMemo = New PdfReader(source)
        readerMemo.ConsolidateNamedDestinations()
        readerMemo.RemoveUnusedObjects()
        Using stamperMemo As PdfStamper = New PdfStamper(readerMemo, memo)
            stamperMemo.Writer.CloseStream = False
            Dim capas = From cada In stamperMemo.GetPdfLayers
            For Each capa In capas
                capa.Value.OnPanel = False
            Next
        End Using
    End Using
    memo.Position = 0
    Using reader = New PdfReader(memo)
        reader.ConsolidateNamedDestinations()
        reader.RemoveUnusedObjects()
        Using stamper As PdfStamper = New PdfStamper(reader, New FileStream(destination, FileMode.Create, FileAccess.Write, FileShare.None))
            Dim capas = From cada In stamper.GetPdfLayers
            For Each capa In capas
                capa.Value.OnPanel = False
            Next
        End Using
    End Using
End Using

Second Problem:

It does not work to put capa.Value.OnPanel = True again.

    
asked by Fran 22.02.2017 в 10:56
source

1 answer

0

Solution to the first problem

The solution to the fudge of modifying the same file twice is this:

    Using reader = New PdfReader(source)
        reader.ConsolidateNamedDestinations()
        reader.RemoveUnusedObjects()
        Using stamper As PdfStamper = New PdfStamper(reader, New FileStream(destination, FileMode.Create, FileAccess.Write, FileShare.None))
            Dim capas = From cada In stamper.GetPdfLayers
            For Each capa In capas
                Dim padre As PdfLayer = capa.Value
                Do While Not IsNothing(padre.Parent)
                    padre = padre.Parent
                Loop
                padre.OnPanel = False
            Next
        End Using
    End Using

The property of the children layers can not be changed without changing the parent:

  

public void setOnPanel (boolean onPanel)

     

Sets the visibility of the layer in Acrobat's layer panel. If   false the layer can not be directly manipulated by the user. Note that   any children layers will also be absent from the panel.

To the second problem I have not found a solution.

I have tried changing parent and children and it does not work.

    
answered by 08.03.2017 / 11:07
source