mupdf::widget - a basic PDF-viewer widget
mupdf::widget is a fully customizable widget for displaying PDF-files, based on the package tclMuPDF, a tcl-binding of the popular MuPDF framework.
You need to install tclMuPDF, then use it for opening PDF files. Once a PDF-file has been opened, i.e. you got a pdfObj, you can use it for working with an instance of a mupdf::widget.
# choose a PDF file ... set PDFname "/docs/abc.pdf" # create a pdfObj set pdfObj [mupdf::open $PDFname] # create a widget for pdfObj mupdf::widget .pdfWin $pdfObj # display the widget .pdfW pack .pdfWin -expand 1 -fill both
The general form for creating a widget is:
This command command creates the pathName widget and returns a new Tcl command whose name is pathName. If pdfObj is not specified, then the widget starts 'empty', with no attached PDF. You should then attach a pdfObj later (see below).
Note that a mupdf::widget starts with a basic set of interaction-controls (bindings) for scrolling the pages, selecting text and annotations; it's your responsability to choose whick kind of UI you want to implement (menus, buttons) and how to respond to some custom events (text/annotation selection) by binding these controls and events with the basic actions mupdf::widget provides.
The mupdf::widget command creates a widget and a new Tcl command whose name is pathName. pathName may be used to invoke various operations on the widget. It has the following general form:
The following subCommands are available for the widget pathName:
return the currently attached pdfObj or {}.
break the link with the attached pdfObj (if any). An 'empty' page is displayed.
attach the pdfObj object (created by mupdf::open) and display the page pageNum or the closest one to pageNum. If pageNum is not specified, then page 0 (the first page) is displayed.
return the current value of the configuration option given by option. Option may have any of the values accepted by the mupdf::widget command.
Query or modify the configuration options of the widget. If no option is specified, returns a list describing all of the available options for pathName (see Tk_ConfigureInfo for information on the format of this list). If option is specified with no value, thena the command returns a list describing the one named option (this list will be identical to the corresponding sublist of the value returned if no option is specified). If one or more option-value pairs are specified, then the command modifies the given widget option(s) to have the given value(s); in this case the command returns an empty string. Option may have any of the values accepted by the mupdf::widget command.
return a list of two integer representing the width and height (in pixel) of the zoomed page including the -extramargin. Note that this size is just the 'virtual' size of the zoomed page since just the visible area of the page (plus some extra 'margin') is rendered as a bitmap by the the underling mupdf engine.
creates a new mupdf::widget named newPath as a clone of pathName.
get the current zoom-factor
set the current zoom-factor
set the optimal zoom-factor for displaying the page. mode can be x (best-width) y (best-height) xy (best fit).
multiply-or-divide the zoom-factor by the -zoomratio coefficient. If delta is positive, zoom-factor is multiplied, else it's divided
align the center of the page with the center of the window. mode can be x, y or xy.
align the page on a side of the window. side can be top, bottom, left or right.
scroll the viewport by dWx,dWy pixels.
The scan subcommands is used to implement scanning. See the scan subcommand of the canvas widget.
These subcommands are used to query and change the horizontal/vertical position of the page displayed in the window. They are usually used for interacting with scrollbars. See the xview,yview subcommands of the canvas widget.
get the current page number (first page is 0).
show the page pagenumber (first page is 0). This subcommand returns an error if pagenumber is out of bounds.
show the next page. This subcommand returns false if there's not a next page, else it returns true.
show the previous page. This subcommand returns false if there's not a previous page, else it returns true.
start searching text and highlight all its occurrencies starting from the current page, or from page n if specified. If option -currpageonly is true, the the search is limited to the current page (or from the page specificed as -startpage).
reset the text-to-search.
get the currently set text-to-search.
turn off the highlights on the currently selected text.
This method should be used only after an existing annotation has been selected, i.e. in response to the <<MuPDF.AnnotSelected>> event.
This method should be used only after receiving the event <<MuPDF.TextSelected>>. The selected text is transformed in a pdf-annotation. type must be highlight, underline, strikeout or squiggly. color must be a symbolic color (eg, red, ligthblue, ,,,) or #rrggbb
This method should be used only after an existing annotation has been selected, i.e. in response to the <<MuPDF.AnnotSelected>> event. color must be a symbolic color (eg, red, lightblue, ,,,) or #rrggbb
This method should be used only after an existing annotation has been selected, i.e. in response to the <<MuPDF.AnnotSelected>> event.
This method should be used only after an existing annotation has been selected, i.e. in response to the <<MuPDF.AnnotSelected>> event.
given a screen point (relative to the origin of the pathName widget), return the corresponding point of the PDF page (in 'points' unit), accordling to the MuPDF coord-sys where (0,0) is the top-left corner of the page.
given a screen point (relative to the origin of the pathName widget), return the corresponding point of the PDF page (in 'points' unit), accordling to the standard PDF coord-sys where (0,0) is the bottom-left corner of the page.
mupdf::widget provides only basic built-in bindings for scrolling the page. The behavior of mupdf::widget can be enhanced by defining new bindings for individual widgets or by redefining the class bindings.
The arrows-keys can be used for scrolling a large page as do the mousewheel or two-finger gesture on a touchpad.
On Windows you can also use an horizontal two-finger gesture for horizontal scrolling; on other platforms you should use <shift>+<mousewheel>.
mupdf::widget subcommands may be associated with different kind of UI controls, such as buttons, menus, or they may be bound to events.
It is recommended to redefine and add new bindings at widget-class level. The name of the widget-class for mupdf::widget is MuPDFWidget.
Just few examples:
Associate the zoomfit subcommand with the keyboard's key "Z"
bind MuPDFWidget <Key-z> { %W zoomfit xy }
Change the page using the PageUp, PageDown keys
bind MuPDFWidget <Key-Next> { %W nextpage } bind MuPDFWidget <Key-Prior> { %W prevpage }
MUPDFWidget defines a few virtual events for the purposes of notification.
Any time the widget is resized, the virtual event <<MuPDF.Configured>> will be generated.
Any time the current page is changed, the virtual event <<MuPDF.PageLoaded>> will be generated. Binding scripts can use the %d value for getting the new page-number.
Any time an annotation is selected, the virtual event <<MuPDF.AnnotSelected>> will be generated. Binding scripts can use the %x and %y value for getting the coords of the selection, and the %d value for getting the annotation-type.
Any time a text is selected, the virtual event <<MuPDF.TextSelected>> will be generated.
Starting with mupdf::widget 2.0, programmers were able to create multiple instances of mupdf::widget even working on the same PDF (e.g. two views on the same PDF).
This worked but with one severe limitation: changes made through one view (e.g. add/change/delete annotations) weren't propagated to other views; as a matter of fact others views were kept in sync only after a full-page reload.
Starting with mupdf::widget 2.2, all the widget instances working on the same PDF are kept in sync, so that changes made in one view (add/change/remove annotations..) are instantly propagated to all the widget instances working on the same doc.
PDF, Snit, widget
widgets
Copyright © 2021, by A.Buratti