Search code examples
pythonannotationspypdf

How to set transparent background of annotations using PyPDF2?


# Create the annotation and add it
annotation = AnnotationBuilder.free_text(
    "Foo",
    rect=(500, 785, 380, 810),
    font="Arial",
    bold=True,
    italic=False,
    font_size="19pt",
    font_color="000000",
    border_color="000000",
    background_color="ffffff00",
)
writer.add_annotation(page_number=0, annotation=annotation)

Any idea how to make the background of my annotation transparent? By the way, I'm also wondering how to center the text.


Solution

  • Upgrade to pypdf>=3.5.0 and set the background_color to None :-)

    https://pypdf.readthedocs.io/en/latest/modules/AnnotationBuilder.html#pypdf.generic.AnnotationBuilder.free_text