Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Add a square with the text #58

Open
EyalMichaeli opened this issue Jul 26, 2020 · 2 comments
Open

Add a square with the text #58

EyalMichaeli opened this issue Jul 26, 2020 · 2 comments

Comments

@EyalMichaeli
Copy link

EyalMichaeli commented Jul 26, 2020

Hi

I didn't understand if I can add a square automatically with the text annotation. and if so, how?

@EyalMichaeli
Copy link
Author

Yes, you can.

Attaching a working code that can make a box and a background color.

-- coding: utf-8 --
"""
Creation date: 02 sep 2020

author: Eyal Michaeli, [email protected]
purpose: for studies annotations
inputs: protocol name

  • SDTM Excel file
  • ALS Excel file
  • Study PDF file to be annotated
    outputs: Annotated PDF file
    What this function does:
    This function gets:
  • text_to_annotate: the annotation content
  • page number
  • locations x1, y1, x2, y2.
  • backColor: background color(e.g: blue = [0.75, 1, 1])
  • type: the type of annotation content(gets domain, cat, scat). The rest is a 'normal' annotation
  • bold: boolean, True if you want the annotation content to be bold. False is the default.
    note: is makes the annotation bold by adding one more annotation.
    """

from pdf_annotate import PdfAnnotator, Location, Appearance
from pdf_annotate.config import constants
from pdf_annotate.graphics import (
ContentStream, Save, BeginText, FillColor, EndText, Restore, Rect, Font, StrokeColor, Stroke
)
from pdf_annotate.annotations.text import get_text_commands
from pdf_annotate.annotations.text import FreeText
from pdf_annotate.graphics import ContentStream, Save, BeginText, FillColor, EndText, Restore,
Rect, Font, StrokeColor, Stroke, StrokeAndFill, Fill
from random import randint

Font size:
regular_font_size = 12
bigger_font_size = 12.4
title_font_size = 18

Background Colors:
blue = [0.75, 1, 1]
yellow = [1, 1, 0.75]
green = [0.75, 1, 0.75]

Font Colors:
black = [0, 0, 0]
red = [1, 0, 0]

def add_text_with_rect(text_to_annotate, page, x1=20, y1=750, x2=135, y2=775, backColor=blue, type='', bold=False):
font_size = regular_font_size
font_color = red # can be changed

Adds the annotation:

content_stream = ContentStream([
Save(),
FillColor(backColor[0], backColor[1], backColor[2]),
Rect(x1, y1, x2 - x1, y2 - y1),
Fill(),
BeginText(),
FillColor(font_color[0], font_color[1], font_color[2]),
Font('MyFont', font_size),
])
content_stream.extend(get_text_commands(
x1, y1, x2, y2,
text=text_to_annotate,
font_size=font_size,
wrap_text=True,
align=constants.TEXT_ALIGN_LEFT,
baseline=constants.TEXT_BASELINE_TOP,
line_spacing=1.2,
))
content_stream.extend([
EndText(),
StrokeColor(0, 0, 0),
Rect(x1, y1, x2 - x1, y2 - y1),
Stroke(),
Restore(),
])
appearance = Appearance(
appearance_stream=content_stream,
fonts={'MyFont': FreeText.make_font_object()},
)
crf.add_annotation(
'square',
location=Location(x1=x1, y1=y1, x2=x2, y2=y2, page=page),
appearance=appearance,
)

If you want the annotaion content to be BOLD:

if bold == True:
crf.add_annotation(
'square',
location=Location(x1=x1, y1=y1-5, x2=x2, y2=y2, page=page),
appearance=appearance,
)
content_stream.extend([
FillColor(0, 1, 0),
StrokeAndFill()
])

@michaeleekk
Copy link

michaeleekk commented Oct 12, 2020

I just did similar things and hope my script could help you a bit.

            a.add_annotation(
                'square',
                Location(x1=x1, y1=y1, x2=x2, y2=y2, page=page),
                Appearance(stroke_color=(1, 0, 0), stroke_width=2),
            )
            a.add_annotation(
                'text',
                Location(x1=x1+5, y1=y1, x2=x2, y2=y2, page=page),
                Appearance(
                    fill=[0.4, 0, 0],
                    stroke_width=1,
                    font_size=10,
                    content=filename,
                ),
            )

The +5 in the text annotation is for a left padding of 5 points from the outer border.

The difference between x1 and x2 could be calculated by the number of characters x (font_size / 2).

The difference between y1 and y2 is font_size.

image

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants