From a139d2b36da39fbfb018ef6973e9316a64a4ca6c Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Fri, 1 Jan 2021 07:31:54 -0500 Subject: Add several methods for working with form XObjects (fixes #436) Make some more methods in QPDFPageObjectHelper work with form XObjects, provide forEach methods to walk through nested form XObjects, possibly recursively. This should make it easier to work with form XObjects from user code. --- include/qpdf/QPDFObjectHandle.hh | 5 +++++ include/qpdf/QPDFPageObjectHelper.hh | 41 +++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/qpdf/QPDFObjectHandle.hh b/include/qpdf/QPDFObjectHandle.hh index e383e274..2a5d0392 100644 --- a/include/qpdf/QPDFObjectHandle.hh +++ b/include/qpdf/QPDFObjectHandle.hh @@ -1150,6 +1150,11 @@ class QPDFObjectHandle QPDF_DLL bool isFormXObject(); + // Indicate if this is an image. If exclude_imagemask is true, + // don't count image masks as images. + QPDF_DLL + bool isImage(bool exclude_imagemask=true); + private: QPDFObjectHandle(QPDF*, int objid, int generation); QPDFObjectHandle(QPDFObject*); diff --git a/include/qpdf/QPDFPageObjectHelper.hh b/include/qpdf/QPDFPageObjectHelper.hh index b25de1fe..93745446 100644 --- a/include/qpdf/QPDFPageObjectHelper.hh +++ b/include/qpdf/QPDFPageObjectHelper.hh @@ -72,11 +72,42 @@ class QPDFPageObjectHelper: public QPDFObjectHelper QPDFObjectHandle getMediaBox(bool copy_if_shared = false); + // Iterate through XObjects, possibly recursing into form + // XObjects. This works with pages or form XObjects. Call action + // on each XObject for which selector, if specified, returns true. + // With no selector, calls action for every object. In addition to + // the object being passed to action, the containing XObject + // dictionary and key are passed in. Remember that the XObject + // dictionary may be shared, and the object may appear in multiple + // XObject dictionaries. + QPDF_DLL + void forEachXObject( + bool recursive, + std::function action, + std::function selector=nullptr); + // Only call action for images + QPDF_DLL + void forEachImage( + bool recursive, + std::function action); + // Only call action for form XObjects + QPDF_DLL + void forEachFormXObject( + bool recursive, + std::function action); + // Returns an empty map if there are no images or no resources. // Prior to qpdf 8.4.0, this function did not support inherited // resources, but it does now. Return value is a map from XObject // name to the image object, which is always a stream. Works with - // form XObjects as well as pages. + // form XObjects as well as pages. This method does not recurse + // into nested form XObjects. For that, use forEachImage. QPDF_DLL std::map getImages(); @@ -84,6 +115,14 @@ class QPDFPageObjectHelper: public QPDFObjectHelper QPDF_DLL std::map getPageImages(); + // Returns an empty map if there are no form XObjects or no + // resources. Otherwise, returns a map of keys to form XObjects + // directly referenced from this page or form XObjects. This does + // not recurse into nested form XObjects. For that, use + // forEachFormXObject. + QPDF_DLL + std::map getFormXObjects(); + // Convert each inline image to an external (normal) image if the // size is at least the specified number of bytes. QPDF_DLL -- cgit v1.2.3-70-g09d2