def draw_bounding_poly_pdf( self, pdf: PdfReader, vertices: List[Dict], color: AnyStr, ): """Annotate a PDF document by drawing a bounding polygon of a given color Args: pdf: PDF document opened with pdfrw.PdfReader vertices: List of 4 vertices describing the polygon Each vertex should a dictionary with normalized coordinates e.g. {"x": 0.1, "y": 0.3} color: Name of the color e.g. "red", "teal", "skyblue" Full list on https://matplotlib.org/3.3.2/gallery/color/named_colors.html """ if len(vertices) == 4: pdf_annotator = PdfAnnotator(pdf) pdf_annotator.set_page_dimensions( dimensions=(1, 1), page_number=0) # normalize page dimensions pdf_annotator.add_annotation( annotation_type="polygon", location=Location( points=[(vertices[i].get("x", 0.0), 1.0 - vertices[i].get("y", 0.0)) for i in range(4)], page=0, ), appearance=Appearance(stroke_color=colors.to_rgba(color)), ) else: raise ValueError( f"Bounding polygon does not contain 4 vertices: {vertices}") return pdf
def test_add_annotation_page_dimensions(self): # Ensure that changing a page's dimensions results in annotations being # placed in the proper locations. a = PdfAnnotator(files.SIMPLE) # Act like the PDF was rastered at 144 DPI (2x default user space) a.set_page_dimensions((1224, 1584), 0) a.add_annotation( 'square', Location(x1=10, y1=20, x2=20, y2=30, page=0), Appearance(), ) with write_to_temp(a) as t: annotations = load_annotations_from_pdf(t) square = annotations.pop() assert len(annotations) == 0 assert square.Subtype == '/Square' # The outer bounding box of the square is padded outward by the stroke # width, and then scaled down by two. self.assertEqual(square.Rect, ['4.5', '9.5', '10.5', '15.5'])
def test_get_scale_from_page_dimensions(self): a = PdfAnnotator(files.SIMPLE) # Rastered at different X and Y scales. Why would you do that? a.set_page_dimensions((1275, 3300), 0) assert a.get_scale(0) == (0.48, 0.24)
def test_get_rotated_scale_from_dimensions(self): a = PdfAnnotator(files.ROTATED_90) a.set_page_dimensions((3300, 1275), 0) assert a.get_scale(0) == (0.24, 0.48)
dim_geschoss = [ [1447.5 - 2 * 18, 1049], [1447.5 - 2 * 18, 1209 - 17 - 18], [1447.5 - 2 * 18, 883 + 292.5 + 31.5], ] file_name = ["UG", "EG", "DG"] yaml_file = "data/setup.yaml" haus = read_setup(yaml_file) for i in range(0, 3): geschoss = haus.geschosse[i] file_name_file = "{}.pdf".format(file_name[i]) file_name_full = "data/plaene/{}".format(file_name_file) print("Reading:{} and storing to:{}".format(file_name_full, file_name_file)) a = PdfAnnotator(file_name_full) a.set_page_dimensions((pdf_dim[i][0], pdf_dim[i][1]), 0) pos_x1 = pdf_dim[i][2] pos_x2 = pdf_dim[i][3] pos_y1 = pdf_dim[i][4] pos_y2 = pdf_dim[i][5] for wall in geschoss.walls: x = (wall.x) / (dim_geschoss[i][0]) * (pos_x2 - pos_x1) + pos_x1 y = (wall.y) / (dim_geschoss[i][1]) * (pos_y1 - pos_y2) + pos_y2 x2 = (wall.x + wall.dx) / (dim_geschoss[i][0]) * (pos_x2 - pos_x1) + pos_x1 y2 = (wall.y + wall.dy) / (dim_geschoss[i][1]) * (pos_y1 - pos_y2) + pos_y2 a.add_annotation( "square", Location(x1=x, y1=y, x2=x2, y2=y2, page=0),