Unable to place text on a rectangle object with fill color using the borb library in Python

I am currently working on creating PDFs using the borb library in Python. I have successfully created a rectangle object using borb and filled it with a desired colour. However, when I try to place text on top of this rectangle object, the text seems to be hidden behind the bounding box of the rectangle, and I am unable to view it.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import typing
from decimal import Decimal
from borb.pdf import Document
from borb.pdf import PDF
from borb.pdf import Page
from borb.pdf import Paragraph
from borb.pdf import HexColor
from borb.pdf.canvas.geometry.rectangle import Rectangle
from borb.pdf.canvas.layout.annotation.square_annotation import SquareAnnotation
def main():
# CREATE A DOCUMENT #######################################################
doc: Document = Document()
# CREATE A PAGE ###########################################################
page: Page = Page()
# ADD A PAGE TO THE DOCUMENT ##############################################
doc.add_page(page)
# GET PAGE WIDTH AND HEIGHT ###############################################
PAGE_WIDTH: typing.Optional[Decimal] = page.get_page_info().get_width()
assert PAGE_WIDTH is not None
PAGE_HEIGHT: typing.Optional[Decimal] = page.get_page_info().get_height()
assert PAGE_WIDTH is not None
# SET HEADER BOUNDING BOX #################################################
HEADER_HEIGHT: int = 70
header_bb: Rectangle = Rectangle(
Decimal(0),
Decimal(PAGE_HEIGHT - HEADER_HEIGHT),
Decimal(PAGE_WIDTH),
Decimal(HEADER_HEIGHT),
)
page.add_annotation(SquareAnnotation(
bounding_box = header_bb,
stroke_color = HexColor("#2f5496"),
fill_color = HexColor("#2f5496") # LINE 43
))
# ADD A TEXT OVER BOUNDING BOX ############################################
Paragraph("Sample Text").paint(page, header_bb)
with open("borb-pdf.pdf", "wb") as out_file_handle:
PDF.dumps(out_file_handle, doc)
if __name__ == "__main__":
main()
</code>
<code>import typing from decimal import Decimal from borb.pdf import Document from borb.pdf import PDF from borb.pdf import Page from borb.pdf import Paragraph from borb.pdf import HexColor from borb.pdf.canvas.geometry.rectangle import Rectangle from borb.pdf.canvas.layout.annotation.square_annotation import SquareAnnotation def main(): # CREATE A DOCUMENT ####################################################### doc: Document = Document() # CREATE A PAGE ########################################################### page: Page = Page() # ADD A PAGE TO THE DOCUMENT ############################################## doc.add_page(page) # GET PAGE WIDTH AND HEIGHT ############################################### PAGE_WIDTH: typing.Optional[Decimal] = page.get_page_info().get_width() assert PAGE_WIDTH is not None PAGE_HEIGHT: typing.Optional[Decimal] = page.get_page_info().get_height() assert PAGE_WIDTH is not None # SET HEADER BOUNDING BOX ################################################# HEADER_HEIGHT: int = 70 header_bb: Rectangle = Rectangle( Decimal(0), Decimal(PAGE_HEIGHT - HEADER_HEIGHT), Decimal(PAGE_WIDTH), Decimal(HEADER_HEIGHT), ) page.add_annotation(SquareAnnotation( bounding_box = header_bb, stroke_color = HexColor("#2f5496"), fill_color = HexColor("#2f5496") # LINE 43 )) # ADD A TEXT OVER BOUNDING BOX ############################################ Paragraph("Sample Text").paint(page, header_bb) with open("borb-pdf.pdf", "wb") as out_file_handle: PDF.dumps(out_file_handle, doc) if __name__ == "__main__": main() </code>
import typing
from decimal import Decimal

from borb.pdf import Document
from borb.pdf import PDF
from borb.pdf import Page
from borb.pdf import Paragraph
from borb.pdf import HexColor
from borb.pdf.canvas.geometry.rectangle import Rectangle
from borb.pdf.canvas.layout.annotation.square_annotation import SquareAnnotation


def main():
    # CREATE A DOCUMENT #######################################################
    doc: Document = Document()

    # CREATE A PAGE ###########################################################
    page: Page = Page()

    # ADD A PAGE TO THE DOCUMENT ##############################################
    doc.add_page(page)

    # GET PAGE WIDTH AND HEIGHT ###############################################
    PAGE_WIDTH: typing.Optional[Decimal] = page.get_page_info().get_width()
    assert PAGE_WIDTH is not None

    PAGE_HEIGHT: typing.Optional[Decimal] = page.get_page_info().get_height()
    assert PAGE_WIDTH is not None

    # SET HEADER BOUNDING BOX #################################################

    HEADER_HEIGHT: int = 70
    header_bb: Rectangle = Rectangle(
        Decimal(0),
        Decimal(PAGE_HEIGHT - HEADER_HEIGHT),
        Decimal(PAGE_WIDTH),
        Decimal(HEADER_HEIGHT),
    )

    page.add_annotation(SquareAnnotation(
        bounding_box = header_bb,
        stroke_color = HexColor("#2f5496"),
        fill_color   = HexColor("#2f5496")    # LINE 43
    ))

    # ADD A TEXT OVER BOUNDING BOX ############################################

    Paragraph("Sample Text").paint(page, header_bb)

    with open("borb-pdf.pdf", "wb") as out_file_handle:
        PDF.dumps(out_file_handle, doc)


if __name__ == "__main__":
    main()

In this code, the rectangle is displayed with the specified fill colour, but the text “Sample Text” does not appear on top of the rectangle. However, if I comment on line 43 then I can view the PDF.

I suspect that the text is being placed behind the bounding box of the rectangle, causing it to be hidden. How can I ensure that the text is displayed on top of the rectangle with the fill colour applied?

Any insights or suggestions would be greatly appreciated. Thank you!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật