diff --git a/PdfFile/PdfEditor.cpp b/PdfFile/PdfEditor.cpp index 7ac52c61f1..64469b8af7 100644 --- a/PdfFile/PdfEditor.cpp +++ b/PdfFile/PdfEditor.cpp @@ -160,7 +160,7 @@ bool SplitSkipDict(Object* obj) oType.free(); return false; } -PdfWriter::CObjectBase* DictToCDictObject2(Object* obj, PdfWriter::CDocument* pDoc, XRef* xref, std::map& mUniqueRef) +PdfWriter::CObjectBase* DictToCDictObject2(Object* obj, PdfWriter::CDocument* pDoc, XRef* xref, std::map& mUniqueRef, int nAddObjToXRef = 0) { PdfWriter::CObjectBase* pBase = NULL; Object oTemp; @@ -214,6 +214,12 @@ PdfWriter::CObjectBase* DictToCDictObject2(Object* obj, PdfWriter::CDocument* pD case objArray: { PdfWriter::CArrayObject* pArray = new PdfWriter::CArrayObject(); + if (nAddObjToXRef > 0) + { + pDoc->AddObject(pArray); + mUniqueRef[nAddObjToXRef] = pArray; + nAddObjToXRef = 0; + } for (int nIndex = 0; nIndex < obj->arrayGetLength(); ++nIndex) { obj->arrayGetNF(nIndex, &oTemp); @@ -229,6 +235,12 @@ PdfWriter::CObjectBase* DictToCDictObject2(Object* obj, PdfWriter::CDocument* pD if (SplitSkipDict(obj)) return new PdfWriter::CNullObject(); PdfWriter::CDictObject* pDict = new PdfWriter::CDictObject(); + if (nAddObjToXRef > 0) + { + pDoc->AddObject(pDict); + mUniqueRef[nAddObjToXRef] = pDict; + nAddObjToXRef = 0; + } for (int nIndex = 0; nIndex < obj->dictGetLength(); ++nIndex) { char* chKey = obj->dictGetKey(nIndex); @@ -248,9 +260,7 @@ PdfWriter::CObjectBase* DictToCDictObject2(Object* obj, PdfWriter::CDocument* pD return it->second; obj->fetch(xref, &oTemp); - pBase = DictToCDictObject2(&oTemp, pDoc, xref, mUniqueRef); - pDoc->AddObject(pBase); - mUniqueRef[nObjNum] = pBase; + pBase = DictToCDictObject2(&oTemp, pDoc, xref, mUniqueRef, nObjNum); oTemp.free(); break; } @@ -261,8 +271,15 @@ PdfWriter::CObjectBase* DictToCDictObject2(Object* obj, PdfWriter::CDocument* pD } case objStream: { - Dict* pODict = obj->streamGetDict(); PdfWriter::CDictObject* pDict = new PdfWriter::CDictObject(); + if (nAddObjToXRef > 0) + { + pDoc->AddObject(pDict); + mUniqueRef[nAddObjToXRef] = pDict; + nAddObjToXRef = 0; + } + + Dict* pODict = obj->streamGetDict(); int nLength = 0; for (int nIndex = 0; nIndex < pODict->getLength(); ++nIndex) { @@ -297,6 +314,11 @@ PdfWriter::CObjectBase* DictToCDictObject2(Object* obj, PdfWriter::CDocument* pD case objEOF: break; } + if (nAddObjToXRef > 0) + { + pDoc->AddObject(pBase); + mUniqueRef[nAddObjToXRef] = pBase; + } return pBase; } @@ -855,7 +877,7 @@ CPdfEditor::CPdfEditor(const std::wstring& _wsSrcFile, const std::wstring& _wsPa if (!bRes) m_nError = 5; // Ошибка применения редактирования } -CPdfEditor::CPdfEditor(CPdfReader* _pReader, CPdfWriter* _pWriter) +CPdfEditor::CPdfEditor(CPdfReader* _pReader, CPdfWriter* _pWriter, unsigned int unPages) { m_pReader = _pReader; m_pWriter = _pWriter; @@ -877,6 +899,21 @@ CPdfEditor::CPdfEditor(CPdfReader* _pReader, CPdfWriter* _pWriter) m_nError = 1; return; } + + // Страницы должны быть созданы заранее для ссылки + Catalog* pCatalog = pPDFDocument->getCatalog(); + for (int i = 1; i <= unPages; ++i) + { + Ref* pPageRef = pCatalog->getPageRef(i); + if (pPageRef->num == 0) + { + return; + } + + PdfWriter::CPage* pPage = new PdfWriter::CPage(pDoc); + pDoc->AddObject(pPage); + pDoc->AddPage(pPage); + } } void CPdfEditor::Close() { diff --git a/PdfFile/PdfEditor.h b/PdfFile/PdfEditor.h index 5b49e1f05e..1a43060c63 100644 --- a/PdfFile/PdfEditor.h +++ b/PdfFile/PdfEditor.h @@ -41,7 +41,7 @@ class CPdfEditor { public: CPdfEditor(const std::wstring& _wsSrcFile, const std::wstring& _wsPassword, CPdfReader* _pReader, const std::wstring& _wsDstFile, CPdfWriter* _pWriter); - CPdfEditor(CPdfReader* _pReader, CPdfWriter* _pWriter); + CPdfEditor(CPdfReader* _pReader, CPdfWriter* _pWriter, unsigned int unPages); int GetError(); void Close(); diff --git a/PdfFile/PdfFile.cpp b/PdfFile/PdfFile.cpp index 34f86534df..5013a01b49 100644 --- a/PdfFile/PdfFile.cpp +++ b/PdfFile/PdfFile.cpp @@ -68,6 +68,10 @@ public: class CPdfFile_Private { +public: + bool SplitPage(int nPageIndex); + bool SplitPages(const int* arrPageIndex, unsigned int unLength, IRenderer* pRenderer); + public: std::wstring wsSrcFile; std::wstring wsPassword; @@ -165,28 +169,32 @@ bool CPdfFile::AddPage(int nPageIndex) return false; return m_pInternal->pEditor->AddPage(nPageIndex); } -bool CPdfFile::SplitPage(int nPageIndex) -{ - if (!m_pInternal->pEditor) - { - if (!m_pInternal->pReader) - return false; - - if (!m_pInternal->pWriter) - m_pInternal->pWriter = new CPdfWriter(m_pInternal->pAppFonts, false, this); - - m_pInternal->pEditor = new CPdfEditor(m_pInternal->pReader, m_pInternal->pWriter); - if (m_pInternal->pEditor->GetError() != 0) - return false; - } - return m_pInternal->pEditor->SplitPage(nPageIndex); -} bool CPdfFile::SplitPages(const std::vector& arrPageIndex) { return SplitPages(arrPageIndex.data(), arrPageIndex.size()); } bool CPdfFile::SplitPages(const int* arrPageIndex, unsigned int unLength) { + return m_pInternal->SplitPages(arrPageIndex, unLength, this); +} +bool CPdfFile_Private::SplitPage(int nPageIndex) +{ + return pEditor->SplitPage(nPageIndex); +} +bool CPdfFile_Private::SplitPages(const int* arrPageIndex, unsigned int unLength, IRenderer* pRenderer) +{ + if (!pEditor) + { + if (!pReader) + return false; + + if (!pWriter) + pWriter = new CPdfWriter(pAppFonts, false, pRenderer); + + pEditor = new CPdfEditor(pReader, pWriter, unLength); + if (pEditor->GetError() != 0) + return false; + } bool bRes = true; for (unsigned int i = 0; i < unLength; ++i) bRes &= SplitPage(arrPageIndex[i]); diff --git a/PdfFile/PdfFile.h b/PdfFile/PdfFile.h index 36db98434c..7a03e0190b 100644 --- a/PdfFile/PdfFile.h +++ b/PdfFile/PdfFile.h @@ -97,7 +97,6 @@ public: bool EditPage (int nPageIndex); bool DeletePage (int nPageIndex); bool AddPage (int nPageIndex); - bool SplitPage (int nPageIndex); bool SplitPages (const std::vector& arrPageIndex); bool SplitPages (const int* arrPageIndex, unsigned int unLength); HRESULT ChangePassword(const std::wstring& wsPath, const std::wstring& wsPassword = L""); diff --git a/PdfFile/test/test.cpp b/PdfFile/test/test.cpp index cd72d7fe12..181711fd2e 100644 --- a/PdfFile/test/test.cpp +++ b/PdfFile/test/test.cpp @@ -358,8 +358,7 @@ TEST_F(CPdfFileTest, SplitPdf) pdfFile->CreatePdf(); - pdfFile->SplitPage(0); - pdfFile->SplitPages({2, 3}); + pdfFile->SplitPages({0, 1}); pdfFile->SaveToFile(wsDstFile); }