diff --git a/include/qpdf/QPDFObjectHandle.hh b/include/qpdf/QPDFObjectHandle.hh index 3e4f633a..9873da7b 100644 --- a/include/qpdf/QPDFObjectHandle.hh +++ b/include/qpdf/QPDFObjectHandle.hh @@ -172,6 +172,8 @@ class QPDFObjectHandle int getArrayNItems(); QPDF_DLL QPDFObjectHandle getArrayItem(int n); + QPDF_DLL + std::vector getArrayAsVector(); // Methods for dictionary objects QPDF_DLL @@ -180,6 +182,8 @@ class QPDFObjectHandle QPDFObjectHandle getKey(std::string const&); QPDF_DLL std::set getKeys(); + QPDF_DLL + std::map getDictAsMap(); // Methods for name and array objects QPDF_DLL diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index b877f3dd..8567eef9 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -246,6 +246,13 @@ QPDFObjectHandle::getArrayItem(int n) return dynamic_cast(obj.getPointer())->getItem(n); } +std::vector +QPDFObjectHandle::getArrayAsVector() +{ + assertType("Array", isArray()); + return dynamic_cast(obj.getPointer())->getAsVector(); +} + // Array mutators void @@ -278,6 +285,13 @@ QPDFObjectHandle::getKeys() return dynamic_cast(obj.getPointer())->getKeys(); } +std::map +QPDFObjectHandle::getDictAsMap() +{ + assertType("Dictionary", isDictionary()); + return dynamic_cast(obj.getPointer())->getAsMap(); +} + // Array and Name accessors bool QPDFObjectHandle::isOrHasName(std::string const& value) diff --git a/libqpdf/QPDF_Array.cc b/libqpdf/QPDF_Array.cc index ae70bd67..ab61f307 100644 --- a/libqpdf/QPDF_Array.cc +++ b/libqpdf/QPDF_Array.cc @@ -51,6 +51,12 @@ QPDF_Array::getItem(int n) const return this->items[n]; } +std::vector const& +QPDF_Array::getAsVector() const +{ + return this->items; +} + void QPDF_Array::setItem(int n, QPDFObjectHandle const& oh) { diff --git a/libqpdf/QPDF_Dictionary.cc b/libqpdf/QPDF_Dictionary.cc index 838a37e6..3be138a4 100644 --- a/libqpdf/QPDF_Dictionary.cc +++ b/libqpdf/QPDF_Dictionary.cc @@ -78,6 +78,13 @@ QPDF_Dictionary::getKeys() return result; } +std::map const& +QPDF_Dictionary::getAsMap() const +{ + + return this->items; +} + void QPDF_Dictionary::replaceKey(std::string const& key, QPDFObjectHandle const& value) diff --git a/libqpdf/qpdf/QPDF_Array.hh b/libqpdf/qpdf/QPDF_Array.hh index 490df723..a57834c8 100644 --- a/libqpdf/qpdf/QPDF_Array.hh +++ b/libqpdf/qpdf/QPDF_Array.hh @@ -14,6 +14,7 @@ class QPDF_Array: public QPDFObject virtual std::string unparse(); int getNItems() const; QPDFObjectHandle getItem(int n) const; + std::vector const& getAsVector() const; void setItem(int, QPDFObjectHandle const&); protected: diff --git a/libqpdf/qpdf/QPDF_Dictionary.hh b/libqpdf/qpdf/QPDF_Dictionary.hh index e84b549e..af9f7f09 100644 --- a/libqpdf/qpdf/QPDF_Dictionary.hh +++ b/libqpdf/qpdf/QPDF_Dictionary.hh @@ -21,6 +21,7 @@ class QPDF_Dictionary: public QPDFObject bool hasKey(std::string const&); QPDFObjectHandle getKey(std::string const&); std::set getKeys(); + std::map const& getAsMap() const; // Replace value of key, adding it if it does not exist void replaceKey(std::string const& key, QPDFObjectHandle const&); diff --git a/qpdf/qtest/qpdf/test14.out b/qpdf/qtest/qpdf/test14.out index 5b68d9f6..65b640b9 100644 --- a/qpdf/qtest/qpdf/test14.out +++ b/qpdf/qtest/qpdf/test14.out @@ -3,4 +3,5 @@ old dict: 1 old dict: 1 new dict: 2 swapped array: /Array +array and dictionary contents are correct test 14 done diff --git a/qpdf/test_driver.cc b/qpdf/test_driver.cc index 17451dc7..d551d6f2 100644 --- a/qpdf/test_driver.cc +++ b/qpdf/test_driver.cc @@ -570,6 +570,20 @@ void runtest(int n, char const* filename) std::cout << "swapped array: " << qdict.getArrayItem(0).getName() << std::endl; + // Exercise getAsMap and getAsArray + std::vector array_elements = + qdict.getArrayAsVector(); + std::map dict_items = + qarray.getDictAsMap(); + if ((array_elements.size() == 1) && + (array_elements[0].getName() == "/Array") && + (dict_items.size() == 1) && + (dict_items["/NewDict"].getIntValue() == 2)) + { + std::cout << "array and dictionary contents are correct" + << std::endl; + } + QPDFWriter w(pdf, "a.pdf"); w.setStaticID(true); w.setStreamDataMode(qpdf_s_preserve);