def test_add_polyline_annotation_using_lineart_factory(self): # create output directory if it does not exist yet if not self.output_file.parent.exists(): self.output_file.parent.mkdir() # attempt to read PDF doc = None with open(self.input_file, "rb") as in_file_handle: print("\treading (1) ..") doc = PDF.loads(in_file_handle) # add annotation doc.get_page(0).append_polyline_annotation( points=LineArtFactory.droplet( Rectangle(Decimal(100), Decimal(100), Decimal(100), Decimal(100))), stroke_color=X11Color("Crimson"), ) # attempt to store PDF with open(self.output_file, "wb") as out_file_handle: print("\twriting ..") PDF.dumps(out_file_handle, doc) # attempt to re-open PDF with open(self.output_file, "rb") as in_file_handle: print("\treading (2) ..") doc = PDF.loads(in_file_handle) return True
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create empty document pdf: Document = Document() # create empty page page: Page = Page() # add page to document pdf.append_page(page) # set layout layout = SingleColumnLayout(page) # add barcode layout.add( Barcode( data="123456789128", type=BarcodeType.CODE_128, width=Decimal(128), stroke_color=HexColor("#080708"), )) # write file = self.output_dir / "output.pdf" with open(file, "wb") as pdf_file_handle: PDF.dumps(pdf_file_handle, pdf) return True
def test_document(self, file): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / (file.stem + "_out.pdf") # attempt to read PDF doc = None with open(file, "rb") as in_file_handle: print("\treading (1) ..") doc = PDF.loads(in_file_handle) # add annotation doc.get_page(0).append_polygon_annotation( points=LineArtFactory.sticky_note( Rectangle(Decimal(128), Decimal(128), Decimal(64), Decimal(64))), stroke_color=X11Color("PowderBlue"), ) # attempt to store PDF with open(out_file, "wb") as out_file_handle: print("\twriting ..") PDF.dumps(out_file_handle, doc) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: print("\treading (2) ..") doc = PDF.loads(in_file_handle) return True
def test(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() for file_to_convert in [ "example-json-input-001.json", "example-json-input-002.json", "example-json-input-003.json", ]: json_data = None path_to_json = Path(__file__).parent / file_to_convert with open(path_to_json, "r") as json_file_handle: json_data = json.loads(json_file_handle.read()) # convert document: Document = JSONToPDF.convert_json_to_pdf(json_data) # store output_file = self.output_dir / (file_to_convert + ".pdf") with open(output_file, "wb") as pdf_file_handle: PDF.dumps(pdf_file_handle, document) return True
def test_document(self): # create output directory if it does not exist yet if not self.output_file.parent.exists(): self.output_file.parent.mkdir() # read document doc = None with open(self.input_file, "rb") as pdf_file_handle: doc = PDF.loads(pdf_file_handle) # append document doc.append_embedded_file( "the_raven.txt", b"Once upon a midnight dreary, while I pondered weak and weary over many a quaint and curious volume of forgotten lore.", ) # attempt to store PDF with open(self.output_file, "wb") as out_file_handle: print("\twriting ..") PDF.dumps(out_file_handle, doc) # attempt to re-open PDF with open(self.output_file, "rb") as in_file_handle: print("\treading (2) ..") doc = PDF.loads(in_file_handle) embedded_files = doc.get_embedded_files() assert len(embedded_files) == 1 assert "the_raven.txt" in embedded_files assert b"Once upon a midnight" in embedded_files["the_raven.txt"]
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) Paragraph( "Once upon a midnight dreary, while I pondered weak and weary, over many a quaint and curious volume of forgotten lore", font_size=Decimal(20), horizontal_alignment=Alignment.RIGHT, ).layout( page, Rectangle(Decimal(20), Decimal(600), Decimal(500), Decimal(124)), ) # add rectangle annotation page.append_square_annotation( stroke_color=X11Color("Red"), rectangle=Rectangle(Decimal(20), Decimal(600), Decimal(500), Decimal(124)), ) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self) -> bool: # read input doc = None with open("/home/joris/Code/pdf-corpus/0203.pdf", "rb") as pdf_file_handle: doc = PDF.loads(pdf_file_handle) # read images xobjects = doc["XRef"]["Trailer"]["Root"]["Pages"]["Kids"][0][ "Resources"]["XObject"] # modify image(s) for k, v in xobjects.items(): if isinstance(v, PILImage.Image): self._modify_image(v) # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # write file = self.output_dir / "output.pdf" with open(file, "wb") as pdf_file_handle: PDF.dumps(pdf_file_handle, doc) return True
def test_add_circle_annotation(self): # create output directory if it does not exist yet if not self.output_file.parent.exists(): self.output_file.parent.mkdir() # attempt to read PDF doc = None with open(self.input_file, "rb") as in_file_handle: print("\treading (1) ..") doc = PDF.loads(in_file_handle) # add annotation doc.get_page(0).append_circle_annotation( rectangle=Rectangle(Decimal(128), Decimal(128), Decimal(64), Decimal(64)), stroke_color=X11Color("Plum"), fill_color=X11Color("Crimson"), ) # attempt to store PDF with open(self.output_file, "wb") as out_file_handle: print("\twriting ..") PDF.dumps(out_file_handle, doc) # attempt to re-open PDF with open(self.output_file, "rb") as in_file_handle: print("\treading (2) ..") doc = PDF.loads(in_file_handle) return True
def test_add_underline_annotation(self): # create output directory if it does not exist yet if not self.output_file.parent.exists(): self.output_file.parent.mkdir() # attempt to read PDF doc = None with open(self.input_file, "rb") as in_file_handle: print("\treading (1) ..") doc = PDF.loads(in_file_handle) # add annotation doc.get_page(0).append_underline_annotation( stroke_color=HexColor("CBEF43"), rectangle=Rectangle(Decimal(72.86), Decimal(486.82), Decimal(129), Decimal(13)), ) # attempt to store PDF with open(self.output_file, "wb") as out_file_handle: print("\twriting ..") PDF.dumps(out_file_handle, doc) # attempt to re-open PDF with open(self.output_file, "rb") as in_file_handle: print("\treading (2) ..") doc = PDF.loads(in_file_handle) return True
def test_add_link_annotation(self): # create output directory if it does not exist yet if not self.output_file.parent.exists(): self.output_file.parent.mkdir() # attempt to read PDF doc = None with open(self.input_file, "rb") as in_file_handle: print("\treading (1) ..") doc = PDF.loads(in_file_handle) # add annotation doc.get_page(0).append_link_annotation( page=Decimal(0), destination_type=DestinationType.FIT, color=X11Color("Red"), rectangle=Rectangle(Decimal(128), Decimal(128), Decimal(64), Decimal(64)), ) # attempt to store PDF with open(self.output_file, "wb") as out_file_handle: print("\twriting ..") PDF.dumps(out_file_handle, doc) # attempt to re-open PDF with open(self.output_file, "rb") as in_file_handle: print("\treading (2) ..") doc = PDF.loads(in_file_handle) return True
def test_document(self, file): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / (file.stem + "_out.pdf") # attempt to read PDF doc = None with open(file, "rb") as in_file_handle: print("\treading (1) ..") doc = PDF.loads(in_file_handle) # add annotation doc.get_page(0).append_text_annotation( contents="The quick brown fox ate the lazy mouse", rectangle=Rectangle(Decimal(128), Decimal(128), Decimal(64), Decimal(64)), name_of_icon="Key", open=True, color=X11Color("Orange"), ) # attempt to store PDF with open(out_file, "wb") as out_file_handle: print("\twriting ..") PDF.dumps(out_file_handle, doc) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: print("\treading (2) ..") doc = PDF.loads(in_file_handle) return True
def test_document(self, file): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / (file.stem + "_out.pdf") # attempt to read PDF delta = time.time() doc = None with open(file, "rb") as in_file_handle: print("\treading (1) ..") doc = PDF.loads(in_file_handle) print("time elapsed : %d" % (time.time() - delta)) delta = time.time() # attempt to store PDF with open(out_file, "wb") as out_file_handle: print("\twriting ..") PDF.dumps(out_file_handle, doc) print("time elapsed : %d" % (time.time() - delta)) delta = time.time() # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: print("\treading (2) ..") doc = PDF.loads(in_file_handle) print("time elapsed : %d" % (time.time() - delta)) delta = time.time() return True
def test_document(self, file): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / (file.stem + "_out.pdf") # attempt to read PDF doc = None with open(file, "rb") as in_file_handle: print("\treading (1) ..") doc = PDF.loads(in_file_handle) number_of_pages = int(doc.get_document_info().get_number_of_pages()) if number_of_pages == 1: return # remove page doc.pop_page(0) # attempt to store PDF with open(out_file, "wb") as out_file_handle: print("\twriting ..") PDF.dumps(out_file_handle, doc) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: print("\treading (2) ..") doc = PDF.loads(in_file_handle) return True
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) for i in range(0, 10): space = "".join([" " for _ in range(0, i + 1)]) txt = "".join([ x + space for x in ["apple", "banana", "carrot", "dragonfruit"] ]) layout.add(Paragraph(txt, respect_spaces_in_text=True)) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_document(self, file): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / (file.stem + "_out.pdf") # attempt to read PDF doc = None with open(file, "rb") as in_file_handle: print("\treading (1) ..") doc = PDF.loads(in_file_handle) # add annotation doc.get_page(0).append_circle_annotation( rectangle=(Decimal(128), Decimal(128), Decimal(64), Decimal(64)), interior_color=X11Color("Plum"), color=X11Color("Crimson"), ) # attempt to store PDF with open(out_file, "wb") as out_file_handle: print("\twriting ..") PDF.dumps(out_file_handle, doc) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: print("\treading (2) ..") doc = PDF.loads(in_file_handle) return True
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create empty document pdf: Document = Document() # create empty page page: Page = Page() # add page to document pdf.append_page(page) # add Image layout = SingleColumnLayout(page) # add image im = PILImage.open( requests.get( "https://images.unsplash.com/photo-1597826368522-9f4cb5a6ba48?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw", stream=True, ).raw ) layout.add(Image(im, width=Decimal(256))) # write file = self.output_dir / "output.pdf" with open(file, "wb") as pdf_file_handle: PDF.dumps(pdf_file_handle, pdf) return True
def test_document(self, file) -> bool: # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() doc = None with open(file, "rb") as pdf_file_handle: doc = PDF.loads(pdf_file_handle) if "XRef" not in doc: return False if "Trailer" not in doc["XRef"]: return False if "Info" not in doc["XRef"]["Trailer"]: doc["XRef"]["Trailer"][Name("Info")] = Dictionary() # change author doc["XRef"]["Trailer"]["Info"]["Author"] = String("Joris Schellekens") # determine output location out_file = self.output_dir / (file.stem + "_out.pdf") with open(out_file, "wb") as pdf_file_handle: PDF.dumps(pdf_file_handle, doc) return True
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) ul = OrderedList() ul.add(Paragraph(text="Lorem Ipsum Dolor Sit Amet Consectetur Nunc")) ul.add(Paragraph(text="Ipsum")) ul.add(Paragraph(text="Dolor")) ul.add(Paragraph(text="Sit")) ul.add(Paragraph(text="Amet")) layout = SingleColumnLayout(page) layout.add(ul) # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_document(self, file): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / (file.stem + "_out.pdf") # attempt to read PDF doc = None with open(file, "rb") as in_file_handle: print("\treading (1) ..") doc = PDF.loads(in_file_handle) # add annotation doc.get_page(0).append_stamp_annotation( name="Confidential", contents="Approved by Joris Schellekens", color=X11Color("White"), rectangle=(Decimal(128), Decimal(128), Decimal(32), Decimal(64)), ) # attempt to store PDF with open(out_file, "wb") as out_file_handle: print("\twriting ..") PDF.dumps(out_file_handle, doc) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: print("\treading (2) ..") doc = PDF.loads(in_file_handle) return True
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) ChunkOfText( "é", font_size=Decimal(24), ).layout( page, Rectangle(Decimal(100), Decimal(600), Decimal(100), Decimal(100))) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_document(self, file) -> bool: doc = None with open(file, "rb") as pdf_file_handle: doc = None with open(file, "rb") as pdf_file_handle: doc = PDF.loads(pdf_file_handle) if "XRef" not in doc: return False if "Trailer" not in doc["XRef"]: return False if "Info" not in doc["XRef"]["Trailer"]: doc["XRef"]["Trailer"][Name("Info")] = Dictionary() # change producer doc["XRef"]["Trailer"]["Info"]["Producer"] = String("pText") # determine output location out_file = self.output_dir / (file.stem + "_out.pdf") with open(out_file, "wb") as pdf_file_handle: PDF.dumps(out_file, doc) return True
def test_document(self, file): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / (file.stem + "_out.pdf") doc = None with open(file, "rb") as pdf_file_handle: doc = PDF.loads(pdf_file_handle) # remove first annotation if "Annots" in doc.get_page(0): annots = doc.get_page(0)["Annots"] annots[0]["P"] = None doc.get_page(0)["Annots"] = annots[1:0] pass # attempt to store PDF with open(out_file, "wb") as out_file_handle: print("\twriting ..") PDF.dumps(out_file_handle, doc) return True
def test_add_rubber_stamp_annotation(self): # create output directory if it does not exist yet if not self.output_file.parent.exists(): self.output_file.parent.mkdir() # attempt to read PDF doc = None with open(self.input_file, "rb") as in_file_handle: print("\treading (1) ..") doc = PDF.loads(in_file_handle) # add annotation doc.get_page(0).append_stamp_annotation( name=RubberStampAnnotationIconType.CONFIDENTIAL, contents="Approved by Joris Schellekens", color=X11Color("Red"), rectangle=Rectangle(Decimal(128), Decimal(56), Decimal(132), Decimal(58)), ) # attempt to store PDF with open(self.output_file, "wb") as out_file_handle: print("\twriting ..") PDF.dumps(out_file_handle, doc) # attempt to re-open PDF with open(self.output_file, "rb") as in_file_handle: print("\treading (2) ..") doc = PDF.loads(in_file_handle) return True
def _test_document(self, file): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / (file.stem + "_out.pdf") # attempt to read PDF doc_a = None with open(file, "rb") as in_file_handle: print("\treading (1) ..") doc_a = PDF.loads(in_file_handle) # attempt to read PDF with open(self.input_file_b, "rb") as in_file_handle_b: print("\treading (2) ..") doc_b = PDF.loads(in_file_handle_b) # concat all pages to same document doc_c = Document() doc_c.append_document(doc_a) doc_c.append_document(doc_b) # attempt to store PDF with open(out_file, "wb") as out_file_handle: print("\twrite ..") PDF.dumps(out_file_handle, doc_c) return True
def test_document(self, file): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / (file.stem + "_out.pdf") # attempt to read PDF doc = None with open(file, "rb") as in_file_handle: print("\treading (1) ..") doc = PDF.loads(in_file_handle) # add annotation doc.get_page(0).append_redact_annotation( overlay_text="Lorem Ipsum", repeat_overlay_text=True, fill_color=X11Color("AliceBlue"), rectangle=Rectangle(Decimal(72.86), Decimal(486.82), Decimal(129), Decimal(13)), ) # attempt to store PDF with open(out_file, "wb") as out_file_handle: print("\twriting ..") PDF.dumps(out_file_handle, doc) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: print("\treading (2) ..") doc = PDF.loads(in_file_handle) return True
def test_add_polygon_annotation(self): # create output directory if it does not exist yet if not self.output_file.parent.exists(): self.output_file.parent.mkdir() # attempt to read PDF doc = None with open(self.input_file, "rb") as in_file_handle: print("\treading (1) ..") doc = PDF.loads(in_file_handle) # add annotation doc.get_page(0).append_polygon_annotation( points=[ (Decimal(72), Decimal(390)), (Decimal(242), Decimal(500)), (Decimal(156), Decimal(390)), ], stroke_color=X11Color("Crimson"), ) # attempt to store PDF with open(self.output_file, "wb") as out_file_handle: print("\twriting ..") PDF.dumps(out_file_handle, doc) # attempt to re-open PDF with open(self.output_file, "rb") as in_file_handle: print("\treading (2) ..") doc = PDF.loads(in_file_handle) return True
def test_document(self, file): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / (file.stem + "_out.pdf") # attempt to read PDF doc = None with open(file, "rb") as in_file_handle: print("\treading (1) ..") doc = PDF.loads(in_file_handle) # add annotation doc.get_page(0).append_watermark_annotation( contents="pText", rectangle=Rectangle(Decimal(128), Decimal(128), Decimal(64), Decimal(64)), ) # attempt to store PDF with open(out_file, "wb") as out_file_handle: print("\twriting ..") PDF.dumps(out_file_handle, doc) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: print("\treading (2) ..") doc = PDF.loads(in_file_handle)
def test_write_document(self): sentences = [ "THE BOAT WILL ARRIVE ON MONDAY", "SHE LIVES AT THE HOUSE WITH THE BLUE DOOR", "A FRIEND IN NEED IS A FRIEND INDEED", "AN APPLE A DAY KEEPS THE DOCTOR AWAY", ] pdf = Document() page = Page() pdf.append_page(page) # layout layout = SingleColumnLayout(page) # add title layout.add( Paragraph( "Secret Code Puzzle", font_size=Decimal(20), font_color=X11Color("YellowGreen"), ) ) # add text layout.add( Paragraph( """ There are three riddles below written in a secret code. Can you unravel them? Once you have, copy the sentence on the line underneath the code. Hint: Each letter of the alphabet has been replaced by a number. All the riddles use the same code. """, respect_newlines_in_text=True, font_color=X11Color("SlateGray"), font_size=Decimal(8), ) ) # add grid for s in sentences: layout.add(self._build_table_for_sentence(s)) layout.add(Paragraph(" ", respect_spaces_in_text=True)) # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create empty document pdf: Document = Document() # create empty page page: Page = Page() # add page to document pdf.append_page(page) # set layout layout = MultiColumnLayout(page) # add chart layout.add(Chart(self._create_plot())) layout.switch_to_next_column() # add Heading layout.add( Heading( "3D Density Chart", font_color=X11Color("YellowGreen"), font_size=Decimal(20), ) ) layout.add( Paragraph( "The mplot3D toolkit of Matplotlib allows to easily create 3D scatterplots. " "Note that most of the customisations presented in the Scatterplot section will work in 3D as well. " "The result can be a bit disappointing since each marker is represented as a dot, not as a sphere.." ) ) layout.add( Paragraph( "Check out https://python-graph-gallery.com/ for more wonderful examples of plots in Python." ) ) layout.add( Barcode( data="https://python-graph-gallery.com/", type=BarcodeType.QR, stroke_color=X11Color("YellowGreen"), ) ) # write file = self.output_dir / "output.pdf" with open(file, "wb") as pdf_file_handle: PDF.dumps(pdf_file_handle, pdf) return True
def _test_document(self, file): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / (file.stem + "_out.pdf") # attempt to read PDF doc = None with open(file, "rb") as in_file_handle: print("\treading (1) ..") doc = PDF.loads(in_file_handle) # determine free space space_finder = FreeSpaceFinder(doc.get_page(0)) # debug purposes if self.in_debug: for i in range(0, len(space_finder.grid)): for j in range(0, len(space_finder.grid[i])): if space_finder.grid[i][j]: continue w = Decimal(space_finder.grid_resolution) x = Decimal(i) * w y = Decimal(j) * w doc.get_page(0).append_square_annotation( Rectangle(x, y, w, w), stroke_color=X11Color("Salmon")) # add annotation w, h = doc.get_page(0).get_page_info().get_size() free_rect = space_finder.find_free_space( Rectangle( Decimal(w / Decimal(2)), Decimal(h * Decimal(2)), Decimal(64), Decimal(64), )) if free_rect is not None: doc.get_page(0).append_square_annotation( rectangle=free_rect, stroke_color=HexColor("#F75C03"), fill_color=HexColor("#04A777"), ) # attempt to store PDF with open(out_file, "wb") as out_file_handle: print("\twriting ..") PDF.dumps(out_file_handle, doc) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: print("\treading (2) ..") doc = PDF.loads(in_file_handle) return True