Uploaded image for project: 'PDFBox'
  1. PDFBox
  2. PDFBOX-5263

Suggestion: Signing actual document changes - Enhancing incremental saving



    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.0.0 PDFBox
    • 3.0.0 PDFBox
    • Parsing, PDModel, Writing
    • None
    • Patch


      Currently it is rather tedious to create incremental changes in between signatures via PDFBox. I attempted to simplify that and wrote a patch.
      This is rather a POC, than an actual suggestion for direct inclusion. (For reasons explained later.)

      Signatures and incremental PDF documents:
      A typical reason for wanting to sign a document multiple times (creating an incremental PDF) is , that in between signatures the document changed and the additional signature shall sign the new state of the document.

      If one wanted to implement such incremental changes using PDFBox, he would find, that most of the time made changes are completly ignored, when calling "saveIncremental".
      As documented for the "saveIncremental" methods and especially the matching constructors in "COSWriter", this would require, to identify the "path" of all made changes and one would need to set the "needToBeUpdated" flag of all elements of that path.

      As documented one would have to have exact understanding of what he did and how the PDF standard does implement this, he would have to identify said structures and the more complex the changes were, the more tedious this would become.

      Because of the implementation of incremental saving in COSWriter, the whole path must be informed that it required an update.
      Resulting in unnecessary large increments, as not all ancestors might actually have changed.
      e.g. If one added an image to a preexisting page of the document - the contentstream, the resources of the page and the page dictionary would have changed. But the "pages" array and all it's ancestors would not have changed a bit, but still would have to be informed and included.

      Assumptions that lead to this patch:

      • COSWriter should not stop iterating a COSTree just because a parent element did not change. It's descendants still could have changed!
      • Externally managing an object´s update state is tedious and error-prone.
        Objects that implement "COSUpdateInfo" should know and manage by themselves whether they were freshly created or altered
        (e.g.: A COSDictionary should be able to remember, that a setter had been called).
      • If "COSUpdateInfo" objects were self aware and would solve this by themselves, it would not be necessary anymore to set update states manually.

      The first and obvious problem is, that the initial loading of a document is creating and altering new COS structures and we obviously don't want objects to observe and remember those changes. An object that is created during document initialization must be treated as preexisting.

      However: COSBase is not context aware - it does know it's descendants, but neither does it know it's parent, nor does it know it's root.
      If it was, that actually would present the optimal solution, as in that case the Object could ask it's root for the current load state and therefore would be able to ignore said changes caused by the initial loading of a document.
      But it is not. (My opinion is - it should be! But more on that later.)

      Therefore a a helper named COSUpdateInfoList was implemented, which was capable of finding COSUpdateInfo objects in a COS structure, and that allowed resetting their update state after loading was completed.

      Description of the patch:
      The patch implements selfaware COSUpdateInfo objects, which the COSWriter has been adapted to process. PDFBox therefore is capable of monitoring changes in realtime and to automatically include altered structures in an incremental save of the document, therefore creating increments (or an increment), that a signature would sign.

      Using this patch documents could be created:
      incrementally adding pages, adding contents to pages, adding annotations, altering structures, removing structures.
      As far as has been initially tested the resulting documents were valid, viewable in a reader and the objects overwritten in increments seemed correct.

      But - Caveat:
      This patch does introduce atleast one ugly class (most likely you will be able to point out more, that could be optimized ) and that is "COSUpdateInfoList" - as already explained: In my opinion such a class should not exist, the COSUpdateInfo objects should be context aware and should be capable of regulating their own behaviour.
      Whenever the alternatives are to either manage an object externally, or to "teach" an object to solve problems autonomously, I will tend to prefer the latter... but I did not dare to do that.

      This would require, that either further constructors or setters would have to be introduced for COSBase objects, that allowed setting parent/root/context for the object.
      Which would result in further massive changes for using applications and PDFBox itself - as all instantiations of COSBase objects (PDObjects) would have to be adapted.

      However: I would prefer if COSBase objects actually were context aware.
      But as stated... I did not dare to touch it and instead chose the ugly workarround, that would introduce yet another iteration over the whole COSDocument structure.
      Eliminating COSUpdateInfoList would be preferable!

      As PDFBox 3 is already changing how documents and objects are handled, I would suggest, that also COSBase objects should be made context and selfaware in PDFBox 3.
      This would allow simplifying handling COS objects using PDFBox and it would allow for an easier and automized handling of incremental saving.

      Usage example:
      The following "pseudo code" (actually using simplified Helper classes) demonstrates the intended usage:

      As always: Thank you very much for your work and support! I hope this suggestion is to your liking.


        1. Enhanced_incremental_saving_PDFBox3.patch
          42 kB
          Christian Appl
        2. image-2021-08-23-14-55-24-077.png
          97 kB
          Christian Appl
        3. image-2021-08-26-09-52-33-567.png
          42 kB
          Christian Appl
        4. image-2021-08-26-09-54-24-897.png
          22 kB
          Christian Appl
        5. image-2021-08-26-10-00-07-383.png
          30 kB
          Christian Appl
        6. image-2021-08-26-10-02-08-003.png
          20 kB
          Christian Appl
        7. image-2021-08-26-10-03-47-940.png
          66 kB
          Christian Appl
        8. image-2021-08-26-10-06-42-925.png
          56 kB
          Christian Appl
        9. image-2021-08-26-10-09-12-698.png
          16 kB
          Christian Appl
        10. image-2021-08-26-10-12-19-265.png
          98 kB
          Christian Appl
        11. Enhanced_incremental_saving_.patch
          52 kB
          Christian Appl
        12. Prototype__Document_and_reference_holder_aware_COSContext_.patch
          24 kB
          Christian Appl
        13. Updating_context_management_.patch
          62 kB
          Christian Appl
        14. Prototype_COSChangeObserver_(code_base).patch
          19 kB
          Christian Appl
        15. Observer_based_incremental_saving_.patch
          58 kB
          Christian Appl
        16. image-2021-09-06-17-06-59-667.png
          75 kB
          Christian Appl
        17. image-2021-09-07-09-35-31-408.png
          103 kB
          Christian Appl
        18. image-2021-09-07-13-33-00-161.png
          165 kB
          Christian Appl
        19. Observer_based_incremental_saving_-_still_eroneous.patch
          63 kB
          Christian Appl
        20. image-2021-09-07-15-40-59-080.png
          21 kB
          Christian Appl
        21. NoSignatureFound.zip
          820 kB
          Christian Appl
        22. Observer_based_incremental_saving_(fixed_).patch
          64 kB
          Christian Appl
        23. out.pdf
          3 kB
          Christian Appl
        24. out2.pdf
          3 kB
          Christian Appl
        25. Observer_based_incremental_saving_(17-00-2021-09-07).patch
          59 kB
          Christian Appl
        26. image-2021-09-08-10-23-44-036.png
          24 kB
          Christian Appl
        27. image-2021-09-08-11-18-34-211.png
          75 kB
          Christian Appl
        28. Observer_based_incremental_saving_(12-00-2021-09-08).patch
          61 kB
          Christian Appl
        29. Observer_based_incremental_saving_(15-19-2021-09-08).patch
          72 kB
          Christian Appl
        30. Observer_based_incremental_saving_(09-09-2021-09-09).patch
          81 kB
          Christian Appl
        31. PDFBOX-5263-YTW2VWJQTDAE67PGJT6GS7QSKW3GNUQR_beforePatch.pdf
          68 kB
          Maruan Sahyoun
        32. PDFBOX-5263-YTW2VWJQTDAE67PGJT6GS7QSKW3GNUQR_afterPatch.pdf
          68 kB
          Maruan Sahyoun
        33. image-2021-09-13-14-40-33-049.png
          23 kB
          Christian Appl
        34. image-2021-09-13-14-41-13-206.png
          21 kB
          Christian Appl
        35. Observer_based_incremental_saving(09-13-2021).patch
          6 kB
          Christian Appl
        36. PDFBOX-5263_Introduce_COSReferenceInfo.patch
          46 kB
          Christian Appl
        37. PDFBOX-5263_Introduce_COSReferenceInfo_(LinkedHashMap).patch
          48 kB
          Christian Appl
        38. image-2021-09-30-09-33-04-449.png
          18 kB
          Christian Appl
        39. image-2021-09-30-09-34-19-175.png
          24 kB
          Christian Appl
        40. PDFBOX-5263_Introduce_state_based_increment_handling__(POC2).patch
          87 kB
          Christian Appl
        41. PDFBOX-5263_Introduce_state_based_increment_handling_.patch
          107 kB
          Christian Appl
        42. PDFBOX-5263_Introduce_state_based_increment_handling__(simplified).patch
          105 kB
          Christian Appl
        43. image-2021-10-07-13-06-40-576.png
          88 kB
          Christian Appl
        44. image-2021-10-07-13-38-34-817.png
          116 kB
          Christian Appl
        45. profiling-2021-10-07 16-27-06.png
          137 kB
          Maruan Sahyoun
        46. image-2021-10-07-16-42-48-685.png
          66 kB
          Christian Appl
        47. image-2021-10-07-16-46-30-004.png
          45 kB
          Christian Appl
        48. image-2021-10-07-17-46-41-793.png
          36 kB
          Christian Appl
        49. image-2021-10-07-18-13-05-737.png
          40 kB
          Christian Appl



            lehmi Andreas Lehmkühler
            capSVD Christian Appl
            1 Vote for this issue
            7 Start watching this issue