From 0ec39e3d3228924fee1a924a04e366ff8b4d04e9 Mon Sep 17 00:00:00 2001 From: Svetlana Kulikova Date: Tue, 28 Nov 2023 16:04:04 +0300 Subject: [PATCH] Write widget's parent --- .../graphics/MetafileToRendererReader.cpp | 1 + DesktopEditor/graphics/commands/AnnotField.h | 7 ++-- PdfFile/OnlineOfficeBinToPdf.cpp | 16 +++++++--- PdfFile/PdfFile.cpp | 9 +++++- PdfFile/PdfFile.h | 1 + PdfFile/PdfWriter.cpp | 32 +++++++++++++++++++ PdfFile/PdfWriter.h | 1 + PdfFile/SrcReader/RendererOutputDev.cpp | 18 ++++++++--- PdfFile/SrcReader/RendererOutputDev.h | 1 + PdfFile/SrcWriter/Document.cpp | 19 +++++++++++ PdfFile/SrcWriter/Document.h | 1 + 11 files changed, 93 insertions(+), 13 deletions(-) diff --git a/DesktopEditor/graphics/MetafileToRendererReader.cpp b/DesktopEditor/graphics/MetafileToRendererReader.cpp index e3532e4135..d40d102989 100644 --- a/DesktopEditor/graphics/MetafileToRendererReader.cpp +++ b/DesktopEditor/graphics/MetafileToRendererReader.cpp @@ -57,6 +57,7 @@ namespace NSOnlineOfficeBinToPdf case ctAnnotField: return Read_Command (this, pCorrector); case ctFormField: return Read_Command (this, pCorrector); case ctAnnotFieldDelete: return Read_Command(this, pCorrector); + case ctWidgetsInfo: return Read_Command (this, pCorrector); default: break; } diff --git a/DesktopEditor/graphics/commands/AnnotField.h b/DesktopEditor/graphics/commands/AnnotField.h index 1a312348f5..9faf1337d3 100644 --- a/DesktopEditor/graphics/commands/AnnotField.h +++ b/DesktopEditor/graphics/commands/AnnotField.h @@ -478,7 +478,7 @@ private: class GRAPHICS_DECL CWidgetsInfo : public IAdvancedCommand { -private: +public: struct CParent { int nID; @@ -488,10 +488,13 @@ private: std::wstring sV; std::wstring sDV; }; -public: + CWidgetsInfo(); virtual ~CWidgetsInfo(); + const std::vector& GetCO() const { return m_arrCO; } + const std::vector& GetParents() const { return m_arrParents; } + bool Read(NSOnlineOfficeBinToPdf::CBufferReader* pReader, IMetafileToRenderter* pCorrector); private: diff --git a/PdfFile/OnlineOfficeBinToPdf.cpp b/PdfFile/OnlineOfficeBinToPdf.cpp index 52f828f1f9..326ade4719 100644 --- a/PdfFile/OnlineOfficeBinToPdf.cpp +++ b/PdfFile/OnlineOfficeBinToPdf.cpp @@ -146,9 +146,10 @@ namespace NSOnlineOfficeBinToPdf enum class AddCommandType { - EditPage = 0, // Annotation + EditPage = 0, // ранее Annotation AddPage = 1, RemovePage = 2, + WidgetInfo = 3, Undefined = 255 }; @@ -172,13 +173,12 @@ namespace NSOnlineOfficeBinToPdf { int nLen = oReader.ReadInt(); AddCommandType CommandType = (AddCommandType)oReader.ReadByte(); - int nPageNum = oReader.ReadInt(); + int nPageNum = 0; + if (CommandType != AddCommandType::WidgetInfo) + nPageNum = oReader.ReadInt(); if (nPageNum < 0) - { - // ошибка в бинарнике return false; - } switch (CommandType) { @@ -196,6 +196,12 @@ namespace NSOnlineOfficeBinToPdf // TODO: version 7.6+ break; } + case AddCommandType::WidgetInfo: + { + NSOnlineOfficeBinToPdf::ConvertBufferToRenderer(oReader.GetCurrentBuffer(), (LONG)(nLen - 5) , &oCorrector); + oReader.Skip(nLen - 5); + break; + } default: return false; } diff --git a/PdfFile/PdfFile.cpp b/PdfFile/PdfFile.cpp index 5d84fe953d..c49b0b4740 100644 --- a/PdfFile/PdfFile.cpp +++ b/PdfFile/PdfFile.cpp @@ -1022,6 +1022,10 @@ bool CPdfFile::DeleteAnnot(int nID) return bRes; } +bool CPdfFile::EditWidgets() +{ + return true; +} #endif // BUILDING_WASM_MODULE // ------------------------------------------------------------------------ @@ -2079,7 +2083,10 @@ HRESULT CPdfFile::AdvancedCommand(IAdvancedCommand* command) case IAdvancedCommand::AdvancedCommandType::WidgetsInfo: { CWidgetsInfo* pCommand = (CWidgetsInfo*)command; - +#ifndef BUILDING_WASM_MODULE + if (m_pInternal->bEdit && EditWidgets()) + return m_pInternal->pWriter->EditWidgetParents(pCommand); +#endif return S_OK; } default: diff --git a/PdfFile/PdfFile.h b/PdfFile/PdfFile.h index feddd7c826..f2ccae8500 100644 --- a/PdfFile/PdfFile.h +++ b/PdfFile/PdfFile.h @@ -99,6 +99,7 @@ public: bool AddPage (int nPageIndex); bool EditAnnot (int nPageIndex, int nID); bool DeleteAnnot(int nID); + bool EditWidgets(); #endif // --- READER --- diff --git a/PdfFile/PdfWriter.cpp b/PdfFile/PdfWriter.cpp index a5518d4c72..fc5c27c9ee 100644 --- a/PdfFile/PdfWriter.cpp +++ b/PdfFile/PdfWriter.cpp @@ -2446,6 +2446,38 @@ HRESULT CPdfWriter::DrawImageWith1bppMask(IGrObject* pImage, NSImages::CPixJbig2 m_pPage->GrRestore(); return S_OK; } +HRESULT CPdfWriter::EditWidgetParents(CWidgetsInfo* pFieldInfo) +{ + if (!m_pDocument || 0 == m_pDocument->GetPagesCount() || !pFieldInfo) + return S_OK; + + if (!m_pDocument->EditCO(pFieldInfo->GetCO())) + return S_FALSE; + + std::vector arrParents = pFieldInfo->GetParents(); + for (CWidgetsInfo::CParent* pParent : arrParents) + { + PdfWriter::CDictObject* pParentObj = m_pDocument->GetParent(pParent->nID); + if (!pParentObj) + continue; + + int nFlags = pParent->nFlags; + if (nFlags & (1 << 0)) + pParentObj->Add("T", new PdfWriter::CStringObject((U_TO_UTF8(pParent->sName)).c_str())); + if (nFlags & (1 << 1)) + pParentObj->Add("V", new PdfWriter::CStringObject((U_TO_UTF8(pParent->sV)).c_str())); + if (nFlags & (1 << 2)) + pParentObj->Add("DV", new PdfWriter::CStringObject((U_TO_UTF8(pParent->sDV)).c_str())); + if (nFlags & (1 << 3)) + { + PdfWriter::CDictObject* pParentObj2 = m_pDocument->GetParent(pParent->nParentID); + if (pParentObj2) + pParentObj->Add("Parent", pParentObj2); + } + } + + return S_OK; +} bool CPdfWriter::EditPage(PdfWriter::CPage* pNewPage) { if (!IsValid()) diff --git a/PdfFile/PdfWriter.h b/PdfFile/PdfWriter.h index fd14005f59..1d4c213c1b 100644 --- a/PdfFile/PdfWriter.h +++ b/PdfFile/PdfWriter.h @@ -214,6 +214,7 @@ public: bool EditClose(); 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(CWidgetsInfo* pFieldInfo); PdfWriter::CDocument* m_pDocument; PdfWriter::CPage* m_pPage; diff --git a/PdfFile/SrcReader/RendererOutputDev.cpp b/PdfFile/SrcReader/RendererOutputDev.cpp index a3de60e047..1ff9505f44 100644 --- a/PdfFile/SrcReader/RendererOutputDev.cpp +++ b/PdfFile/SrcReader/RendererOutputDev.cpp @@ -70,7 +70,7 @@ #define OO_INLINE inline #endif -namespace NSCorrectFontName +namespace PdfReader { bool CheckFontNameStyle(std::wstring& sName, const std::wstring& sStyle) { @@ -97,7 +97,7 @@ namespace NSCorrectFontName return bRet; } - void CheckFontNamePDF(std::wstring& sName, NSFonts::CFontSelectFormat* format) + void CheckFontStylePDF(std::wstring& sName, bool& bBold, bool& bItalic) { if (sName.length() > 7 && sName.at(6) == '+') { @@ -117,8 +117,8 @@ namespace NSCorrectFontName } } - bool bBold = false; - bool bItalic = false; + bBold = false; + bItalic = false; CheckFontNameStyle(sName, L"condensedbold"); CheckFontNameStyle(sName, L"semibold"); @@ -149,6 +149,14 @@ namespace NSCorrectFontName //if (CheckFontNameStyle(sName, L"bolditalicmt")) { bBold = true; bItalic = true; } //if (CheckFontNameStyle(sName, L"bolditalic")) { bBold = true; bItalic = true; } //if (CheckFontNameStyle(sName, L"boldoblique")) { bBold = true; bItalic = true; } + } + + void CheckFontNamePDF(std::wstring& sName, NSFonts::CFontSelectFormat* format) + { + bool bBold = false; + bool bItalic = false; + + CheckFontStylePDF(sName, bBold, bItalic); if (format) { @@ -1273,7 +1281,7 @@ namespace PdfReader oRefObject.free(); NSFonts::CFontSelectFormat oFontSelect; - NSCorrectFontName::CheckFontNamePDF(wsFontBaseName, &oFontSelect); + CheckFontNamePDF(wsFontBaseName, &oFontSelect); if (oFontObject.isDict()) { Dict *pFontDict = oFontObject.getDict(); diff --git a/PdfFile/SrcReader/RendererOutputDev.h b/PdfFile/SrcReader/RendererOutputDev.h index 2e57daf431..da8a93e804 100644 --- a/PdfFile/SrcReader/RendererOutputDev.h +++ b/PdfFile/SrcReader/RendererOutputDev.h @@ -103,6 +103,7 @@ namespace PdfReader }; void GetFont(XRef* pXref, NSFonts::IFontManager* pFontManager, CFontList *pFontList, GfxFont* pFont, std::wstring& wsFileName, std::wstring& wsFontName); + void CheckFontStylePDF(std::wstring& sName, bool& bBold, bool& bItalic); //------------------------------------------------------------------------------------------------------------------------------- template inline static double PDFCoordsToMM(T tX) diff --git a/PdfFile/SrcWriter/Document.cpp b/PdfFile/SrcWriter/Document.cpp index 319592fd2a..666b10cf6e 100644 --- a/PdfFile/SrcWriter/Document.cpp +++ b/PdfFile/SrcWriter/Document.cpp @@ -1355,6 +1355,25 @@ namespace PdfWriter return p->second; return NULL; } + bool CDocument::EditCO(const std::vector& arrCO) + { + if (arrCO.empty() || !CheckAcroForm()) + return false; + + CArrayObject* pArray = new CArrayObject(); + if (!pArray) + return false; + + m_pAcroForm->Add("CO", pArray); + + for (const std::wstring& CO : arrCO) + { + std::string sCO = U_TO_UTF8(CO); + pArray->Add(new CStringObject(sCO.c_str())); + } + + return true; + } CPage* CDocument::AddPage(int nPageIndex) { if (!m_pPageTree) diff --git a/PdfFile/SrcWriter/Document.h b/PdfFile/SrcWriter/Document.h index 0f3b788348..ec68a45f6c 100644 --- a/PdfFile/SrcWriter/Document.h +++ b/PdfFile/SrcWriter/Document.h @@ -191,6 +191,7 @@ namespace PdfWriter CDictObject* GetParent(int nID); CPage* GetCurPage() { return m_pCurPage; } void SetCurPage(CPage* pPage) { m_pCurPage = pPage; } + bool EditCO(const std::vector& arrCO); private: char* GetTTFontTag();