diff --git a/include/qpdf/QPDF.hh b/include/qpdf/QPDF.hh index 2e24b261..b547fcbd 100644 --- a/include/qpdf/QPDF.hh +++ b/include/qpdf/QPDF.hh @@ -1079,6 +1079,7 @@ class QPDF std::shared_ptr resolve(int objid, int generation); void resolveObjectsInStream(int obj_stream_number); void stopOnError(std::string const& message); + QPDFObjectHandle reserveObjectIfNotExists(int objid, int gen); // Calls finish() on the pipeline when done but does not delete it bool pipeStreamData( diff --git a/include/qpdf/QPDFObjectHandle.hh b/include/qpdf/QPDFObjectHandle.hh index 70c7cf54..ec4a9699 100644 --- a/include/qpdf/QPDFObjectHandle.hh +++ b/include/qpdf/QPDFObjectHandle.hh @@ -1444,6 +1444,12 @@ class QPDFObjectHandle return QPDFObjectHandle::newStream( qpdf, objid, generation, stream_dict, offset, length); } + // Reserve an object with a specific ID + static QPDFObjectHandle + makeReserved() + { + return QPDFObjectHandle::makeReserved(); + } }; friend class Factory; @@ -1561,6 +1567,7 @@ class QPDFObjectHandle QPDFObjectHandle stream_dict, qpdf_offset_t offset, size_t length); + static QPDFObjectHandle makeReserved(); void typeWarning(char const* expected_type, std::string const& warning); void objectWarning(std::string const& warning); diff --git a/libqpdf/QPDF.cc b/libqpdf/QPDF.cc index 2658dd26..1807a838 100644 --- a/libqpdf/QPDF.cc +++ b/libqpdf/QPDF.cc @@ -2155,6 +2155,17 @@ QPDF::makeIndirectObject(QPDFObjectHandle oh) this, next.getObj(), next.getGen()); } +QPDFObjectHandle +QPDF::reserveObjectIfNotExists(int objid, int gen) +{ + QPDFObjGen og(objid, gen); + if ((!this->m->obj_cache.count(og)) && (!this->m->xref_table.count(og))) { + resolve(objid, gen); + replaceObject(objid, gen, QPDFObjectHandle::Factory::makeReserved()); + } + return getObjectByID(objid, gen); +} + QPDFObjectHandle QPDF::getObjectByObjGen(QPDFObjGen const& og) { diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index 4a61f595..4a0a69ae 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -2775,14 +2775,19 @@ QPDFObjectHandle::newReserved(QPDF* qpdf) { // Reserve a spot for this object by assigning it an object // number, but then return an unresolved handle to the object. - QPDFObjectHandle reserved = - qpdf->makeIndirectObject(QPDFObjectHandle(new QPDF_Reserved())); + QPDFObjectHandle reserved = qpdf->makeIndirectObject(makeReserved()); QPDFObjectHandle result = newIndirect(qpdf, reserved.objid, reserved.generation); result.reserved = true; return result; } +QPDFObjectHandle +QPDFObjectHandle::makeReserved() +{ + return QPDFObjectHandle(new QPDF_Reserved()); +} + void QPDFObjectHandle::setObjectDescription( QPDF* owning_qpdf, std::string const& object_description)