static int InsertNewPage(CPDF_Document* pDoc, int iPage, CPDF_Dictionary* pPageDict, CFX_DWordArray &pageList) { CPDF_Dictionary* pRoot = pDoc->GetRoot(); if (!pRoot) { return -1; } CPDF_Dictionary* pPages = pRoot->GetDict(FX_BSTRC("Pages")); if (!pPages) { return -1; } int nPages = pDoc->GetPageCount(); if (iPage < 0 || iPage > nPages) { return -1; } if (iPage == nPages) { CPDF_Array* pPagesList = pPages->GetArray(FX_BSTRC("Kids")); if (!pPagesList) { pPagesList = FX_NEW CPDF_Array; pPages->SetAt(FX_BSTRC("Kids"), pPagesList); } pPagesList->Add(pPageDict, pDoc); pPages->SetAtInteger(FX_BSTRC("Count"), nPages + 1); pPageDict->SetAtReference(FX_BSTRC("Parent"), pDoc, pPages->GetObjNum()); } else { CFX_PtrArray stack; stack.Add(pPages); if (InsertDeletePDFPage(pDoc, pPages, iPage, pPageDict, TRUE, stack) < 0) { return -1; } } pageList.InsertAt(iPage, pPageDict->GetObjNum()); return iPage; }
static int InsertDeletePDFPage(CPDF_Document* pDoc, CPDF_Dictionary* pPages, int nPagesToGo, CPDF_Dictionary* pPage, FX_BOOL bInsert, CFX_ArrayTemplate<CPDF_Dictionary*>& stackList) { CPDF_Array* pKidList = pPages->GetArray("Kids"); if (!pKidList) { return -1; } int nKids = pKidList->GetCount(); for (int i = 0; i < nKids; i++) { CPDF_Dictionary* pKid = pKidList->GetDict(i); if (pKid->GetString("Type") == "Page") { if (nPagesToGo == 0) { if (bInsert) { pKidList->InsertAt(i, new CPDF_Reference(pDoc, pPage->GetObjNum())); pPage->SetAtReference("Parent", pDoc, pPages->GetObjNum()); } else { pKidList->RemoveAt(i); } pPages->SetAtInteger("Count", pPages->GetInteger("Count") + (bInsert ? 1 : -1)); return 1; } nPagesToGo--; } else { int nPages = pKid->GetInteger("Count"); if (nPagesToGo < nPages) { int stackCount = stackList.GetSize(); for (int j = 0; j < stackCount; ++j) { if (pKid == stackList[j]) { return -1; } } stackList.Add(pKid); if (InsertDeletePDFPage(pDoc, pKid, nPagesToGo, pPage, bInsert, stackList) < 0) { return -1; } stackList.RemoveAt(stackCount); pPages->SetAtInteger("Count", pPages->GetInteger("Count") + (bInsert ? 1 : -1)); return 1; } nPagesToGo -= nPages; } } return 0; }
void CPDF_Document::DeletePage(int iPage) { CPDF_Dictionary* pPages = GetPagesDict(); if (!pPages) return; int nPages = pPages->GetIntegerFor("Count"); if (iPage < 0 || iPage >= nPages) return; std::set<CPDF_Dictionary*> stack = {pPages}; if (InsertDeletePDFPage(this, pPages, iPage, nullptr, FALSE, &stack) < 0) return; m_PageList.RemoveAt(iPage); }
void CPDF_Document::DeletePage(int iPage) { CPDF_Dictionary* pRoot = GetRoot(); if (!pRoot) { return; } CPDF_Dictionary* pPages = pRoot->GetDict("Pages"); if (!pPages) { return; } int nPages = pPages->GetInteger("Count"); if (iPage < 0 || iPage >= nPages) { return; } CFX_ArrayTemplate<CPDF_Dictionary*> stack; stack.Add(pPages); if (InsertDeletePDFPage(this, pPages, iPage, NULL, FALSE, stack) < 0) { return; } m_PageList.RemoveAt(iPage); }