Open medpower opened 4 years ago
Hi Stanley,
I ran into the same issue, and Michael brayent helped me. This is the working code:
""" Creation date: 02 sep 2020
author: Eyal Michaeli, Eyalmichaeli98@gmail.com purpose: for studies annotations inputs: protocol name
This function gets:
"""
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
regular_font_size = 12 bigger_font_size = 12.4 title_font_size = 18
blue = [0.75, 1, 1] yellow = [1, 1, 0.75] green = [0.75, 1, 0.75]
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 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()
])
Thanks for this excellent package development and sharing!
I got a question and not sure whether there is any configuration that could be used to configure the background color of the text type annotation. As the snapshot below shows, the filling color was not set, I assume the parameter "fill" for text type means the font color, I also tried to change stroke color but does not take effect. Is there any metadata parameter for this purpose? I checked the source code but not find the approriate configure. Please advise. Thanks a lot for any idea sharing!
Here is the annotation code used for this demo:
Regards, Stanley