@@ -64,6 +64,33 @@ namespace iText.Kernel.Pdf.Layer {
64
64
public class PdfOCProperties : PdfObjectWrapper < PdfDictionary > {
65
65
private IList < PdfLayer > layers = new List < PdfLayer > ( ) ;
66
66
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
+
67
94
/// <summary>Creates a new PdfOCProperties instance.</summary>
68
95
/// <param name="document">the document the optional content belongs to</param>
69
96
public PdfOCProperties ( PdfDocument document )
@@ -145,6 +172,7 @@ public virtual PdfObject FillDictionary() {
145
172
if ( rbGroups != null ) {
146
173
d . Put ( PdfName . RBGroups , rbGroups ) ;
147
174
}
175
+ d . Put ( PdfName . Name , new PdfString ( CreateUniqueName ( ) , PdfEncodings . UNICODE_BIG ) ) ;
148
176
GetPdfObject ( ) . Put ( PdfName . D , d ) ;
149
177
IList < PdfLayer > docOrder = new List < PdfLayer > ( layers ) ;
150
178
for ( int i = 0 ; i < docOrder . Count ; i ++ ) {
@@ -193,6 +221,24 @@ public virtual PdfObject FillDictionary() {
193
221
return GetPdfObject ( ) ;
194
222
}
195
223
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
+
196
242
public override void Flush ( ) {
197
243
FillDictionary ( ) ;
198
244
base . Flush ( ) ;
@@ -224,33 +270,6 @@ protected internal virtual PdfDocument GetDocument() {
224
270
return GetPdfObject ( ) . GetIndirectReference ( ) . GetDocument ( ) ;
225
271
}
226
272
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
-
254
273
/// <summary>Populates the /AS entry in the /D dictionary.</summary>
255
274
private void AddASEvent ( PdfName @event , PdfName category ) {
256
275
PdfArray arr = new PdfArray ( ) ;
0 commit comments