diff --git a/DesktopEditor/graphics/commands/AnnotField.cpp b/DesktopEditor/graphics/commands/AnnotField.cpp index 7c4fb94a5f..db166795e3 100644 --- a/DesktopEditor/graphics/commands/AnnotField.cpp +++ b/DesktopEditor/graphics/commands/AnnotField.cpp @@ -897,6 +897,7 @@ CAnnotFieldInfo::CWidgetAnnotPr::CButtonWidgetPr* CAnnotFieldInfo::CWidgetAnn CAnnotFieldInfo::CWidgetAnnotPr::CTextWidgetPr* CAnnotFieldInfo::CWidgetAnnotPr::GetTextWidgetPr() { return m_pTextPr; } CAnnotFieldInfo::CWidgetAnnotPr::CChoiceWidgetPr* CAnnotFieldInfo::CWidgetAnnotPr::GetChoiceWidgetPr() { return m_pChoicePr; } CAnnotFieldInfo::CWidgetAnnotPr::CSignatureWidgetPr* CAnnotFieldInfo::CWidgetAnnotPr::GetSignatureWidgetPr() { return m_pSignaturePr; } +void CAnnotFieldInfo::CWidgetAnnotPr::SetFontName(const std::wstring& sName) { m_wsFN = sName; } CAnnotFieldInfo::CWidgetAnnotPr::CWidgetAnnotPr(BYTE nType) { m_pButtonPr = NULL; diff --git a/DesktopEditor/graphics/commands/AnnotField.h b/DesktopEditor/graphics/commands/AnnotField.h index ee74ea2d5f..2b333f030d 100644 --- a/DesktopEditor/graphics/commands/AnnotField.h +++ b/DesktopEditor/graphics/commands/AnnotField.h @@ -203,6 +203,8 @@ public: const std::vector& GetBG(); const std::vector& GetActions(); + void SetFontName(const std::wstring& sName); + CButtonWidgetPr* GetButtonWidgetPr(); CTextWidgetPr* GetTextWidgetPr(); CChoiceWidgetPr* GetChoiceWidgetPr(); diff --git a/PdfFile/PdfEditor.cpp b/PdfFile/PdfEditor.cpp index a8910860a9..4bbf45b357 100644 --- a/PdfFile/PdfEditor.cpp +++ b/PdfFile/PdfEditor.cpp @@ -1098,7 +1098,10 @@ bool CPdfEditor::IncrementalUpdates() return true; m_nMode = Mode::WriteAppend; - PDFDoc* pPDFDocument = m_pReader->GetPDFDocument(0); + PDFDoc* pPDFDocument = NULL; + PdfReader::CPdfFontList* pFontList = NULL; + int nStartRefID = 0; + m_pReader->GetPageIndex(0, &pPDFDocument, &pFontList, &nStartRefID); XRef* xref = pPDFDocument->getXRef(); PdfWriter::CDocument* pDoc = m_pWriter->GetDocument(); @@ -1330,14 +1333,28 @@ bool CPdfEditor::IncrementalUpdates() bRes = pDoc->EditResources(pDRXref, pDR); } pagesRefObj.free(); + + if (form) + { + Object oDR; + Object* oAcroForm = form->getAcroFormObj(); + if (oAcroForm->dictLookup("DR", &oDR)->isDict()) + { + Dict* pResources = oDR.getDict(); + ScanFonts(pPDFDocument, pResources); + } + oDR.free(); + } return bRes; } void CPdfEditor::NewFrom() { PdfWriter::CDocument* pDoc = m_pWriter->GetDocument(); Object oCatalog; - PDFDoc* pPDFDocument = m_pReader->GetPDFDocument(0); + PDFDoc* pPDFDocument = NULL; + PdfReader::CPdfFontList* pFontList = NULL; int nStartRefID = 0; + m_pReader->GetPageIndex(0, &pPDFDocument, &pFontList, &nStartRefID); XRef* xref = pPDFDocument->getXRef(); if (!xref->getCatalog(&oCatalog)->isDict()) { @@ -1564,8 +1581,12 @@ void CPdfEditor::NewFrom() } } } - oTemp2.free(); oTemp.free(); + oTemp2.free(); pDR->Fix(); + + Dict* pResources = oTemp.getDict(); + ScanFonts(pPDFDocument, pResources); + oTemp.free(); continue; } else @@ -2724,7 +2745,11 @@ bool CPdfEditor::SplitPages(const int* arrPageIndex, unsigned int unLength, PDFD } } } - oTemp2.free(); oTemp.free(); + oTemp2.free(); + + Dict* pResources = oTemp.getDict(); + ScanFonts(pPDFDocument, pResources); + oTemp.free(); continue; } else @@ -4189,12 +4214,7 @@ void CPdfEditor::ClearPage() // Mode::WriteAppend && Mode::WriteNew // Reading and processing fonts from page Dict* pResources = pOPage->getResourceDict(); - if (pResources) - { - std::vector arrUniqueResources; - ScanAndProcessFonts(pPDFDocument, xref, pResources, 0, arrUniqueResources, pFontList, nStartRefID); - m_pReader->SetFonts(m_nEditPage); - } + ScanFonts(pPDFDocument, pResources); oAnnots.free(); } void CPdfEditor::AddShapeXML(const std::string& sXML) @@ -4443,6 +4463,18 @@ std::vector CPdfEditor::WriteRedact(const std::vector& arr } return arrRes; } +void CPdfEditor::ScanFonts(PDFDoc* pPDFDocument, Dict* pResources) +{ + if (!pResources) + return; + XRef* xref = pPDFDocument->getXRef(); + int nStartRefID = m_pReader->GetStartRefID(pPDFDocument); + PdfReader::CPdfFontList* pFontList = m_pReader->GetFontList(pPDFDocument); + + std::vector arrUniqueResources; + ScanAndProcessFonts(pPDFDocument, xref, pResources, 0, arrUniqueResources, pFontList, nStartRefID); + m_pReader->SetFonts(pFontList); +} void CPdfEditor::ScanAndProcessFonts(PDFDoc* pPDFDocument, XRef* xref, Dict* pResources, int nDepth, std::vector& arrUniqueResources, PdfReader::CPdfFontList* pFontList, int nStartRefID) { if (nDepth > 5 || !pResources) diff --git a/PdfFile/PdfEditor.h b/PdfFile/PdfEditor.h index a582ca7a8b..d3e99343bb 100644 --- a/PdfFile/PdfEditor.h +++ b/PdfFile/PdfEditor.h @@ -116,6 +116,7 @@ private: bool SplitPages(const int* arrPageIndex, unsigned int unLength, PDFDoc* _pDoc, int nStartRefID); bool ChangeFullNameParent(int nParent, const std::string& sPrefixForm, std::vector& arrRename); void ScanAndProcessFonts(PDFDoc* pPDFDocument, XRef* xref, Dict* pResources, int nDepth, std::vector& arrUniqueResources, PdfReader::CPdfFontList* pFontList, int nStartRefID); + void ScanFonts(PDFDoc* pPDFDocument, Dict* pResources); struct CRedactData { diff --git a/PdfFile/PdfFile.cpp b/PdfFile/PdfFile.cpp index 257eecd0ff..5eb1d254c6 100644 --- a/PdfFile/PdfFile.cpp +++ b/PdfFile/PdfFile.cpp @@ -508,7 +508,12 @@ BYTE* CPdfFile::GetWidgets() void CPdfFile::SetPageFonts(int nPageIndex) { if (m_pInternal->pReader) - m_pInternal->pReader->SetFonts(nPageIndex); + { + PDFDoc* pDoc = NULL; + PdfReader::CPdfFontList* pFontList = NULL; + m_pInternal->pReader->GetPageIndex(nPageIndex, &pDoc, &pFontList); + m_pInternal->pReader->SetFonts(pFontList); + } } BYTE* CPdfFile::GetAnnotEmbeddedFonts() { @@ -1444,6 +1449,34 @@ HRESULT CPdfFile::AdvancedCommand(IAdvancedCommand* command) if (nFlags & (1 << 15)) m_pInternal->pEditor->EditAnnot(pCommand->GetPage(), pCommand->GetCopyAP()); } + if (pCommand->IsWidget()) + { + CAnnotFieldInfo::CWidgetAnnotPr* pPr = pCommand->GetWidgetAnnotPr(); + std::wstring wsFontName = pPr->GetFontName(); + + size_t lastSpace = wsFontName.find_last_of(L' '); + if (lastSpace != std::wstring::npos && lastSpace + 1 == wsFontName.size() - 32) + { + std::wstring sHash = wsFontName.substr(lastSpace + 1); + bool bValidHash = true; + for (wchar_t c : sHash) + { + if (!((c >= L'A' && c <= L'F') || (c >= L'0' && c <= L'9'))) + { + bValidHash = false; + break; + } + } + + if (bValidHash) + { + std::wstring sName = wsFontName.substr(0, lastSpace); + wsFontName = sName; + } + } + + pPr->SetFontName(wsFontName); + } } return m_pInternal->pWriter->AddAnnotField(m_pInternal->pAppFonts, pCommand); } diff --git a/PdfFile/PdfReader.cpp b/PdfFile/PdfReader.cpp index bde27114a6..8d1cf6d3d1 100644 --- a/PdfFile/PdfReader.cpp +++ b/PdfFile/PdfReader.cpp @@ -245,6 +245,18 @@ int CPdfReader::GetNumPagesBefore(PDFDoc* _pDoc) } return -1; } +PdfReader::CPdfFontList* CPdfReader::GetFontList(PDFDoc* _pDoc) +{ + for (CPdfReaderContext* pPDFContext : m_vPDFContext) + { + if (!pPDFContext || !pPDFContext->m_pDocument) + continue; + PDFDoc* pDoc = pPDFContext->m_pDocument; + if (_pDoc == pDoc) + return pPDFContext->m_pFontList; + } + return NULL; +} std::string CPdfReader::GetPrefixForm(PDFDoc* _pDoc) { for (CPdfReaderContext* pPDFContext : m_vPDFContext) @@ -1299,15 +1311,11 @@ BYTE* CPdfReader::GetWidgets() oRes.ClearWithoutAttack(); return bRes; } -void CPdfReader::SetFonts(int _nPageIndex) +void CPdfReader::SetFonts(PdfReader::CPdfFontList* pFontList) { if (m_vPDFContext.empty()) return; - PDFDoc* pDoc = NULL; - PdfReader::CPdfFontList* pFontList = NULL; - GetPageIndex(_nPageIndex, &pDoc, &pFontList); - const std::map& mapFonts = pFontList->GetFonts(); for (std::map::const_iterator it = mapFonts.begin(); it != mapFonts.end(); ++it) { diff --git a/PdfFile/PdfReader.h b/PdfFile/PdfReader.h index 81cce2ce9f..28bb452270 100644 --- a/PdfFile/PdfReader.h +++ b/PdfFile/PdfReader.h @@ -110,11 +110,12 @@ public: PDFDoc* GetPDFDocument(int PDFIndex); int GetStartRefID(PDFDoc* pDoc); int GetNumPagesBefore(PDFDoc* pDoc); + PdfReader::CPdfFontList* GetFontList(PDFDoc* pDoc); std::string GetPrefixForm(PDFDoc* pDoc); int FindRefNum(int nObjID, PDFDoc** pDoc = NULL, int* nStartRefID = NULL); int GetPageIndex(int nPageIndex, PDFDoc** pDoc = NULL, PdfReader::CPdfFontList** pFontList = NULL, int* nStartRefID = NULL); - void SetFonts(int nPageIndex); + void SetFonts(PdfReader::CPdfFontList* pFontList); BYTE* GetStructure(); BYTE* GetLinks(int nPageIndex); BYTE* GetWidgets();