plangrid / pdf-annotate

Pure-python library for adding annotations to PDFs
MIT License
195 stars 45 forks source link

About background color setting for text type annotation #61

Open medpower opened 4 years ago

medpower commented 4 years ago

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!

image

Here is the annotation code used for this demo:

    annotator.add_annotation('text',
                                     location=Location(x1=obj.bbox[0],
                                                       y1=obj.bbox[1],
                                                       x2=obj.bbox[2],
                                                       y2=obj.bbox[3],
                                                       page=curpage),
                                     appearance=Appearance(
                                         fill=(0.705, 0.094, 0.125, 1),
                                         stroke_width=3,
                                         stroke_color=(0.941, 0.835, 0.156, 1),
                                         font_size=9,
                                         content=string)
                                     )

Regards, Stanley

EyalMichaeli commented 4 years ago

Hi Stanley,

I ran into the same issue, and Michael brayent helped me. This is the working code:

-- coding: utf-8 --

""" Creation date: 02 sep 2020

author: Eyal Michaeli, Eyalmichaeli98@gmail.com purpose: for studies annotations inputs: protocol name

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()
])