From a9b225782d070ff7e19e2abadb50212ca9c58be3 Mon Sep 17 00:00:00 2001 From: Svetlana Kulikova Date: Fri, 31 Jan 2025 13:38:29 +0300 Subject: [PATCH] Create MovePage --- PdfFile/OnlineOfficeBinToPdf.cpp | 10 ++++++++++ PdfFile/PdfEditor.cpp | 13 +++++++++++-- PdfFile/PdfEditor.h | 3 ++- PdfFile/PdfFile.cpp | 6 ++++++ PdfFile/PdfFile.h | 1 + PdfFile/SrcWriter/Document.cpp | 17 +++++++++++++++-- PdfFile/SrcWriter/Document.h | 3 ++- 7 files changed, 47 insertions(+), 6 deletions(-) diff --git a/PdfFile/OnlineOfficeBinToPdf.cpp b/PdfFile/OnlineOfficeBinToPdf.cpp index b41b122b24..674f2c30e6 100644 --- a/PdfFile/OnlineOfficeBinToPdf.cpp +++ b/PdfFile/OnlineOfficeBinToPdf.cpp @@ -150,6 +150,7 @@ namespace NSOnlineOfficeBinToPdf AddPage = 1, RemovePage = 2, WidgetInfo = 3, + MovePage = 4, Undefined = 255 }; @@ -198,6 +199,15 @@ namespace NSOnlineOfficeBinToPdf oReader.Skip(nLen - 9); break; } + case AddCommandType::MovePage: + { + int nPos = oReader.ReadInt(); + pPdf->MovePage(nPageNum, nPos); + + NSOnlineOfficeBinToPdf::ConvertBufferToRenderer(oReader.GetCurrentBuffer(), (LONG)(nLen - 13) , &oCorrector); + oReader.Skip(nLen - 13); + break; + } case AddCommandType::RemovePage: { pPdf->DeletePage(nPageNum); diff --git a/PdfFile/PdfEditor.cpp b/PdfFile/PdfEditor.cpp index 2874f4ce90..fe0ee45886 100644 --- a/PdfFile/PdfEditor.cpp +++ b/PdfFile/PdfEditor.cpp @@ -895,7 +895,7 @@ void CPdfEditor::GetPageTree(XRef* xref, Object* pPagesRefObj, PdfWriter::CPageT } kidsArrObj.free(); } -bool CPdfEditor::EditPage(int nPageIndex, bool bSet) +bool CPdfEditor::EditPage(int nPageIndex, bool bSet, int nNewPage) { PDFDoc* pPDFDocument = pReader->GetPDFDocument(); PdfWriter::CDocument* pDoc = pWriter->GetDocument(); @@ -1030,7 +1030,7 @@ bool CPdfEditor::EditPage(int nPageIndex, bool bSet) pageObj.free(); // Применение редактирования страницы для writer - if (pDoc->EditPage(pXref, pPage, nPageIndex)) + if (pDoc->EditPage(pXref, pPage, nPageIndex, nNewPage)) { if (bSet) { @@ -1073,6 +1073,15 @@ bool CPdfEditor::AddPage(int nPageIndex) pWriter->put_Height(dHeight); return true; } +bool CPdfEditor::MovePage(int nPageIndex, int nPos) +{ + if (EditPage(nPageIndex)) + { + m_nEditPage = nPos; + return pWriter->GetDocument()->MovePage(nPageIndex, nPos); + } + return false; +} bool CPdfEditor::EditAnnot(int nPageIndex, int nID) { PDFDoc* pPDFDocument = pReader->GetPDFDocument(); diff --git a/PdfFile/PdfEditor.h b/PdfFile/PdfEditor.h index 61e3b15513..d5439c8644 100644 --- a/PdfFile/PdfEditor.h +++ b/PdfFile/PdfEditor.h @@ -44,9 +44,10 @@ public: int GetError(); void Close(); - bool EditPage(int nPageIndex, bool bSet = true); + bool EditPage(int nPageIndex, bool bSet = true, int nNewPage = -1); bool DeletePage(int nPageIndex); bool AddPage(int nPageIndex); + bool MovePage(int nPageIndex, int nPos); bool EditAnnot(int nPageIndex, int nID); bool DeleteAnnot(int nID, Object* oAnnots = NULL); bool EditWidgets(IAdvancedCommand* pCommand); diff --git a/PdfFile/PdfFile.cpp b/PdfFile/PdfFile.cpp index bede929870..25b9164474 100644 --- a/PdfFile/PdfFile.cpp +++ b/PdfFile/PdfFile.cpp @@ -165,6 +165,12 @@ bool CPdfFile::AddPage(int nPageIndex) return false; return m_pInternal->pEditor->AddPage(nPageIndex); } +bool CPdfFile::MovePage(int nPageIndex, int nPos) +{ + if (!m_pInternal->pEditor) + return false; + return m_pInternal->pEditor->MovePage(nPageIndex, nPos); +} HRESULT CPdfFile::ChangePassword(const std::wstring& wsPath, const std::wstring& wsPassword) { RELEASEOBJECT(m_pInternal->pWriter); diff --git a/PdfFile/PdfFile.h b/PdfFile/PdfFile.h index dc64c231df..d055c338c6 100644 --- a/PdfFile/PdfFile.h +++ b/PdfFile/PdfFile.h @@ -97,6 +97,7 @@ public: bool EditPage (int nPageIndex); bool DeletePage (int nPageIndex); bool AddPage (int nPageIndex); + bool MovePage (int nPageIndex, int nPos); HRESULT ChangePassword(const std::wstring& wsPath, const std::wstring& wsPassword = L""); #endif diff --git a/PdfFile/SrcWriter/Document.cpp b/PdfFile/SrcWriter/Document.cpp index 237e6edf64..120205ebc3 100644 --- a/PdfFile/SrcWriter/Document.cpp +++ b/PdfFile/SrcWriter/Document.cpp @@ -1442,7 +1442,7 @@ namespace PdfWriter return pRes; } - bool CDocument::EditPage(CXref* pXref, CPage* pPage, int nPageIndex) + bool CDocument::EditPage(CXref* pXref, CPage* pPage, int nPageIndex, int nNewPage) { if (!pPage || !EditXref(pXref)) return false; @@ -1454,7 +1454,10 @@ namespace PdfWriter #endif m_pCurPage = pPage; - m_mEditPages[nPageIndex] = pPage; + if (nNewPage > 0) + m_mEditPages[nNewPage] = pPage; + else + m_mEditPages[nPageIndex] = pPage; if (m_pPageTree) m_pPageTree->ReplacePage(nPageIndex, pPage); @@ -1589,6 +1592,16 @@ namespace PdfWriter } return false; } + bool CDocument::MovePage(int nPageIndex, int nPos) + { + if (m_pPageTree) + { + CObjectBase* pObj = m_pPageTree->RemovePage(nPageIndex); + if (pObj->GetType() == object_type_DICT && ((CDictObject*)pObj)->GetDictType() == dict_type_PAGE) + return m_pPageTree->InsertPage(nPageIndex, (CPage*)pObj); + } + return false; + } bool CDocument::AddToFile(CXref* pXref, CDictObject* pTrailer, CXref* pInfoXref, CInfoDict* pInfo) { if (!pTrailer || m_wsFilePath.empty()) diff --git a/PdfFile/SrcWriter/Document.h b/PdfFile/SrcWriter/Document.h index 6e5fc1f065..af1888598f 100644 --- a/PdfFile/SrcWriter/Document.h +++ b/PdfFile/SrcWriter/Document.h @@ -191,9 +191,10 @@ namespace PdfWriter bool EditPdf(const std::wstring& wsPath, int nPosLastXRef, int nSizeXRef, CXref* pXref, CCatalog* pCatalog, CEncryptDict* pEncrypt, int nFormField); bool EditResources(CXref* pXref, CResourcesDict* pResources); std::pair GetPageRef(int nPageIndex); - bool EditPage(CXref* pXref, CPage* pPage, int nPageIndex); + bool EditPage(CXref* pXref, CPage* pPage, int nPageIndex, int nNewPage = -1); CPage* AddPage(int nPageIndex); bool DeletePage(int nPageIndex); + bool MovePage(int nPageIndex, int nPos); bool AddToFile(CXref* pXref, CDictObject* pTrailer, CXref* pInfoXref, CInfoDict* pInfo); void Sign(const TRect& oRect, CImageDict* pImage, ICertificate* pCert); std::wstring GetEditPdfPath() { return m_wsFilePath; }