1+ using Datalogics . PDFL ;
12using System ;
23using System . Collections . Generic ;
3- using System . Text ;
4- using Datalogics . PDFL ;
54
65/*
7- * This sample adds an Optional Content Group (a layer ) to a PDF document and
8- * then adds an image to that layer.
6+ * This sample adds Optional Content Groups (layers ) to a PDF document and
7+ * then adds Content to those layers.
98 *
109 * The related ChangeLayerConfiguration program makes layers visible or invisible.
1110 *
12- * You can toggle back and forth to make the layer (the duck image) visible or invisible
13- * in the PDF file. Open the output PDF document in a PDF Viewer, click View and select
14- * Show/Hide. Select Navigation Panes and Layers to display the layers in the PDF file.
15- * Click on the box next to the name of the layer.
11+ * You can toggle back and forth to make a layer visible or invisible
12+ * in a PDF Viewer.
1613 *
1714 */
1815namespace CreateLayer
@@ -38,70 +35,129 @@ static void Main(string[] args)
3835
3936 Console . WriteLine ( "Input file: " + sInput + ", writing to " + sOutput ) ;
4037
41- Document doc = new Document ( sInput ) ;
42-
43- Console . WriteLine ( "Opened a document." ) ;
44-
45- Page pg = doc . GetPage ( 0 ) ;
46- Image img = ( pg . Content . GetElement ( 0 ) as Image ) ;
47-
48- // Containers, Forms and Annotations can be attached to an
49- // OptionalContentGroup; other content (like Image) can
50- // be made optional by placing it inside a Container
51- Container container = new Container ( ) ;
52- container . Content = new Content ( ) ;
53- container . Content . AddElement ( img ) ;
38+ using ( Document doc = new Document ( sInput ) )
39+ {
40+ Console . WriteLine ( "Opened a document." ) ;
41+
42+ using ( Page pg = doc . GetPage ( 0 ) )
43+ {
44+ Image image = ( Image ) pg . Content . GetElement ( 0 ) ;
45+ image . Matrix = new Matrix ( image . Matrix . A * .5 , 0 , 0 , image . Matrix . D * .5 , image . Matrix . H , image . Matrix . V ) ;
46+
47+ Image image2 = new Image ( Library . ResourceDirectory + "Sample_Input/Image.png" ) ;
48+
49+ Text text = new Text ( ) ;
50+ Matrix matrix = new Matrix ( ) ;
51+ Font font = new Font ( "Helvetica" ) ;
52+ GraphicState graphicState = new GraphicState ( ) ;
53+ TextState textState = new TextState ( ) ;
54+
55+ matrix . A = 42 ;
56+ matrix . D = 22 ;
57+ matrix . H = 72 ;
58+ matrix . V = 72 ;
59+
60+ TextRun textRun = new TextRun ( "sample text" , font , graphicState , textState , matrix ) ;
61+ text . AddRun ( textRun ) ;
62+
63+ Text text2 = new Text ( ) ;
64+
65+ matrix . A = 30 ;
66+ matrix . D = 30 ;
67+ matrix . H = 72 ;
68+ matrix . V = 288 ;
69+
70+ TextRun textRun2 = new TextRun ( "Text definition provided here" , font , graphicState , textState , matrix ) ;
71+ text2 . AddRun ( textRun2 ) ;
72+
73+ // Containers, Forms and Annotations can be attached to an
74+ // OptionalContentGroup; other content (like Image) can
75+ // be made optional by placing it inside a Container
76+ Container imageContainer = new Container ( ) ;
77+ imageContainer . Content = new Content ( ) ;
78+ imageContainer . Content . AddElement ( image ) ;
79+
80+ Container imageContainer2 = new Container ( ) ;
81+ imageContainer2 . Content = new Content ( ) ;
82+ imageContainer2 . Content . AddElement ( image2 ) ;
83+
84+ Container textContainer = new Container ( ) ;
85+ textContainer . Content = new Content ( ) ;
86+ textContainer . Content . AddElement ( text ) ;
87+
88+ Container textContainer2 = new Container ( ) ;
89+ textContainer2 . Content = new Content ( ) ;
90+ textContainer2 . Content . AddElement ( text2 ) ;
91+
92+ using ( Document newDoc = new Document ( ) )
93+ {
94+ using ( Page newPage = newDoc . CreatePage ( Document . BeforeFirstPage , pg . MediaBox ) )
95+ {
96+ newPage . Content . AddElement ( imageContainer ) ;
97+ newPage . Content . AddElement ( imageContainer2 ) ;
98+ newPage . Content . AddElement ( textContainer ) ;
99+ newPage . Content . AddElement ( textContainer2 ) ;
100+
101+ // We create new OptionalContentGroups and place them in the OptionalContentConfig.Order array
102+ List < OptionalContentGroup > ocgs = CreateNewOptionalContentGroups ( newDoc , new List < string > { "Rubber Ducky" , "PNG Logo" , "Example Text" , "Text Definition" } ) ;
103+
104+ AssociateOCGWithContainer ( newDoc , ocgs [ 0 ] , imageContainer ) ;
105+ AssociateOCGWithContainer ( newDoc , ocgs [ 1 ] , imageContainer2 ) ;
106+ AssociateOCGWithContainer ( newDoc , ocgs [ 2 ] , textContainer ) ;
107+ AssociateOCGWithContainer ( newDoc , ocgs [ 3 ] , textContainer2 ) ;
108+
109+ newPage . UpdateContent ( ) ;
110+
111+ newDoc . Save ( SaveFlags . Full , sOutput ) ;
112+ }
113+ }
114+ }
115+ }
116+ }
117+ }
54118
55- // We replace the Image with the Container
56- // (which now holds the image)
57- pg . Content . RemoveElement ( 0 ) ;
58- pg . UpdateContent ( ) ;
119+ public static List < OptionalContentGroup > CreateNewOptionalContentGroups ( Document doc , List < string > names )
120+ {
121+ List < OptionalContentGroup > ocgs = new List < OptionalContentGroup > ( ) ;
59122
60- pg . Content . AddElement ( container ) ;
61- pg . UpdateContent ( ) ;
123+ OptionalContentGroup ocg = new OptionalContentGroup ( doc , names [ 0 ] ) ;
124+ OptionalContentGroup ocg2 = new OptionalContentGroup ( doc , names [ 1 ] ) ;
125+ OptionalContentGroup ocg3 = new OptionalContentGroup ( doc , names [ 2 ] ) ;
126+ OptionalContentGroup ocg4 = new OptionalContentGroup ( doc , names [ 3 ] ) ;
62127
63- // We create a new OptionalContentGroup and place it in the
64- // OptionalContentConfig.Order array
65- OptionalContentGroup ocg = CreateNewOptionalContentGroup ( doc , "Rubber Ducky" ) ;
128+ ocgs . Add ( ocg ) ;
129+ ocgs . Add ( ocg2 ) ;
130+ ocgs . Add ( ocg3 ) ;
131+ ocgs . Add ( ocg4 ) ;
66132
67- // Now we associate the Container with the OptionalContentGroup
68- // via an OptionalContentMembershipDict. Note that we MUST
69- // update the Page's content afterwards.
70- AssociateOCGWithContainer ( doc , ocg , container ) ;
71- pg . UpdateContent ( ) ;
133+ // Add it to the Order array -- this is required so that it will appear in the 'Layers' panel in a PDF Viewer.
134+ OptionalContentOrderArray order_list = doc . DefaultOptionalContentConfig . Order ;
72135
73- doc . Save ( SaveFlags . Full , sOutput ) ;
74- }
75- }
136+ OptionalContentOrderArray grouping = new OptionalContentOrderArray ( doc , "Image Grouping" ) ;
137+ grouping . Add ( new OptionalContentOrderLeaf ( ocg ) ) ;
138+ grouping . Add ( new OptionalContentOrderLeaf ( ocg2 ) ) ;
76139
77- // Create an OptionalContentGroup with a given name, and add it to the
78- // default OptionalContentConfig's Order array.
79- public static OptionalContentGroup CreateNewOptionalContentGroup ( Document doc , string name )
80- {
81- // Create an OptionalContentGroup
82- OptionalContentGroup ocg = new OptionalContentGroup ( doc , name ) ;
140+ OptionalContentOrderArray grouping2 = new OptionalContentOrderArray ( doc , "Text Grouping" ) ;
141+ grouping2 . Add ( new OptionalContentOrderLeaf ( ocg3 ) ) ;
142+ grouping2 . Add ( new OptionalContentOrderLeaf ( ocg4 ) ) ;
83143
84- // Add it to the Order array -- this is required so that the OptionalContentGroup
85- // will appear in the 'Layers' control panel in a PDF Viewer. It will appear in
86- // the control panel with the name given in the OptionalContentGroup constructor.
87- OptionalContentOrderArray order_list = doc . DefaultOptionalContentConfig . Order ;
88- order_list . Insert ( order_list . Length , new OptionalContentOrderLeaf ( ocg ) ) ;
144+ order_list . Insert ( order_list . Length , grouping ) ;
145+ order_list . Insert ( order_list . Length , grouping2 ) ;
89146
90- return ocg ;
147+ return ocgs ;
91148 }
92149
93150 // Associate a Container with an OptionalContentGroup via an OptionalContentMembershipDict.
94- // This function associates a Containter with a single OptionalContentGroup and uses
151+ // This function associates a Container with a single OptionalContentGroup and uses
95152 // a VisibilityPolicy of AnyOn.
96153 public static void AssociateOCGWithContainer ( Document doc , OptionalContentGroup ocg , Container cont )
97154 {
98155 // Create an OptionalContentMembershipDict. The options here are appropriate for a
99156 // 'typical' usage; other options can be used to create an 'inverting' layer
100157 // (i.e. 'Display this content when the layer is turned OFF'), or to make the
101158 // Container's visibility depend on several OptionalContentGroups
102- OptionalContentMembershipDict ocmd = new OptionalContentMembershipDict ( doc , new OptionalContentGroup [ ] { ocg } , VisibilityPolicy . AnyOn ) ;
159+ OptionalContentMembershipDict ocmd = new OptionalContentMembershipDict ( doc , new OptionalContentGroup [ ] { ocg } , VisibilityPolicy . AnyOn ) ;
103160
104- // Associate the Container with the OptionalContentMembershipDict
105161 cont . OptionalContentMembershipDict = ocmd ;
106162 }
107163 }
0 commit comments