77//
88// Command-line: <input-file> <output-file> (Both optional)
99//
10- #include < iostream>
11- #include < map>
1210#include < set>
13- #include < utility>
14- #include < string>
1511
1612#include " InitializeLibrary.h"
1713#include " APDFLDoc.h"
@@ -46,7 +42,6 @@ int main(int argc, char **argv) {
4642 << " and writing to " << csOutputFileName.c_str () << std::endl;
4743
4844 // Step 1) Determine what kinds of content will be copied.
49-
5045 // All element types in this set will be copied, others will be ignored
5146 std::set<ASInt32> sContentTypes ;
5247 sContentTypes .insert (kPDEContainer );
@@ -70,9 +65,7 @@ int main(int argc, char **argv) {
7065 sPagesToCopy .insert (5 );
7166
7267 DURING
73-
7468 // Step 2) Open the input PDF, create the output PDF.
75-
7669 APDFLDoc inAPDoc (csInputFileName.c_str (), true );
7770 PDDoc inDoc = inAPDoc.getPDDoc ();
7871
@@ -83,7 +76,6 @@ int main(int argc, char **argv) {
8376 PDDoc outDoc = outAPDoc.getPDDoc ();
8477
8578 // Step 3) Copy the specified content from the input PDF into the output PDF.
86-
8779 for (int i = 0 ; i < iInDocPages; i++) {
8880 if (!sPagesToCopy .empty () && (sPagesToCopy .find (i) == sPagesToCopy .end ())) {
8981 // Don't copy this page if the page selection set is not empty, and this page number isn't in it!
@@ -115,12 +107,10 @@ int main(int argc, char **argv) {
115107 }
116108
117109 // Step 4) Save the output PDF and close both PDFs.
118-
119110 outAPDoc.saveDoc (csOutputFileName.c_str ());
120111
121112 // APDFLDoc's destructor will properly close both documents,
122113 // and APDFLib's will shut down the library
123-
124114 HANDLER
125115 errCode = ERRORCODE;
126116 lib.displayError (errCode);
@@ -135,22 +125,23 @@ int main(int argc, char **argv) {
135125// "scalar" element types are copied directly
136126void copyElements (PDEContent to, PDEContent from, const std::set<ASInt32> &ElementTypesSet) {
137127 DURING
138-
139128 ASInt32 i, iNumElems = PDEContentGetNumElems (from);
140129
141130 for (i = 0 ; i < iNumElems; ++i) {
142131 // Fetch each element, and get its type
143- PDEElement nextElem = PDEContentGetElem (from, i);
144- ASInt32 type = PDEObjectGetType (reinterpret_cast <PDEObject>(nextElem ));
132+ PDEElement elem = PDEContentGetElem (from, i);
133+ ASInt32 type = PDEObjectGetType (reinterpret_cast <PDEObject>(elem ));
145134
146135 if (ElementTypesSet.find (type) == ElementTypesSet.end ()) {
147136 continue ;
148137 }
149138
139+ PDEClip clip = PDEElementGetClip (elem);
140+
150141 switch (type) {
151142 case kPDEContainer : {
152143 // This is a compound container, need to recurse
153- PDEContainer fromContainer = reinterpret_cast <PDEContainer>(nextElem );
144+ PDEContainer fromContainer = reinterpret_cast <PDEContainer>(elem );
154145 PDEContent fromContent = PDEContainerGetContent (fromContainer);
155146
156147 // The new container, to which which we give blank contents, and give to the output document.
@@ -161,6 +152,10 @@ void copyElements(PDEContent to, PDEContent from, const std::set<ASInt32> &Eleme
161152
162153 copyElements (toContent, fromContent, ElementTypesSet);
163154
155+ if (clip != NULL ) {
156+ PDEElementSetClip (reinterpret_cast <PDEElement>(toContainer), clip);
157+ }
158+
164159 // Now copy the new container into "to".
165160 PDEContentAddElem (to, kPDEAfterLast , reinterpret_cast <PDEElement>(toContainer));
166161
@@ -171,7 +166,7 @@ void copyElements(PDEContent to, PDEContent from, const std::set<ASInt32> &Eleme
171166
172167 case kPDEGroup : {
173168 // This is a compound container, need to recurse
174- PDEGroup fromGroup = reinterpret_cast <PDEGroup>(nextElem );
169+ PDEGroup fromGroup = reinterpret_cast <PDEGroup>(elem );
175170 PDEContent fromContent = PDEGroupGetContent (fromGroup);
176171
177172 PDEGroup toGroup = PDEGroupCreate ();
@@ -180,6 +175,10 @@ void copyElements(PDEContent to, PDEContent from, const std::set<ASInt32> &Eleme
180175
181176 copyElements (toContent, fromContent, ElementTypesSet);
182177
178+ if (clip != NULL ) {
179+ PDEElementSetClip (reinterpret_cast <PDEElement>(toGroup), clip);
180+ }
181+
183182 // Now copy the new group into "to".
184183 PDEContentAddElem (to, kPDEAfterLast , reinterpret_cast <PDEElement>(toGroup));
185184
@@ -189,17 +188,21 @@ void copyElements(PDEContent to, PDEContent from, const std::set<ASInt32> &Eleme
189188
190189 case kPDEForm : {
191190 // This is a compound container, need to recurse
192- PDEForm fromForm = reinterpret_cast <PDEForm>(nextElem );
191+ PDEForm fromForm = reinterpret_cast <PDEForm>(elem );
193192 PDEContent fromFormContent = PDEFormGetContent (fromForm);
194193
195- // Note: this also clones the underlying xObject Cos Object(s), for each occurence .
194+ // Note: this also clones the underlying xObject Cos Object(s), for each occurrence .
196195 PDEForm toForm = PDEFormCreateClone (fromForm);
197196
198197 // Replace the contents of toForm with a new one, with only the elements we want copied
199198 PDEContent toContent = PDEContentCreate ();
200199 copyElements (toContent, fromFormContent, ElementTypesSet);
201200 PDEFormSetContent (toForm, toContent);
202201
202+ if (clip != NULL ) {
203+ PDEElementSetClip (reinterpret_cast <PDEElement>(toForm), clip);
204+ }
205+
203206 // Now copy the new form into "to".
204207 PDEContentAddElem (to, kPDEAfterLast , reinterpret_cast <PDEElement>(toForm));
205208
@@ -211,7 +214,12 @@ void copyElements(PDEContent to, PDEContent from, const std::set<ASInt32> &Eleme
211214 }
212215 default : {
213216 // Must be a "scalar" - just copy it
214- PDEElement copyNextElem = PDEElementCopy (nextElem, kPDEElementCopyClipping );
217+ PDEElement copyNextElem = PDEElementCopy (elem, kPDEElementCopyClipping );
218+
219+ if (clip != NULL ) {
220+ PDEElementSetClip (reinterpret_cast <PDEElement>(copyNextElem), clip);
221+ }
222+
215223 PDEContentAddElem (to, kPDEAfterLast , copyNextElem);
216224 PDERelease (reinterpret_cast <PDEObject>(copyNextElem));
217225 } break ;
0 commit comments