From cb1c31ba3d0fdc3c3cbd92a318ade0f2301f598e Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Sat, 22 Oct 2022 17:04:47 +0200 Subject: [PATCH] Issue #27: Signature plugin - inserting bitmap images does not work --- Pdf4QtLib/sources/pdfdocumentbuilder.cpp | 37 ++++++++++++++++++++++-- Pdf4QtLib/sources/pdfdocumentbuilder.h | 2 ++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Pdf4QtLib/sources/pdfdocumentbuilder.cpp b/Pdf4QtLib/sources/pdfdocumentbuilder.cpp index 1908d8a..6ff160f 100644 --- a/Pdf4QtLib/sources/pdfdocumentbuilder.cpp +++ b/Pdf4QtLib/sources/pdfdocumentbuilder.cpp @@ -821,9 +821,10 @@ void PDFPageContentStreamBuilder::end(QPainter* painter) } } - PDFObject oldResourcesObject = pageDictionary->get("Resources"); + PDFObject oldResourcesObject = m_documentBuilder->getObject(pageDictionary->get("Resources")); + oldResourcesObject = removeDictionaryReferencesFromResources(oldResourcesObject); replaceResources(contentsReference, resourcesReference, oldResourcesObject); - m_documentBuilder->mergeTo(resourcesReference, m_documentBuilder->getObject(oldResourcesObject)); + m_documentBuilder->mergeTo(resourcesReference, oldResourcesObject); } switch (m_mode) @@ -859,6 +860,38 @@ void PDFPageContentStreamBuilder::end(QPainter* painter) } } +PDFObject PDFPageContentStreamBuilder::removeDictionaryReferencesFromResources(PDFObject resources) +{ + PDFObjectFactory resourcesBuilder; + + resources = m_documentBuilder->getObject(resources); + if (resources.isDictionary()) + { + resourcesBuilder.beginDictionary(); + + const PDFDictionary* resourcesDictionary = resources.getDictionary(); + const size_t count = resourcesDictionary->getCount(); + for (size_t i = 0; i < count; ++i) + { + PDFObject object = m_documentBuilder->getObject(resourcesDictionary->getValue(i)); + + if (object.isNull()) + { + continue; + } + + resourcesBuilder.beginDictionaryItem(resourcesDictionary->getKey(i).getString()); + resourcesBuilder << object; + resourcesBuilder.endDictionaryItem(); + } + + resourcesBuilder.endDictionary(); + resources = resourcesBuilder.takeObject(); + } + + return resources; +} + void PDFPageContentStreamBuilder::replaceResources(PDFObjectReference contentStreamReference, PDFObjectReference resourcesReference, PDFObject oldResources) diff --git a/Pdf4QtLib/sources/pdfdocumentbuilder.h b/Pdf4QtLib/sources/pdfdocumentbuilder.h index a7fe950..b7f33fc 100644 --- a/Pdf4QtLib/sources/pdfdocumentbuilder.h +++ b/Pdf4QtLib/sources/pdfdocumentbuilder.h @@ -301,6 +301,8 @@ public: void end(QPainter* painter); private: + PDFObject removeDictionaryReferencesFromResources(PDFObject resources); + void replaceResources(PDFObjectReference contentStreamReference, PDFObjectReference resourcesReference, PDFObject oldResources);