-
Notifications
You must be signed in to change notification settings - Fork 557
Demo: GUI script to display a PDF using wxPython or Tkinter
This demonstrates the superior rendering capabilities of MuPDF using wxPython as the display manager.
To access the program, have a look at PDFdisplay.py in the demo folder of the repository.
The program now supports the display of links contained in a page. When activating this option, the next displayed page will mark any link with a blue-lined frame. When clicking such a link, the respective action will be performed. Supported are links within the document (GoTo
), links to other documents (GoToR
, in this case subprocess.Popen
is used to display the requested document with the standard PDF reader), links to web sites (URI
) and named destinations.
This is how the new version will look like:
This demo can easily be adopted to Tkinter. You need the imports
from Tkinter import Tk, Canvas, Frame, BOTH, NW
from PIL import Image, ImageTk
and do the following to display each PDF page image:
#-----------------------------------------------------------------
# MuPDF code
#-----------------------------------------------------------------
pix = doc.getPagePixmap(pno - 1) # create pixmap for a page
#-----------------------------------------------------------------
# Tkinter code
#-----------------------------------------------------------------
self.img = Image.frombytes("RGBA", [pix.width, pix.height], pix.samples)
selfphoto = ImageTk.PhotoImage(self.img)
canvas = Canvas(self, width=self.img.size[0]+20,
height=self.img.size[1]+20)
canvas.create_image(10, 10, anchor=NW, image=self.photo)
canvas.pack(fill=BOTH, expand=1)
When using this newest wxPython version, you can simply use its new pdfviewer
class. Here is the sample code provided in the online documentation.
HOWTO Button annots with JavaScript
HOWTO work with PDF embedded files
HOWTO extract text from inside rectangles
HOWTO extract text in natural reading order
HOWTO create or extract graphics
HOWTO create your own PDF Drawing
Rectangle inclusion & intersection
Metadata & bookmark maintenance