def create_bookmarks(bookmarks, pages, parent=None): count = len(bookmarks) bookmark_objects = [] for label, target, children in bookmarks: destination = (pages[target[0]].indirect, PdfName('XYZ'), target[1], target[2], 0) bookmark_object = PdfDict(Title=PdfString.encode(label), A=PdfDict(Type=PdfName('Action'), S=PdfName('GoTo'), D=PdfArray(destination))) bookmark_object.indirect = True children_objects, children_count = create_bookmarks( children, pages, parent=bookmark_object) bookmark_object.Count = 1 + children_count if bookmark_objects: bookmark_object.Prev = bookmark_objects[-1] bookmark_objects[-1].Next = bookmark_object if children_objects: bookmark_object.First = children_objects[0] bookmark_object.Last = children_objects[-1] if parent is not None: bookmark_object.Parent = parent count += children_count bookmark_objects.append(bookmark_object) return bookmark_objects, count
def get_png_smask(image): width, height = image.size smask = Image.make_compressed_image_content(image.getchannel('A')) smask_xobj = PdfDict( stream=smask, Width=width, Height=height, BitsPerComponent=8, Filter=PdfName('FlateDecode'), ColorSpace=PdfName('DeviceGray'), Subtype=PdfName('Image'), Type=PdfName('XObject'), ) smask_xobj.indirect = True return smask_xobj
def create_highlight(points, color=(1, 0.92, 0.23), author=None, contents=None): """Given Quad points, create a highligh object in standard pdf format.""" new_highlight = PdfDict() new_highlight.F = 4 new_highlight.Type = PdfName('Annot') new_highlight.Subtype = PdfName('Highlight') if author: new_highlight.T = author new_highlight.C = color if contents: new_highlight.Contents = contents new_highlight.indirect = True ############################################################# ### Search for bounding coordinates ############################################################# bot_left_x = float('inf') bot_left_y = float('inf') top_right_x = 0.0 top_right_y = 0.0 quad_pts = [] for (x1, y1, x2, y2) in points: # this quadpoints specified PDF definition of rect box quad_pts.extend([x1, y2, x2, y2, x1, y1, x2, y1]) bot_left_x = min(bot_left_x, x1, x2) bot_left_y = min(bot_left_y, y1, y2) top_right_x = max(top_right_x, x1, x2) top_right_y = max(top_right_y, y1, y2) new_highlight.QuadPoints = PdfArray(quad_pts) new_highlight.Rect = PdfArray( [bot_left_x, bot_left_y, top_right_x, top_right_y]) return new_highlight
def apply_annotations(rmpage, page_annot, ocgorderinner): for k, layer_a in enumerate(page_annot): layerannots = layer_a[1] for a in layerannots: # PDF origin is in bottom-left, so invert all # y-coordinates. author = 'RCU' #self.model.device_info['rcuname'] pdf_a = PdfDict(Type=PdfName('Annot'), Rect=PdfArray([(a[1] * PTPERPX), PDFHEIGHT - (a[2] * PTPERPX), (a[3] * PTPERPX), PDFHEIGHT - (a[4] * PTPERPX)]), T=author, ANN='pdfmark', Subtype=PdfName(a[0]), P=rmpage) # Set to indirect because it makes a cleaner PDF # output. pdf_a.indirect = True if ocgorderinner: pdf_a.OC = ocgorderinner[k] if not '/Annots' in rmpage: rmpage.Annots = PdfArray() rmpage.Annots.append(pdf_a)
pdf_kid.Contents = img_kid.Contents[0] pdf_kid.Contents.stream = pdf_kid.Contents.stream[: 105] # unnecessary clean-up: getting rid of traces of Image-7370 pdf_kid.MediaBox = img_kid.MediaBox alt_img = PdfDict(Type=PdfName.XObject, SubType=PdfName.Image, BitsPerComponent=8, ColorSpace=PdfName.DeviceRGB, Height=800, Width=600, Length=0, F=PdfDict(FS=PdfName.URL, F='https://chezsoi.org/lucas/ThePatch.jpg'), FFilter=PdfName.DCTDecode) alt_img.indirect = true alternates = PdfArray([PdfDict(DefaultForPrinting=True, Image=alt_img)]) alternates.indirect = true img_name = PdfName('Image-9960') img = img_kid.Resources.XObject[img_name] img.Alternates = alternates pdf_kid.Resources.XObject = PdfDict() pdf_kid.Resources.XObject[img_name] = img out = PdfWriter() out.addpage(pdf.pages[0]) out.write('out.pdf') # CONCLUSION: neither Adobe nor Sumatra readers visit the link...