@@ -64,6 +64,33 @@ namespace iText.Kernel.Pdf.Layer {
6464 public class PdfOCProperties : PdfObjectWrapper < PdfDictionary > {
6565 private IList < PdfLayer > layers = new List < PdfLayer > ( ) ;
6666
67+ /// <summary>
68+ /// Gets the order of the layers in which they will be displayed in the layer view panel,
69+ /// including nesting.
70+ /// </summary>
71+ private static void GetOCGOrder ( PdfArray order , PdfLayer layer ) {
72+ if ( ! layer . IsOnPanel ( ) ) {
73+ return ;
74+ }
75+ if ( layer . GetTitle ( ) == null ) {
76+ order . Add ( layer . GetPdfObject ( ) . GetIndirectReference ( ) ) ;
77+ }
78+ IList < PdfLayer > children = layer . GetChildren ( ) ;
79+ if ( children == null ) {
80+ return ;
81+ }
82+ PdfArray kids = new PdfArray ( ) ;
83+ if ( layer . GetTitle ( ) != null ) {
84+ kids . Add ( new PdfString ( layer . GetTitle ( ) , PdfEncodings . UNICODE_BIG ) ) ;
85+ }
86+ foreach ( PdfLayer child in children ) {
87+ GetOCGOrder ( kids , child ) ;
88+ }
89+ if ( kids . Size ( ) > 0 ) {
90+ order . Add ( kids ) ;
91+ }
92+ }
93+
6794 /// <summary>Creates a new PdfOCProperties instance.</summary>
6895 /// <param name="document">the document the optional content belongs to</param>
6996 public PdfOCProperties ( PdfDocument document )
@@ -145,6 +172,7 @@ public virtual PdfObject FillDictionary() {
145172 if ( rbGroups != null ) {
146173 d . Put ( PdfName . RBGroups , rbGroups ) ;
147174 }
175+ d . Put ( PdfName . Name , new PdfString ( CreateUniqueName ( ) , PdfEncodings . UNICODE_BIG ) ) ;
148176 GetPdfObject ( ) . Put ( PdfName . D , d ) ;
149177 IList < PdfLayer > docOrder = new List < PdfLayer > ( layers ) ;
150178 for ( int i = 0 ; i < docOrder . Count ; i ++ ) {
@@ -193,6 +221,24 @@ public virtual PdfObject FillDictionary() {
193221 return GetPdfObject ( ) ;
194222 }
195223
224+ private String CreateUniqueName ( ) {
225+ int uniqueID = 0 ;
226+ ICollection < String > usedNames = new HashSet < String > ( ) ;
227+ PdfArray configs = GetPdfObject ( ) . GetAsArray ( PdfName . Configs ) ;
228+ if ( null != configs ) {
229+ for ( int i = 0 ; i < configs . Size ( ) ; i ++ ) {
230+ PdfDictionary alternateDictionary = configs . GetAsDictionary ( i ) ;
231+ if ( null != alternateDictionary && alternateDictionary . ContainsKey ( PdfName . Name ) ) {
232+ usedNames . Add ( alternateDictionary . GetAsString ( PdfName . Name ) . ToUnicodeString ( ) ) ;
233+ }
234+ }
235+ }
236+ while ( usedNames . Contains ( "OCConfigName" + uniqueID ) ) {
237+ uniqueID ++ ;
238+ }
239+ return "OCConfigName" + uniqueID ;
240+ }
241+
196242 public override void Flush ( ) {
197243 FillDictionary ( ) ;
198244 base . Flush ( ) ;
@@ -224,33 +270,6 @@ protected internal virtual PdfDocument GetDocument() {
224270 return GetPdfObject ( ) . GetIndirectReference ( ) . GetDocument ( ) ;
225271 }
226272
227- /// <summary>
228- /// Gets the order of the layers in which they will be displayed in the layer view panel,
229- /// including nesting.
230- /// </summary>
231- private static void GetOCGOrder ( PdfArray order , PdfLayer layer ) {
232- if ( ! layer . IsOnPanel ( ) ) {
233- return ;
234- }
235- if ( layer . GetTitle ( ) == null ) {
236- order . Add ( layer . GetPdfObject ( ) . GetIndirectReference ( ) ) ;
237- }
238- IList < PdfLayer > children = layer . GetChildren ( ) ;
239- if ( children == null ) {
240- return ;
241- }
242- PdfArray kids = new PdfArray ( ) ;
243- if ( layer . GetTitle ( ) != null ) {
244- kids . Add ( new PdfString ( layer . GetTitle ( ) , PdfEncodings . UNICODE_BIG ) ) ;
245- }
246- foreach ( PdfLayer child in children ) {
247- GetOCGOrder ( kids , child ) ;
248- }
249- if ( kids . Size ( ) > 0 ) {
250- order . Add ( kids ) ;
251- }
252- }
253-
254273 /// <summary>Populates the /AS entry in the /D dictionary.</summary>
255274 private void AddASEvent ( PdfName @event , PdfName category ) {
256275 PdfArray arr = new PdfArray ( ) ;
0 commit comments