]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/pdfwin.py
1 #----------------------------------------------------------------------
3 # Purpose: A class that allows the use of the Acrobat PDF reader
8 # Created: 22-March-2004
10 # Copyright: (c) 2004 by Total Control Software
11 # Licence: wxWindows license
12 #----------------------------------------------------------------------
16 #----------------------------------------------------------------------
20 def get_acroversion():
21 " Return version of Adobe Acrobat executable or None"
23 if _acroversion
== None:
25 adobesoft
= _winreg
.OpenKey(_winreg
.HKEY_LOCAL_MACHINE
, r
'Software\Adobe')
26 acrokeys
, acroversions
= [], []
27 for index
in range(_winreg
.QueryInfoKey(adobesoft
)[0]):
28 key
= _winreg
.EnumKey(adobesoft
, index
)
29 if "acrobat" in key
.lower():
30 acrokeys
.append(_winreg
.OpenKey(_winreg
.HKEY_LOCAL_MACHINE
, 'Software\\Adobe\\%s' % key
))
31 for acrokey
in acrokeys
:
32 for index
in range(_winreg
.QueryInfoKey(acrokey
)[0]):
33 key
= _winreg
.EnumKey(acrokey
, index
)
35 acroversions
.append(float(key
))
38 acroversions
.sort(reverse
=True)
40 _acroversion
= acroversions
[0]
44 #----------------------------------------------------------------------
46 # The ActiveX module from Acrobat 7.0 has changed and it seems to now
47 # require much more from the container than what our wx.activex module
48 # provides. If we try to use it via wx.activex then Acrobat crashes.
49 # So instead we will use Internet Explorer (via the win32com modules
50 # so we can use better dynamic dispatch) and embed the Acrobat control
51 # within that. Acrobat provides the IAcroAXDocShim COM class that is
52 # accessible via the IE window's Document property after a PDF file
55 # Details of the Acrobat reader methods can be found in
56 # http://partners.adobe.com/public/developer/en/acrobat/sdk/pdf/iac/IACOverview.pdf
57 # http://partners.adobe.com/public/developer/en/acrobat/sdk/pdf/iac/IACReference.pdf
59 # Co-ordinates passed as parameters are in points (1/72 inch).
62 if get_acroversion() >= 7.0:
64 from wx
.lib
.activexwrapper
import MakeActiveXClass
65 import win32com
.client
.gencache
66 _browserModule
= win32com
.client
.gencache
.EnsureModule(
67 "{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1)
69 class PDFWindowError(RuntimeError):
71 RuntimeError.__init
__(self
, "A PDF must be loaded before calling this method.")
74 class PDFWindow(wx
.Panel
):
75 def __init__(self
, *args
, **kw
):
76 wx
.Panel
.__init
__(self
, *args
, **kw
)
78 # Make a new class that derives from the WebBrowser class
79 # in the COM module imported above. This class also
80 # derives from wxWindow and implements the machinery
81 # needed to integrate the two worlds.
82 _WebBrowserClass
= MakeActiveXClass(_browserModule
.WebBrowser
)
83 self
.ie
= _WebBrowserClass(self
, -1)
85 sizer
.Add(self
.ie
, 1, wx
.EXPAND
)
89 def LoadFile(self
, fileName
):
91 Opens and displays the specified document within the browser.
94 return self
.ie
.Document
.LoadFile(fileName
)
96 self
.ie
.Navigate2(fileName
)
97 return True # can we sense failure at this point?
99 def GetVersions(self
):
101 Deprecated: No longer available - do not use.
104 return self
.ie
.Document
.GetVersions()
106 raise PDFWindowError()
110 Prints the document according to the specified options in a user dialog box.
113 return self
.ie
.Document
.Print()
115 raise PDFWindowError()
117 def goBackwardStack(self
):
119 Goes to the previous view on the view stack, if it exists.
122 return self
.ie
.Document
.goBackwardStack()
124 raise PDFWindowError()
126 def goForwardStack(self
):
128 Goes to the next view on the view stack, if it exists.
131 return self
.ie
.Document
.goForwardStack()
133 raise PDFWindowError()
135 def gotoFirstPage(self
):
137 Goes to the first page in the document.
140 return self
.ie
.Document
.gotoFirstPage()
142 raise PDFWindowError()
144 def gotoLastPage(self
):
146 Goes to the last page in the document.
149 return self
.ie
.Document
.gotoLastPage()
151 raise PDFWindowError()
153 def gotoNextPage(self
):
155 Goes to the next page in the document, if it exists
158 return self
.ie
.Document
.gotoNextPage()
160 raise PDFWindowError()
162 def gotoPreviousPage(self
):
164 Goes to the previous page in the document, if it exists.
167 return self
.ie
.Document
.gotoPreviousPage()
169 raise PDFWindowError()
173 Prints the entire document without displaying a user
174 dialog box. The current printer, page settings, and job
175 settings are used. This method returns immediately, even
176 if the printing has not completed.
179 return self
.ie
.Document
.printAll()
181 raise PDFWindowError()
183 def printAllFit(self
, shrinkToFit
):
185 Prints the entire document without a user dialog box, and
186 (if shrinkToFit) shrinks pages as needed to fit the
187 imageable area of a page in the printer.
190 return self
.ie
.Document
.printAllFit(shrinkToFit
)
192 raise PDFWindowError()
194 def printPages(self
, from_
, to
):
196 Prints the specified pages without displaying a user dialog box.
199 return self
.ie
.Document
.printPages(from_
, to
)
201 raise PDFWindowError()
203 def printPagesFit(self
, from_
, to
, shrinkToFit
):
205 Prints the specified pages without displaying a user
206 dialog box, and (if shrinkToFit) shrinks pages as needed
207 to fit the imageable area of a page in the printer.
210 return self
.ie
.Document
.printPagesFit( from_
, to
, shrinkToFit
)
212 raise PDFWindowError()
214 def printWithDialog(self
):
216 Prints the document according to the specified options in
217 a user dialog box. These options may include embedded
218 printing and specifying which printer is to be used.
220 NB. The page range in the dialog defaults to
221 'From Page 1 to 1' - Use Print() above instead. (dfh)
224 return self
.ie
.Document
.printWithDialog()
226 raise PDFWindowError()
228 def setCurrentHighlight(self
, a
, b
, c
, d
):
230 return self
.ie
.Document
.setCurrentHighlight(a
, b
, c
, d
)
232 raise PDFWindowError()
234 def setCurrentPage(self
, npage
):
236 Goes to the specified page in the document. Maintains the
237 current location within the page and zoom level. npage is
238 the page number of the destination page. The first page
239 in a document is page 0.
241 ## Oh no it isn't! The first page is 1 (dfh)
244 return self
.ie
.Document
.setCurrentPage(npage
)
246 raise PDFWindowError()
248 def setLayoutMode(self
, layoutMode
):
250 LayoutMode possible values:
252 ================= ====================================
253 'DontCare' use the current user preference
254 'SinglePage' use single page mode (as in pre-Acrobat
256 'OneColumn' use one-column continuous mode
257 'TwoColumnLeft' use two-column continuous mode, first
259 'TwoColumnRight' use two-column continuous mode, first
261 ================= ====================================
264 return self
.ie
.Document
.setLayoutMode(layoutMode
)
266 raise PDFWindowError()
268 def setNamedDest(self
, namedDest
):
270 Changes the page view to the named destination in the specified string.
273 return self
.ie
.Document
.setNamedDest(namedDest
)
275 raise PDFWindowError()
277 def setPageMode(self
, pageMode
):
279 Sets the page mode to display the document only, or to
280 additionally display bookmarks or thumbnails. pageMode =
281 'none' or 'bookmarks' or 'thumbs'.
283 ## NB.'thumbs' is case-sensitive, the other are not (dfh)
286 return self
.ie
.Document
.setPageMode(pageMode
)
288 raise PDFWindowError()
290 def setShowScrollbars(self
, On
):
292 Determines whether scrollbars will appear in the document
295 ## NB. If scrollbars are off, the navigation tools disappear as well (dfh)
298 return self
.ie
.Document
.setShowScrollbars(On
)
300 raise PDFWindowError()
302 def setShowToolbar(self
, On
):
304 Determines whether a toolbar will appear in the application.
307 return self
.ie
.Document
.setShowToolbar(On
)
309 raise PDFWindowError()
311 def setView(self
, viewMode
):
313 Determines how the page will fit in the current view.
314 viewMode possible values:
316 ======== ==============================================
317 'Fit' fits whole page within the window both vertically
319 'FitH' fits the width of the page within the window.
320 'FitV' fits the height of the page within the window.
321 'FitB' fits bounding box within the window both vertically
323 'FitBH' fits the width of the bounding box within the window.
324 'FitBV' fits the height of the bounding box within the window.
325 ======== ==============================================
328 return self
.ie
.Document
.setView(viewMode
)
330 raise PDFWindowError()
332 def setViewRect(self
, left
, top
, width
, height
):
334 Sets the view rectangle according to the specified coordinates.
336 :param left: The upper left horizontal coordinate.
337 :param top: The vertical coordinate in the upper left corner.
338 :param width: The horizontal width of the rectangle.
339 :param height: The vertical height of the rectangle.
342 return self
.ie
.Document
.setViewRect(left
, top
, width
, height
)
344 raise PDFWindowError()
346 def setViewScroll(self
, viewMode
, offset
):
348 Sets the view of a page according to the specified string.
349 Depending on the view mode, the page is either scrolled to
350 the right or scrolled down by the amount specified in
351 offset. Possible values of viewMode are as in setView
352 above. offset is the horizontal or vertical coordinate
353 positioned either at the left or top edge.
356 return self
.ie
.Document
.setViewScroll(viewMode
, offset
)
358 raise PDFWindowError()
360 def setZoom(self
, percent
):
362 Sets the magnification according to the specified value
363 expressed as a percentage (float)
366 return self
.ie
.Document
.setZoom(percent
)
368 raise PDFWindowError()
370 def setZoomScroll(self
, percent
, left
, top
):
372 Sets the magnification according to the specified value,
373 and scrolls the page view both horizontally and vertically
374 according to the specified amounts.
376 :param left: the horizontal coordinate positioned at the left edge.
377 :param top: the vertical coordinate positioned at the top edge.
380 return self
.ie
.Document
.setZoomScroll(percent
, left
, top
)
382 raise PDFWindowError()
389 clsID
= '{CA8A9780-280D-11CF-A24D-444553540000}'
390 progID
= 'AcroPDF.PDF.1'
393 # Create eventTypes and event binders
394 wxEVT_Error
= wx
.activex
.RegisterActiveXEvent('OnError')
395 wxEVT_Message
= wx
.activex
.RegisterActiveXEvent('OnMessage')
397 EVT_Error
= wx
.PyEventBinder(wxEVT_Error
, 1)
398 EVT_Message
= wx
.PyEventBinder(wxEVT_Message
, 1)
401 # Derive a new class from ActiveXWindow
402 class PDFWindow(wx
.activex
.ActiveXWindow
):
403 def __init__(self
, parent
, ID
=-1, pos
=wx
.DefaultPosition
,
404 size
=wx
.DefaultSize
, style
=0, name
='PDFWindow'):
405 wx
.activex
.ActiveXWindow
.__init
__(self
, parent
,
406 wx
.activex
.CLSID('{CA8A9780-280D-11CF-A24D-444553540000}'),
407 ID
, pos
, size
, style
, name
)
409 # Methods exported by the ActiveX object
410 def QueryInterface(self
, riid
):
411 return self
.CallAXMethod('QueryInterface', riid
)
414 return self
.CallAXMethod('AddRef')
417 return self
.CallAXMethod('Release')
419 def GetTypeInfoCount(self
):
420 return self
.CallAXMethod('GetTypeInfoCount')
422 def GetTypeInfo(self
, itinfo
, lcid
):
423 return self
.CallAXMethod('GetTypeInfo', itinfo
, lcid
)
425 def GetIDsOfNames(self
, riid
, rgszNames
, cNames
, lcid
):
426 return self
.CallAXMethod('GetIDsOfNames', riid
, rgszNames
, cNames
, lcid
)
428 def Invoke(self
, dispidMember
, riid
, lcid
, wFlags
, pdispparams
):
429 return self
.CallAXMethod('Invoke', dispidMember
, riid
, lcid
, wFlags
, pdispparams
)
431 def LoadFile(self
, fileName
):
432 return self
.CallAXMethod('LoadFile', fileName
)
434 def setShowToolbar(self
, On
):
435 return self
.CallAXMethod('setShowToolbar', On
)
437 def gotoFirstPage(self
):
438 return self
.CallAXMethod('gotoFirstPage')
440 def gotoLastPage(self
):
441 return self
.CallAXMethod('gotoLastPage')
443 def gotoNextPage(self
):
444 return self
.CallAXMethod('gotoNextPage')
446 def gotoPreviousPage(self
):
447 return self
.CallAXMethod('gotoPreviousPage')
449 def setCurrentPage(self
, n
):
450 return self
.CallAXMethod('setCurrentPage', n
)
452 def goForwardStack(self
):
453 return self
.CallAXMethod('goForwardStack')
455 def goBackwardStack(self
):
456 return self
.CallAXMethod('goBackwardStack')
458 def setPageMode(self
, pageMode
):
459 return self
.CallAXMethod('setPageMode', pageMode
)
461 def setLayoutMode(self
, layoutMode
):
462 return self
.CallAXMethod('setLayoutMode', layoutMode
)
464 def setNamedDest(self
, namedDest
):
465 return self
.CallAXMethod('setNamedDest', namedDest
)
468 return self
.CallAXMethod('Print')
470 def printWithDialog(self
):
471 return self
.CallAXMethod('printWithDialog')
473 def setZoom(self
, percent
):
474 return self
.CallAXMethod('setZoom', percent
)
476 def setZoomScroll(self
, percent
, left
, top
):
477 return self
.CallAXMethod('setZoomScroll', percent
, left
, top
)
479 def setView(self
, viewMode
):
480 return self
.CallAXMethod('setView', viewMode
)
482 def setViewScroll(self
, viewMode
, offset
):
483 return self
.CallAXMethod('setViewScroll', viewMode
, offset
)
485 def setViewRect(self
, left
, top
, width
, height
):
486 return self
.CallAXMethod('setViewRect', left
, top
, width
, height
)
488 def printPages(self
, from_
, to
):
489 return self
.CallAXMethod('printPages', from_
, to
)
491 def printPagesFit(self
, from_
, to
, shrinkToFit
):
492 return self
.CallAXMethod('printPagesFit', from_
, to
, shrinkToFit
)
495 return self
.CallAXMethod('printAll')
497 def printAllFit(self
, shrinkToFit
):
498 return self
.CallAXMethod('printAllFit', shrinkToFit
)
500 def setShowScrollbars(self
, On
):
501 return self
.CallAXMethod('setShowScrollbars', On
)
503 def GetVersions(self
):
504 return self
.CallAXMethod('GetVersions')
506 def setCurrentHighlight(self
, a
, b
, c
, d
):
507 return self
.CallAXMethod('setCurrentHighlight', a
, b
, c
, d
)
509 def postMessage(self
, strArray
):
510 return self
.CallAXMethod('postMessage', strArray
)
512 # Getters, Setters and properties
514 return self
.GetAXProp('src')
515 def _set_src(self
, src
):
516 self
.SetAXProp('src', src
)
517 src
= property(_get_src
, _set_src
)
519 def _get_messageHandler(self
):
520 return self
.GetAXProp('messageHandler')
521 def _set_messageHandler(self
, messageHandler
):
522 self
.SetAXProp('messageHandler', messageHandler
)
523 messagehandler
= property(_get_messageHandler
, _set_messageHandler
)
527 # --------------------
529 # type:string arg:string canGet:True canSet:True
532 # type:VT_VARIANT arg:VT_VARIANT canGet:True canSet:True
538 # --------------------
543 # in:True out:False optional:False type:unsupported type 29
545 # in:False out:True optional:False type:unsupported type 26
557 # in:False out:True optional:False type:int
563 # in:True out:False optional:False type:int
565 # in:True out:False optional:False type:int
567 # in:False out:True optional:False type:unsupported type 26
573 # in:True out:False optional:False type:unsupported type 29
575 # in:True out:False optional:False type:unsupported type 26
577 # in:True out:False optional:False type:int
579 # in:True out:False optional:False type:int
581 # in:False out:True optional:False type:int
587 # in:True out:False optional:False type:int
589 # in:True out:False optional:False type:unsupported type 29
591 # in:True out:False optional:False type:int
593 # in:True out:False optional:False type:int
595 # in:True out:False optional:False type:unsupported type 29
597 # in:False out:True optional:False type:VT_VARIANT
599 # in:False out:True optional:False type:unsupported type 29
601 # in:False out:True optional:False type:int
607 # in:True out:False optional:False type:string
613 # in:True out:False optional:False type:bool
631 # in:True out:False optional:False type:int
643 # in:True out:False optional:False type:string
649 # in:True out:False optional:False type:string
655 # in:True out:False optional:False type:string
667 # in:True out:False optional:False type:double
673 # in:True out:False optional:False type:double
675 # in:True out:False optional:False type:double
677 # in:True out:False optional:False type:double
683 # in:True out:False optional:False type:string
689 # in:True out:False optional:False type:string
691 # in:True out:False optional:False type:double
697 # in:True out:False optional:False type:double
699 # in:True out:False optional:False type:double
701 # in:True out:False optional:False type:double
703 # in:True out:False optional:False type:double
709 # in:True out:False optional:False type:int
711 # in:True out:False optional:False type:int
717 # in:True out:False optional:False type:int
719 # in:True out:False optional:False type:int
721 # in:True out:False optional:False type:bool
730 # in:True out:False optional:False type:bool
736 # in:True out:False optional:False type:bool
739 # retType: VT_VARIANT
741 # setCurrentHightlight
745 # in:True out:False optional:False type:int
747 # in:True out:False optional:False type:int
749 # in:True out:False optional:False type:int
751 # in:True out:False optional:False type:int
753 # setCurrentHighlight
757 # in:True out:False optional:False type:int
759 # in:True out:False optional:False type:int
761 # in:True out:False optional:False type:int
763 # in:True out:False optional:False type:int
769 # in:True out:False optional:False type:VT_VARIANT
775 # --------------------