Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Return color/font/formatting information for outline/bookmarks #513

Closed
mtd91429 opened this issue Aug 11, 2019 · 1 comment · Fixed by #1104
Closed

ENH: Return color/font/formatting information for outline/bookmarks #513

mtd91429 opened this issue Aug 11, 2019 · 1 comment · Fixed by #1104
Labels
is-feature A feature request PdfWriter The PdfWriter component is affected workflow-bookmarks From a users perspective, bookmarks is the affected feature/workflow

Comments

@mtd91429
Copy link
Contributor

Currently, when applying bookmarks to a document through the PdfFileWriter class method addBookmark, one can specify the color, bold, and italic properties of the bookmark using code such as

title = 'a newly added bookmark that is red and bold'
pagenum = 2
# left , top, zoom parameters
fit = '/XYZ' 
fitArgs = [317, 507, 0]
# red, green, blue specified between 0-1
color = (1, 0, 0) #red
bold = True
italic = False

# parent, color, bold, italic, fit, fitArgs
args = [None, color, bold, italic, fit, *fitArgs]
pdf.addBookmark(title, pagenum, *args)

However, when retrieving the bookmarks via the PdfFileReader class method getOutlines the color, bold, and italic formatting information is not returned.

@mtd91429
Copy link
Contributor Author

With pull request #514, the following code should work.

def detail_outlines(pdf):
    for line in pdf.getOutlines():
        if isinstance(line, list):
            yield extract_outlines(line)
        else:
            title = line.title
            page = line.page.pdf.getDestinationPageNumber(line)
            typ = line.typ
            color = rgb_to_hex([0, 0, 0])
            italic = 'notItalic'
            bold = 'notBold'
            if item.get('/C'):
                color = rgb_to_hex(item.get('/C'))
            if item.get('/F'):
                if item.get('/F') in [1,3]:
                    italic = 'italic'
                if item.get('/F') in [2,3]:
                    bold = 'bold'
            yield f'{title}, {pagenum}, {color}, {italic}, {bold}, {typ}'

def rgb_to_hex(rgb):
    red, green, blue = (int(x*255) for x in rgb)
    hex_string = f'#{red:02X}{green:02X}{blue:02X}'
    return hex_string

detail_outlines(pdf)
>>> 'a newly added bookmark that is red and bold, 2, #FF0000, notItalic, bold, /XYZ'

@MartinThoma MartinThoma added is-feature A feature request workflow-bookmarks From a users perspective, bookmarks is the affected feature/workflow PdfWriter The PdfWriter component is affected labels Apr 16, 2022
@MartinThoma MartinThoma changed the title Extracting outline/bookmarks doesn't return color or font formatting information ENH: Return color/font/formatting information for outline/bookmarks Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
is-feature A feature request PdfWriter The PdfWriter component is affected workflow-bookmarks From a users perspective, bookmarks is the affected feature/workflow
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants