def write_to_pdf(self, page, outpdf, crop=None, fake_highlight_text=False, **kw): """Write the annotation to page in outpdf.""" if crop is None: # PDFMiner reports positions relative to the mediaBox. crop = map(float, page.mediaBox[:]) if self.bboxes is not None: shifted_bboxes = [(bb[0] + crop[0], bb[1] + crop[1], bb[2] + crop[0], bb[3] + crop[1]) for bb in self.bboxes] if (self.text_content and fake_highlight_text): annot = highlight_annotation(shifted_bboxes, None, 'Sony eReader') pcrop = intersection(page.cropBox[:], page.mediaBox[:]) bb_center = sum((bb[0] + bb[2])/2 for bb in shifted_bboxes) / len(shifted_bboxes) if bb_center < (pcrop[0] + pcrop[2]) / 2: x = 10 else: x = pcrop[2] - 30 y = shifted_bboxes[0][1] ta = text_annotation([x, y-20, x+20, y], self.text_content, 'Sony eReader') add_annotation(outpdf, page, ta) else: annot = highlight_annotation(shifted_bboxes, self.text_content, 'Sony eReader') else: if not hasattr(page, 'prsannot_vskip'): page.prsannot_vskip = 0 pcrop = intersection(page.cropBox[:], page.mediaBox[:]) x,y = pcrop[0]+10, pcrop[3]-10-page.prsannot_vskip # Add a little margin page.prsannot_vskip += 25 annot = text_annotation([x, y-20, x+20, y], self.text_content, 'Sony eReader') add_annotation(outpdf, page, annot)
def add_annotation2pdf(inpdf, outpdf, annotations): for pg in range(1, inpdf.getNumPages() + 1): inpg = inpdf.getPage(pg - 1) if pg in annotations.keys(): if 'highlights' in annotations[pg]: for hn in annotations[pg]['highlights']: if hn['color'] is not None: annot = pdfannotation.highlight_annotation( hn["rect"], cdate=hn["cdate"], color=hn["color"]) else: annot = pdfannotation.highlight_annotation( hn["rect"], cdate=hn["cdate"]) pdfannotation.add_annotation(outpdf, inpg, annot) if 'notes' in annotations[pg]: for nt in annotations[pg]['notes']: if nt['color'] is not None: note = pdfannotation.text_annotation( nt["rect"], contents=nt["content"], author=nt["author"], color=nt["color"], cdate=nt["cdate"]) else: note = pdfannotation.text_annotation( nt["rect"], contents=nt["content"], author=nt["author"], cdate=nt["cdate"]) pdfannotation.add_annotation(outpdf, inpg, note) outpdf.addPage(inpg) return outpdf
def add_annotation2pdf(inpdf, outpdf, annotations): for pg in list(annotations.keys()): inpg = inpdf.getPage(pg-1) if 'highlights' in annotations[pg]: for hn in annotations[pg]['highlights']: annot = pdfannotation.highlight_annotation(hn["rect"], cdate=hn["cdate"]) pdfannotation.add_annotation(outpdf, inpg, annot) if 'notes' in annotations[pg]: for nt in annotations[pg]['notes']: note = pdfannotation.text_annotation(nt["rect"], contents=nt["content"], author=nt["author"], cdate=nt["cdate"]) pdfannotation.add_annotation(outpdf, inpg, note) outpdf.addPage(inpg) return outpdf