jorisschellekens / borb

borb is a library for reading, creating and manipulating PDF files in python.
https://borbpdf.com/
Other
3.4k stars 147 forks source link

KeyError: 'OCGs' #160

Closed ranga-tolapi closed 1 year ago

ranga-tolapi commented 1 year ago

Describe the bug For an existing PDF document, doc["XRef"]["Trailer"]["Root"]["OCGs"] giving an error KeyError: 'OCGs'

To Reproduce Execute the below python code snippet on the enclosed sample pdf.

import typing
from borb.pdf import Document
from borb.pdf import PDF

def main():

    # read the Document
    doc: typing.Optional[Document] = None
    with open(r"D:\Temp\usa.pdf", "rb") as in_file_handle:
        doc = PDF.loads(in_file_handle)

    # check whether we have read a Document
    assert doc is not None

    print("OCGs: %s" % doc["XRef"]["Trailer"]["Root"]["OCGs"])

if __name__ == "__main__":
    main()

Expected behaviour Expected to see a List of OCG layers.

Screenshots image

Desktop (please complete the following information):

jorisschellekens commented 1 year ago

Hi @ranga-tolapi,

This is the issues tab of borb. Please do not confuse this page with Stackoverflow.

If you have found a bug in the library, or you would like to request a feature, you are welcome to use this page. But your questions are more of the type "why is my code not working".

I have already pointed you towards the examples and the PDF specification. In particular 8.11.2 of the specification deals with optional content groups, and it will tell you exactly which keys you can query.

Keep in mind these keys might not be mandatory. A document without OCGs may not have the dictionary at all.

Kind regards, Joris Schellekens