diff --git a/DesktopEditor/graphics/pro/js/wasm/src/pdfwriter.cpp b/DesktopEditor/graphics/pro/js/wasm/src/pdfwriter.cpp index dcefa78387..8aca52fb2c 100644 --- a/DesktopEditor/graphics/pro/js/wasm/src/pdfwriter.cpp +++ b/DesktopEditor/graphics/pro/js/wasm/src/pdfwriter.cpp @@ -143,7 +143,6 @@ HRESULT CPdfWriter::DrawImageWith1bppMask(IGrObject* pImage, NSImages::CPixJbig2 bool CPdfWriter::EditPage(PdfWriter::CPage* pNewPage) { return false; } bool CPdfWriter::AddPage(int nPageIndex) { return false; } bool CPdfWriter::EditClose() { return false; } -void CPdfWriter::PageClear() {} void CPdfWriter::PageRotate(int nRotate) {} void CPdfWriter::Sign(const double& dX, const double& dY, const double& dW, const double& dH, const std::wstring& wsPicturePath, ICertificate* pCertificate) {} HRESULT CPdfWriter::EditWidgetParents(NSFonts::IApplicationFonts* pAppFonts, CWidgetsInfo* pFieldInfo, const std::wstring& wsTempDirectory) { return 0; } diff --git a/PdfFile/PdfEditor.cpp b/PdfFile/PdfEditor.cpp index 8bc470e8a3..ec49c35450 100644 --- a/PdfFile/PdfEditor.cpp +++ b/PdfFile/PdfEditor.cpp @@ -469,7 +469,7 @@ CPdfEditor::CPdfEditor(const std::wstring& _wsSrcFile, const std::wstring& _wsPa wsPassword = _wsPassword; pReader = _pReader; pWriter = _pWriter; - bEditPage = false; + m_nEditPage = -1; nError = 0; PDFDoc* pPDFDocument = pReader->GetPDFDocument(); @@ -812,7 +812,7 @@ void CPdfEditor::Close() pReader = NULL; pWriter = NULL; - bEditPage = false; + m_nEditPage = -1; } int CPdfEditor::GetError() { @@ -958,20 +958,13 @@ bool CPdfEditor::EditPage(int nPageIndex) } else if (strcmp("Annots", chKey) == 0) { - // ВРЕМЕНО удаление Link аннотаций при редактировании if (pageObj.dictGetVal(nIndex, &oTemp)->isArray()) { PdfWriter::CArrayObject* pArray = new PdfWriter::CArrayObject(); pPage->Add("Annots", pArray); for (int nIndex = 0; nIndex < oTemp.arrayGetLength(); ++nIndex) { - Object oAnnot, oSubtype; - if (oTemp.arrayGet(nIndex, &oAnnot)->isDict("Annot") && oAnnot.dictLookup("Subtype", &oSubtype)->isName("Link")) - { - oAnnot.free(); oSubtype.free(); - continue; - } - oAnnot.free(); oSubtype.free(); + Object oAnnot; oTemp.arrayGetNF(nIndex, &oAnnot); DictToCDictObject(&oAnnot, pArray, false, ""); oAnnot.free(); @@ -1012,7 +1005,7 @@ bool CPdfEditor::EditPage(int nPageIndex) // Применение редактирования страницы для writer if (pWriter->EditPage(pPage) && pDoc->EditPage(pXref, pPage, nPageIndex)) { - bEditPage = true; + m_nEditPage = nPageIndex; pPage->StartTransform(dCTM[0], dCTM[1], dCTM[2], dCTM[3], dCTM[4], dCTM[5]); return true; } @@ -1261,7 +1254,7 @@ bool CPdfEditor::EditAnnot(int nPageIndex, int nID) RELEASEOBJECT(pXref); return false; } -bool CPdfEditor::DeleteAnnot(int nID) +bool CPdfEditor::DeleteAnnot(int nID, Object* oAnnots) { PDFDoc* pPDFDocument = pReader->GetPDFDocument(); PdfWriter::CDocument* pDoc = pWriter->GetDocument(); @@ -1269,27 +1262,33 @@ bool CPdfEditor::DeleteAnnot(int nID) return false; XRef* xref = pPDFDocument->getXRef(); - std::pair pPageRef; - pPageRef.first = pDoc->GetCurPage()->GetObjId(); - pPageRef.second = pDoc->GetCurPage()->GetGenNo(); - if (!xref || pPageRef.first == 0) - return false; - - // Получение объекта аннотации - Object pageRefObj, pageObj, oAnnots; - pageRefObj.initRef(pPageRef.first, pPageRef.second); - if (!pageRefObj.fetch(xref, &pageObj)->isDict() || !pageObj.dictLookup("Annots", &oAnnots)->isArray()) + bool bClear = false; + if (!oAnnots) { - pageRefObj.free(); pageObj.free(); oAnnots.free(); - return false; + std::pair pPageRef = pDoc->GetPageRef(m_nEditPage); + if (pPageRef.first == 0) + return false; + + oAnnots = new Object(); + bClear = true; + + // Получение объекта аннотации + Object pageRefObj, pageObj; + pageRefObj.initRef(pPageRef.first, pPageRef.second); + if (!pageRefObj.fetch(xref, &pageObj)->isDict() || !pageObj.dictLookup("Annots", oAnnots)->isArray()) + { + pageRefObj.free(); pageObj.free(); oAnnots->free(); + RELEASEOBJECT(oAnnots); + return false; + } + pageRefObj.free(); pageObj.free(); } - pageRefObj.free(); pageObj.free(); bool bRes = false; - for (int i = 0; i < oAnnots.arrayGetLength(); ++i) + for (int i = 0; i < oAnnots->arrayGetLength(); ++i) { Object oAnnotRef, oAnnot; - if (oAnnots.arrayGetNF(i, &oAnnotRef)->isRef() && oAnnotRef.getRefNum() == nID) + if (oAnnots->arrayGetNF(i, &oAnnotRef)->isRef() && oAnnotRef.getRefNum() == nID) { bRes = pDoc->DeleteAnnot(oAnnotRef.getRefNum(), oAnnotRef.getRefGen()); if (oAnnotRef.fetch(xref, &oAnnot)->isDict()) @@ -1300,16 +1299,21 @@ bool CPdfEditor::DeleteAnnot(int nID) oPopupRef.free(); } } - else if (oAnnots.arrayGet(i, &oAnnot)->isDict()) + else if (oAnnots->arrayGet(i, &oAnnot)->isDict()) { Object oIRTRef; if (oAnnot.dictLookupNF("IRT", &oIRTRef)->isRef() && oIRTRef.getRefNum() == nID) - DeleteAnnot(oAnnotRef.getRefNum()); + DeleteAnnot(oAnnotRef.getRefNum(), oAnnots); oIRTRef.free(); } oAnnotRef.free(); oAnnot.free(); } - oAnnots.free(); + + if (bClear) + { + oAnnots->free(); + RELEASEOBJECT(oAnnots); + } return bRes; } @@ -1369,7 +1373,43 @@ int CPdfEditor::GetRotate(int nPageIndex) } bool CPdfEditor::IsEditPage() { - return bEditPage; + return m_nEditPage >= 0; +} +void CPdfEditor::ClearPage() +{ + PDFDoc* pPDFDocument = pReader->GetPDFDocument(); + XRef* xref = pPDFDocument->getXRef(); + PdfWriter::CDocument* pDoc = pWriter->GetDocument(); + std::pair pPageRef = pDoc->GetPageRef(m_nEditPage); + + // Получение объекта страницы + Object pageRefObj, pageObj; + pageRefObj.initRef(pPageRef.first, pPageRef.second); + if (!pageRefObj.fetch(xref, &pageObj)->isDict()) + { + pageObj.free(); pageRefObj.free(); + return; + } + pageRefObj.free(); + + Object oAnnots; + // ВРЕМЕННО удаление Link аннотаций при редактировании + if (pageObj.dictLookup("Annots", &oAnnots)->isArray()) + { + for (int nIndex = 0; nIndex < oAnnots.arrayGetLength(); ++nIndex) + { + Object oAnnot, oSubtype, oAnnotRef; + if (oAnnots.arrayGet(nIndex, &oAnnot)->isDict("Annot") && oAnnot.dictLookup("Subtype", &oSubtype)->isName("Link")) + { + oAnnots.arrayGetNF(nIndex, &oAnnotRef); + DeleteAnnot(oAnnotRef.getRefNum(), &oAnnots); + } + oAnnot.free(); oSubtype.free(); oAnnotRef.free(); + } + } + pageObj.free(); + + pDoc->ClearPage(); } void CPdfEditor::AddShapeXML(const std::string& sXML) { diff --git a/PdfFile/PdfEditor.h b/PdfFile/PdfEditor.h index 8331d377c0..c87f7df185 100644 --- a/PdfFile/PdfEditor.h +++ b/PdfFile/PdfEditor.h @@ -48,12 +48,13 @@ public: bool DeletePage(int nPageIndex); bool AddPage(int nPageIndex); bool EditAnnot(int nPageIndex, int nID); - bool DeleteAnnot(int nID); + bool DeleteAnnot(int nID, Object* oAnnots = NULL); bool EditWidgets(IAdvancedCommand* pCommand); int GetPagesCount(); void GetPageInfo(int nPageIndex, double* pdWidth, double* pdHeight, double* pdDpiX, double* pdDpiY); int GetRotate(int nPageIndex); bool IsEditPage(); + void ClearPage(); void AddShapeXML(const std::string& sXML); void EndMarkedContent(); bool IsBase14(const std::wstring& wsFontName, bool& bBold, bool& bItalic, std::wstring& wsFontPath); @@ -69,7 +70,7 @@ private: CPdfWriter* pWriter; int nError; - bool bEditPage; + int m_nEditPage; }; #endif // _PDF_EDITOR_H diff --git a/PdfFile/PdfFile.cpp b/PdfFile/PdfFile.cpp index de42f8f7e5..2a9a375c45 100644 --- a/PdfFile/PdfFile.cpp +++ b/PdfFile/PdfFile.cpp @@ -53,12 +53,13 @@ public: bool DeletePage(int nPageIndex) { return false; } bool AddPage(int nPageIndex) { return false; } bool EditAnnot(int nPageIndex, int nID) { return false; } - bool DeleteAnnot(int nID) { return false; } + bool DeleteAnnot(int nID, Object* oAnnots = NULL) { return false; } bool EditWidgets(IAdvancedCommand* pCommand) { return false; } int GetPagesCount() { return 0; } void GetPageInfo(int nPageIndex, double* pdWidth, double* pdHeight, double* pdDpiX, double* pdDpiY) {} int GetRotate(int nPageIndex) { return 0; } bool IsEditPage() { return false; } + void ClearPage() {} void AddShapeXML(const std::string& sXML) {} void EndMarkedContent() {} bool IsBase14(const std::wstring& wsFontName, bool& bBold, bool& bItalic, std::wstring& wsFontPath) { return false; } @@ -1257,7 +1258,7 @@ HRESULT CPdfFile::AdvancedCommand(IAdvancedCommand* command) case IAdvancedCommand::AdvancedCommandType::PageClear: { if (m_pInternal->pEditor && m_pInternal->pEditor->IsEditPage()) - m_pInternal->pWriter->PageClear(); + m_pInternal->pEditor->ClearPage(); return S_OK; } case IAdvancedCommand::AdvancedCommandType::PageRotate: diff --git a/PdfFile/PdfWriter.cpp b/PdfFile/PdfWriter.cpp index ff6056a543..7a207d7765 100644 --- a/PdfFile/PdfWriter.cpp +++ b/PdfFile/PdfWriter.cpp @@ -2750,10 +2750,6 @@ bool CPdfWriter::EditClose() return true; } -void CPdfWriter::PageClear() -{ - m_pDocument->ClearPage(); -} void CPdfWriter::PageRotate(int nRotate) { if (m_pPage) diff --git a/PdfFile/PdfWriter.h b/PdfFile/PdfWriter.h index 8cbe0301f7..d8113852ca 100644 --- a/PdfFile/PdfWriter.h +++ b/PdfFile/PdfWriter.h @@ -211,7 +211,6 @@ public: bool EditPage(PdfWriter::CPage* pNewPage); bool AddPage(int nPageIndex); bool EditClose(); - void PageClear(); void PageRotate(int nRotate); void Sign(const double& dX, const double& dY, const double& dW, const double& dH, const std::wstring& wsPicturePath, ICertificate* pCertificate); HRESULT EditWidgetParents(NSFonts::IApplicationFonts* pAppFonts, CWidgetsInfo* pFieldInfo, const std::wstring& wsTempDirectory);