John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Tom Sepez | 1ed8a21 | 2015-05-11 15:25:39 -0700 | [diff] [blame] | 7 | #include "../../public/fpdf_ppo.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 8 | #include "../include/fsdk_define.h" |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 9 | #include "../include/fpdfxfa/fpdfxfa_doc.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 10 | |
| 11 | class CPDF_PageOrganizer |
| 12 | { |
| 13 | public: |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 14 | CPDF_PageOrganizer(); |
| 15 | ~CPDF_PageOrganizer(); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 16 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 17 | public: |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 18 | FX_BOOL PDFDocInit(CPDF_Document *pDestPDFDoc, CPDF_Document *pSrcPDFDoc); |
| 19 | FX_BOOL ExportPage(CPDF_Document *pSrcPDFDoc, CFX_WordArray* nPageNum, CPDF_Document *pDestPDFDoc, int nIndex); |
| 20 | CPDF_Object* PageDictGetInheritableTag(CPDF_Dictionary *pDict, CFX_ByteString nSrctag); |
| 21 | FX_BOOL UpdateReference(CPDF_Object *pObj, CPDF_Document *pDoc, CFX_MapPtrToPtr* pMapPtrToPtr); |
| 22 | int GetNewObjId(CPDF_Document *pDoc, CFX_MapPtrToPtr* pMapPtrToPtr, CPDF_Reference *pRef); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 23 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | |
| 27 | CPDF_PageOrganizer::CPDF_PageOrganizer() |
| 28 | { |
| 29 | |
| 30 | } |
| 31 | |
| 32 | CPDF_PageOrganizer::~CPDF_PageOrganizer() |
| 33 | { |
| 34 | |
| 35 | } |
| 36 | |
| 37 | FX_BOOL CPDF_PageOrganizer::PDFDocInit(CPDF_Document *pDestPDFDoc, CPDF_Document *pSrcPDFDoc) |
| 38 | { |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 39 | if(!pDestPDFDoc || !pSrcPDFDoc) |
| 40 | return false; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 41 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 42 | CPDF_Dictionary* pNewRoot = pDestPDFDoc->GetRoot(); |
| 43 | if(!pNewRoot) return FALSE; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 44 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 45 | //Set the document information//////////////////////////////////////////// |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 46 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 47 | CPDF_Dictionary* DInfoDict = pDestPDFDoc->GetInfo(); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 48 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 49 | if(!DInfoDict) |
| 50 | return FALSE; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 51 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 52 | CFX_ByteString producerstr; |
| 53 | producerstr.Format("PDFium"); |
| 54 | DInfoDict->SetAt("Producer", new CPDF_String(producerstr)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 55 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 56 | //Set type//////////////////////////////////////////////////////////////// |
| 57 | CFX_ByteString cbRootType = pNewRoot->GetString("Type",""); |
| 58 | if( cbRootType.Equal("") ) |
| 59 | { |
| 60 | pNewRoot->SetAt("Type", new CPDF_Name("Catalog")); |
| 61 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 62 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 63 | CPDF_Dictionary* pNewPages = (CPDF_Dictionary*)(pNewRoot->GetElement("Pages")? pNewRoot->GetElement("Pages")->GetDirect() : NULL); |
| 64 | if(!pNewPages) |
| 65 | { |
| 66 | pNewPages = new CPDF_Dictionary; |
| 67 | FX_DWORD NewPagesON = pDestPDFDoc->AddIndirectObject(pNewPages); |
| 68 | pNewRoot->SetAt("Pages", new CPDF_Reference(pDestPDFDoc, NewPagesON)); |
| 69 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 70 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 71 | CFX_ByteString cbPageType = pNewPages->GetString("Type",""); |
| 72 | if(cbPageType.Equal("")) |
| 73 | { |
| 74 | pNewPages->SetAt("Type", new CPDF_Name("Pages")); |
| 75 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 76 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 77 | CPDF_Array* pKeysArray = pNewPages->GetArray("Kids"); |
| 78 | if(pKeysArray == NULL) |
| 79 | { |
| 80 | CPDF_Array* pNewKids = new CPDF_Array; |
| 81 | FX_DWORD Kidsobjnum = -1; |
| 82 | Kidsobjnum = pDestPDFDoc->AddIndirectObject(pNewKids);//, Kidsobjnum, Kidsgennum); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 83 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 84 | pNewPages->SetAt("Kids", new CPDF_Reference(pDestPDFDoc, Kidsobjnum));//, Kidsgennum)); |
| 85 | pNewPages->SetAt("Count", new CPDF_Number(0)); |
| 86 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 87 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 88 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 91 | FX_BOOL CPDF_PageOrganizer::ExportPage(CPDF_Document *pSrcPDFDoc, CFX_WordArray* nPageNum, |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 92 | CPDF_Document *pDestPDFDoc,int nIndex) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 93 | { |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 94 | int curpage =nIndex; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 95 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 96 | CFX_MapPtrToPtr* pMapPtrToPtr = new CFX_MapPtrToPtr; |
| 97 | pMapPtrToPtr->InitHashTable(1001); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 98 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 99 | for(int i=0; i<nPageNum->GetSize(); i++) |
| 100 | { |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 101 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 102 | CPDF_Dictionary* pCurPageDict = pDestPDFDoc->CreateNewPage(curpage); |
| 103 | CPDF_Dictionary* pSrcPageDict = pSrcPDFDoc->GetPage(nPageNum->GetAt(i)-1); |
| 104 | if(!pSrcPageDict || !pCurPageDict) |
| 105 | { |
| 106 | delete pMapPtrToPtr; |
| 107 | return FALSE; |
| 108 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 109 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 110 | // Clone the page dictionary/////////// |
| 111 | FX_POSITION SrcPos = pSrcPageDict->GetStartPos(); |
| 112 | while (SrcPos) |
| 113 | { |
| 114 | CFX_ByteString cbSrcKeyStr; |
| 115 | CPDF_Object* pObj = pSrcPageDict->GetNextElement(SrcPos, cbSrcKeyStr); |
| 116 | if(cbSrcKeyStr.Compare(("Type")) && cbSrcKeyStr.Compare(("Parent"))) |
| 117 | { |
| 118 | if(pCurPageDict->KeyExist(cbSrcKeyStr)) |
| 119 | pCurPageDict->RemoveAt(cbSrcKeyStr); |
| 120 | pCurPageDict->SetAt(cbSrcKeyStr, pObj->Clone()); |
| 121 | } |
| 122 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 123 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 124 | //inheritable item/////////////////////// |
| 125 | CPDF_Object* pInheritable = NULL; |
| 126 | //1 MediaBox //required |
| 127 | if(!pCurPageDict->KeyExist("MediaBox")) |
| 128 | { |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 129 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 130 | pInheritable = PageDictGetInheritableTag(pSrcPageDict, "MediaBox"); |
| 131 | if(!pInheritable) |
| 132 | { |
| 133 | //Search the "CropBox" from source page dictionary, if not exists,we take the letter size. |
| 134 | pInheritable = PageDictGetInheritableTag(pSrcPageDict, "CropBox"); |
| 135 | if(pInheritable) |
| 136 | pCurPageDict->SetAt("MediaBox", pInheritable->Clone()); |
| 137 | else |
| 138 | { |
| 139 | //Make the default size to be letter size (8.5'x11') |
| 140 | CPDF_Array* pArray = new CPDF_Array; |
| 141 | pArray->AddNumber(0); |
| 142 | pArray->AddNumber(0); |
| 143 | pArray->AddNumber(612); |
| 144 | pArray->AddNumber(792); |
| 145 | pCurPageDict->SetAt("MediaBox", pArray); |
| 146 | } |
| 147 | } |
| 148 | else |
| 149 | pCurPageDict->SetAt("MediaBox", pInheritable->Clone()); |
| 150 | } |
| 151 | //2 Resources //required |
| 152 | if(!pCurPageDict->KeyExist("Resources")) |
| 153 | { |
| 154 | pInheritable = PageDictGetInheritableTag(pSrcPageDict, "Resources"); |
| 155 | if(!pInheritable) |
| 156 | { |
| 157 | delete pMapPtrToPtr; |
| 158 | return FALSE; |
| 159 | } |
| 160 | pCurPageDict->SetAt("Resources", pInheritable->Clone()); |
| 161 | } |
| 162 | //3 CropBox //Optional |
| 163 | if(!pCurPageDict->KeyExist("CropBox")) |
| 164 | { |
| 165 | pInheritable = PageDictGetInheritableTag(pSrcPageDict, "CropBox"); |
| 166 | if(pInheritable) |
| 167 | pCurPageDict->SetAt("CropBox", pInheritable->Clone()); |
| 168 | } |
| 169 | //4 Rotate //Optional |
| 170 | if(!pCurPageDict->KeyExist("Rotate")) |
| 171 | { |
| 172 | pInheritable = PageDictGetInheritableTag(pSrcPageDict, "Rotate"); |
| 173 | if(pInheritable) |
| 174 | pCurPageDict->SetAt("Rotate", pInheritable->Clone()); |
| 175 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 176 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 177 | ///////////////////////////////////////////// |
| 178 | //Update the reference |
| 179 | FX_DWORD dwOldPageObj = pSrcPageDict->GetObjNum(); |
| 180 | FX_DWORD dwNewPageObj = pCurPageDict->GetObjNum(); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 181 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 182 | pMapPtrToPtr->SetAt((void*)(uintptr_t)dwOldPageObj, (void*)(uintptr_t)dwNewPageObj); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 183 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 184 | UpdateReference(pCurPageDict, pDestPDFDoc, pMapPtrToPtr); |
| 185 | curpage++; |
| 186 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 187 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 188 | delete pMapPtrToPtr; |
| 189 | return TRUE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | CPDF_Object* CPDF_PageOrganizer::PageDictGetInheritableTag(CPDF_Dictionary *pDict, CFX_ByteString nSrctag) |
| 193 | { |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 194 | if(!pDict || !pDict->KeyExist("Type") || nSrctag.IsEmpty()) |
| 195 | return NULL; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 196 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 197 | CPDF_Object* pType = pDict->GetElement("Type")->GetDirect(); |
| 198 | if(!pType || pType->GetType() != PDFOBJ_NAME) return NULL; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 199 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 200 | if(pType->GetString().Compare("Page")) return NULL; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 201 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 202 | if(!pDict->KeyExist("Parent")) return NULL; |
| 203 | CPDF_Object* pParent = pDict->GetElement("Parent")->GetDirect(); |
| 204 | if(!pParent || pParent->GetType() != PDFOBJ_DICTIONARY) return NULL; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 205 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 206 | CPDF_Dictionary* pp = (CPDF_Dictionary*)pParent; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 207 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 208 | if(pDict->KeyExist((const char*)nSrctag)) |
| 209 | return pDict->GetElement((const char*)nSrctag); |
| 210 | while (pp) |
| 211 | { |
| 212 | if (pp->KeyExist((const char*)nSrctag)) |
| 213 | return pp->GetElement((const char*)nSrctag); |
| 214 | if (pp->KeyExist("Parent")) |
| 215 | { |
| 216 | pp = (CPDF_Dictionary*)pp->GetElement("Parent")->GetDirect(); |
| 217 | if (pp->GetType() == PDFOBJ_NULL) |
| 218 | break; |
| 219 | } |
| 220 | else |
| 221 | break; |
| 222 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 223 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 224 | return NULL; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 227 | FX_BOOL CPDF_PageOrganizer::UpdateReference(CPDF_Object *pObj, CPDF_Document *pDoc, |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 228 | CFX_MapPtrToPtr* pMapPtrToPtr) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 229 | { |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 230 | switch (pObj->GetType()) |
| 231 | { |
| 232 | case PDFOBJ_REFERENCE: |
| 233 | { |
| 234 | CPDF_Reference* pReference = (CPDF_Reference*)pObj; |
| 235 | int newobjnum = GetNewObjId(pDoc, pMapPtrToPtr, pReference); |
| 236 | if (newobjnum == 0) return FALSE; |
| 237 | pReference->SetRef(pDoc, newobjnum);//, 0); |
| 238 | break; |
| 239 | } |
| 240 | case PDFOBJ_DICTIONARY: |
| 241 | { |
| 242 | CPDF_Dictionary* pDict = (CPDF_Dictionary*)pObj; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 243 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 244 | FX_POSITION pos = pDict->GetStartPos(); |
| 245 | while(pos) |
| 246 | { |
| 247 | CFX_ByteString key(""); |
| 248 | CPDF_Object* pNextObj = pDict->GetNextElement(pos, key); |
| 249 | if (!FXSYS_strcmp(key, "Parent") || !FXSYS_strcmp(key, "Prev") || !FXSYS_strcmp(key, "First")) |
| 250 | continue; |
| 251 | if(pNextObj) |
| 252 | { |
| 253 | if(!UpdateReference(pNextObj, pDoc, pMapPtrToPtr)) |
| 254 | pDict->RemoveAt(key); |
| 255 | } |
| 256 | else |
| 257 | return FALSE; |
| 258 | } |
| 259 | break; |
| 260 | } |
| 261 | case PDFOBJ_ARRAY: |
| 262 | { |
| 263 | CPDF_Array* pArray = (CPDF_Array*)pObj; |
| 264 | FX_DWORD count = pArray->GetCount(); |
| 265 | for(FX_DWORD i = 0; i < count; i ++) |
| 266 | { |
| 267 | CPDF_Object* pNextObj = pArray->GetElement(i); |
| 268 | if(pNextObj) |
| 269 | { |
| 270 | if(!UpdateReference(pNextObj, pDoc, pMapPtrToPtr)) |
| 271 | return FALSE; |
| 272 | } |
| 273 | else |
| 274 | return FALSE; |
| 275 | } |
| 276 | break; |
| 277 | } |
| 278 | case PDFOBJ_STREAM: |
| 279 | { |
| 280 | CPDF_Stream* pStream = (CPDF_Stream*)pObj; |
| 281 | CPDF_Dictionary* pDict = pStream->GetDict(); |
| 282 | if(pDict) |
| 283 | { |
| 284 | if(!UpdateReference(pDict, pDoc, pMapPtrToPtr)) |
| 285 | return FALSE; |
| 286 | } |
| 287 | else |
| 288 | return FALSE; |
| 289 | break; |
| 290 | } |
| 291 | default: break; |
| 292 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 293 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 294 | return TRUE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 297 | int CPDF_PageOrganizer::GetNewObjId(CPDF_Document *pDoc, CFX_MapPtrToPtr* pMapPtrToPtr, |
| 298 | CPDF_Reference *pRef) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 299 | { |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 300 | if(!pRef) |
| 301 | return 0; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 302 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 303 | size_t dwObjnum = pRef->GetRefObjNum(); |
| 304 | size_t dwNewObjNum = 0; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 305 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 306 | pMapPtrToPtr->Lookup((void*)dwObjnum, (void*&)dwNewObjNum); |
| 307 | if(dwNewObjNum) |
| 308 | { |
| 309 | return (int)dwNewObjNum; |
| 310 | } |
Jun Fang | 4cf3695 | 2014-09-08 11:27:02 -0700 | [diff] [blame] | 311 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 312 | CPDF_Object* pDirect = pRef->GetDirect(); |
| 313 | if(!pDirect) |
| 314 | { |
| 315 | return 0; |
| 316 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 317 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 318 | CPDF_Object* pClone = pDirect->Clone(); |
| 319 | if(!pClone) |
| 320 | { |
| 321 | return 0; |
| 322 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 323 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 324 | if(pClone->GetType() == PDFOBJ_DICTIONARY) |
| 325 | { |
| 326 | CPDF_Dictionary* pDictClone = (CPDF_Dictionary*)pClone; |
| 327 | if(pDictClone->KeyExist("Type")) |
| 328 | { |
| 329 | CFX_ByteString strType = pDictClone->GetString("Type"); |
| 330 | if(!FXSYS_stricmp(strType, "Pages")) |
| 331 | { |
| 332 | pDictClone->Release(); |
| 333 | return 4; |
| 334 | } |
| 335 | else if(!FXSYS_stricmp(strType, "Page")) |
| 336 | { |
| 337 | pDictClone->Release(); |
| 338 | return 0; |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | dwNewObjNum = pDoc->AddIndirectObject(pClone); |
| 344 | pMapPtrToPtr->SetAt((void*)dwObjnum, (void*)dwNewObjNum); |
| 345 | if(!UpdateReference(pClone, pDoc, pMapPtrToPtr)) |
| 346 | { |
| 347 | pClone->Release(); |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | return (int)dwNewObjNum; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | FPDF_BOOL ParserPageRangeString(CFX_ByteString rangstring, CFX_WordArray* pageArray,int nCount) |
| 355 | { |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 356 | if(rangstring.GetLength() != 0) |
| 357 | { |
| 358 | rangstring.Remove(' '); |
| 359 | int nLength = rangstring.GetLength(); |
| 360 | CFX_ByteString cbCompareString("0123456789-,"); |
| 361 | for(int i=0; i<nLength; i++) |
| 362 | { |
| 363 | if(cbCompareString.Find(rangstring[i]) == -1) |
| 364 | return FALSE; |
| 365 | } |
| 366 | CFX_ByteString cbMidRange; |
| 367 | int nStringFrom = 0; |
| 368 | int nStringTo=0; |
| 369 | while(nStringTo < nLength) |
| 370 | { |
| 371 | nStringTo = rangstring.Find(',',nStringFrom); |
| 372 | if(nStringTo == -1) |
| 373 | { |
| 374 | nStringTo = nLength; |
| 375 | } |
| 376 | cbMidRange = rangstring.Mid(nStringFrom,nStringTo-nStringFrom); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 377 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 378 | int nMid = cbMidRange.Find('-'); |
| 379 | if(nMid == -1) |
| 380 | { |
| 381 | long lPageNum = atol(cbMidRange); |
| 382 | if(lPageNum <= 0 || lPageNum > nCount) |
| 383 | return FALSE; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 384 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 385 | pageArray->Add((FX_WORD)lPageNum); |
| 386 | } |
| 387 | else |
| 388 | { |
| 389 | int nStartPageNum = atol(cbMidRange.Mid(0,nMid)); |
| 390 | if (nStartPageNum ==0) |
| 391 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 392 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 393 | nMid = nMid+1; |
| 394 | int nEnd = cbMidRange.GetLength()-nMid; |
| 395 | if (nEnd ==0) |
| 396 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 397 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 398 | int nEndPageNum = atol(cbMidRange.Mid(nMid,nEnd)); |
| 399 | if (nStartPageNum < 0 ||nStartPageNum >nEndPageNum|| nEndPageNum > nCount) |
| 400 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 401 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 402 | for (int nIndex = nStartPageNum; nIndex <= nEndPageNum; ++nIndex) |
| 403 | pageArray->Add(nIndex); |
| 404 | } |
| 405 | nStringFrom = nStringTo + 1; |
| 406 | } |
| 407 | } |
| 408 | return TRUE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 411 | DLLEXPORT FPDF_BOOL STDCALL FPDF_ImportPages(FPDF_DOCUMENT dest_doc,FPDF_DOCUMENT src_doc, |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 412 | FPDF_BYTESTRING pagerange, int index) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 413 | { |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 414 | if(dest_doc == NULL || src_doc == NULL ) |
| 415 | return FALSE; |
| 416 | CFX_WordArray pageArray; |
| 417 | CPDFXFA_Document* pSrcDoc = (CPDFXFA_Document*)src_doc; |
| 418 | CPDF_Document* pSrcPDFDoc = pSrcDoc->GetPDFDoc(); |
| 419 | int nCount = pSrcPDFDoc->GetPageCount(); |
| 420 | if(pagerange) |
| 421 | { |
| 422 | if(ParserPageRangeString(pagerange,&pageArray,nCount) == FALSE) |
| 423 | return FALSE; |
| 424 | } |
| 425 | else |
| 426 | { |
| 427 | for(int i=1; i<=nCount; i++) |
| 428 | { |
| 429 | pageArray.Add(i); |
| 430 | } |
| 431 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 432 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 433 | CPDFXFA_Document* pDestDoc = (CPDFXFA_Document*)dest_doc; |
| 434 | CPDF_Document* pDestPDFDoc = pDestDoc->GetPDFDoc(); |
| 435 | CPDF_PageOrganizer pageOrg; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 436 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 437 | pageOrg.PDFDocInit(pDestPDFDoc,pSrcPDFDoc); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 438 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 439 | if(pageOrg.ExportPage(pSrcPDFDoc,&pageArray,pDestPDFDoc,index)) |
| 440 | return TRUE; |
| 441 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | DLLEXPORT FPDF_BOOL STDCALL FPDF_CopyViewerPreferences(FPDF_DOCUMENT dest_doc, FPDF_DOCUMENT src_doc) |
| 445 | { |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame^] | 446 | if(src_doc == NULL || dest_doc == NULL) |
| 447 | return false; |
| 448 | CPDFXFA_Document* pSrcDoc = (CPDFXFA_Document*)src_doc; |
| 449 | CPDF_Document* pSrcPDFDoc = pSrcDoc->GetPDFDoc(); |
| 450 | CPDF_Dictionary* pSrcDict = pSrcPDFDoc->GetRoot(); |
| 451 | pSrcDict = pSrcDict->GetDict(FX_BSTRC("ViewerPreferences")); |
| 452 | if(!pSrcDict) |
| 453 | return FALSE; |
| 454 | CPDFXFA_Document* pDstDoc = (CPDFXFA_Document*)dest_doc; |
| 455 | CPDF_Document* pDstPDFDoc = pDstDoc->GetPDFDoc(); |
| 456 | CPDF_Dictionary* pDstDict = pDstPDFDoc->GetRoot(); |
| 457 | if(!pDstDict) |
| 458 | return FALSE; |
| 459 | pDstDict->SetAt(FX_BSTRC("ViewerPreferences"), pSrcDict->Clone(TRUE)); |
| 460 | return TRUE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 461 | } |
| 462 | |