]>
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 acrosoft
= [r
'SOFTWARE\Adobe\Acrobat Reader\%version%\InstallPath',
26 r
'SOFTWARE\Adobe\Adobe Acrobat\%version%\InstallPath',]
28 for regkey
in acrosoft
:
29 for version
in ('7.0', '6.0', '5.0', '4.0'):
31 path
= _winreg
.QueryValue(_winreg
.HKEY_LOCAL_MACHINE
,
32 regkey
.replace('%version%', version
))
33 _acroversion
= version
39 #----------------------------------------------------------------------
41 # The ActiveX module from Acrobat 7.0 has changed and it seems to now
42 # require much more from the container than what our wx.activex module
43 # provides. If we try to use it via wx.activex then Acrobat crashes.
44 # So instead we will use Internet Explorer (via the win32com modules
45 # so we can use better dynamic dispatch) and embed the Acrobat control
46 # within that. Acrobat provides the IAcroAXDocShim COM class that is
47 # accessible via the IE window's Document property after a PDF file
50 # Details of the Acrobat reader methods can be found in
51 # http://partners.adobe.com/public/developer/en/acrobat/sdk/pdf/iac/IACOverview.pdf
52 # http://partners.adobe.com/public/developer/en/acrobat/sdk/pdf/iac/IACReference.pdf
54 # Co-ordinates passed as parameters are in points (1/72 inch).
57 if get_acroversion() >= '7.0':
59 from wx
.lib
.activexwrapper
import MakeActiveXClass
60 import win32com
.client
.gencache
61 _browserModule
= win32com
.client
.gencache
.EnsureModule(
62 "{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1)
64 class PDFWindowError(RuntimeError):
66 RuntimeError.__init
__(self
, "A PDF must be loaded before calling this method.")
69 class PDFWindow(wx
.Panel
):
70 def __init__(self
, *args
, **kw
):
71 wx
.Panel
.__init
__(self
, *args
, **kw
)
73 # Make a new class that derives from the WebBrowser class
74 # in the COM module imported above. This class also
75 # derives from wxWindow and implements the machinery
76 # needed to integrate the two worlds.
77 _WebBrowserClass
= MakeActiveXClass(_browserModule
.WebBrowser
)
78 self
.ie
= _WebBrowserClass(self
, -1)
80 sizer
.Add(self
.ie
, 1, wx
.EXPAND
)
84 def LoadFile(self
, fileName
):
86 Opens and displays the specified document within the browser.
89 return self
.ie
.Document
.LoadFile(fileName
)
91 self
.ie
.Navigate2(fileName
)
92 return True # can we sense failure at this point?
94 def GetVersions(self
):
96 Deprecated: No longer available - do not use.
99 return self
.ie
.Document
.GetVersions()
101 raise PDFWindowError()
105 Prints the document according to the specified options in a user dialog box.
108 return self
.ie
.Document
.Print()
110 raise PDFWindowError()
112 def goBackwardStack(self
):
114 Goes to the previous view on the view stack, if it exists.
117 return self
.ie
.Document
.goBackwardStack()
119 raise PDFWindowError()
121 def goForwardStack(self
):
123 Goes to the next view on the view stack, if it exists.
126 return self
.ie
.Document
.goForwardStack()
128 raise PDFWindowError()
130 def gotoFirstPage(self
):
132 Goes to the first page in the document.
135 return self
.ie
.Document
.gotoFirstPage()
137 raise PDFWindowError()
139 def gotoLastPage(self
):
141 Goes to the last page in the document.
144 return self
.ie
.Document
.gotoLastPage()
146 raise PDFWindowError()
148 def gotoNextPage(self
):
150 Goes to the next page in the document, if it exists
153 return self
.ie
.Document
.gotoNextPage()
155 raise PDFWindowError()
157 def gotoPreviousPage(self
):
159 Goes to the previous page in the document, if it exists.
162 return self
.ie
.Document
.gotoPreviousPage()
164 raise PDFWindowError()
168 Prints the entire document without displaying a user
169 dialog box. The current printer, page settings, and job
170 settings are used. This method returns immediately, even
171 if the printing has not completed.
174 return self
.ie
.Document
.printAll()
176 raise PDFWindowError()
178 def printAllFit(self
, shrinkToFit
):
180 Prints the entire document without a user dialog box, and
181 (if shrinkToFit) shrinks pages as needed to fit the
182 imageable area of a page in the printer.
185 return self
.ie
.Document
.printAllFit(shrinkToFit
)
187 raise PDFWindowError()
189 def printPages(self
, from_
, to
):
191 Prints the specified pages without displaying a user dialog box.
194 return self
.ie
.Document
.printPages(from_
, to
)
196 raise PDFWindowError()
198 def printPagesFit(self
, from_
, to
, shrinkToFit
):
200 Prints the specified pages without displaying a user
201 dialog box, and (if shrinkToFit) shrinks pages as needed
202 to fit the imageable area of a page in the printer.
205 return self
.ie
.Document
.printPagesFit( from_
, to
, shrinkToFit
)
207 raise PDFWindowError()
209 def printWithDialog(self
):
211 Prints the document according to the specified options in
212 a user dialog box. These options may include embedded
213 printing and specifying which printer is to be used.
215 NB. The page range in the dialog defaults to
216 'From Page 1 to 1' - Use Print() above instead. (dfh)
219 return self
.ie
.Document
.printWithDialog()
221 raise PDFWindowError()
223 def setCurrentHighlight(self
, a
, b
, c
, d
):
225 return self
.ie
.Document
.setCurrentHighlight(a
, b
, c
, d
)
227 raise PDFWindowError()
229 def setCurrentPage(self
, npage
):
231 Goes to the specified page in the document. Maintains the
232 current location within the page and zoom level. npage is
233 the page number of the destination page. The first page
234 in a document is page 0.
236 ## Oh no it isn't! The first page is 1 (dfh)
239 return self
.ie
.Document
.setCurrentPage(npage
)
241 raise PDFWindowError()
243 def setLayoutMode(self
, layoutMode
):
245 LayoutMode possible values:
247 ================= ====================================
248 'DontCare' use the current user preference
249 'SinglePage' use single page mode (as in pre-Acrobat
251 'OneColumn' use one-column continuous mode
252 'TwoColumnLeft' use two-column continuous mode, first
254 'TwoColumnRight' use two-column continuous mode, first
256 ================= ====================================
259 return self
.ie
.Document
.setLayoutMode(layoutMode
)
261 raise PDFWindowError()
263 def setNamedDest(self
, namedDest
):
265 Changes the page view to the named destination in the specified string.
268 return self
.ie
.Document
.setNamedDest(namedDest
)
270 raise PDFWindowError()
272 def setPageMode(self
, pageMode
):
274 Sets the page mode to display the document only, or to
275 additionally display bookmarks or thumbnails. pageMode =
276 'none' or 'bookmarks' or 'thumbs'.
278 ## NB.'thumbs' is case-sensitive, the other are not (dfh)
281 return self
.ie
.Document
.setPageMode(pageMode
)
283 raise PDFWindowError()
285 def setShowScrollbars(self
, On
):
287 Determines whether scrollbars will appear in the document
290 ## NB. If scrollbars are off, the navigation tools disappear as well (dfh)
293 return self
.ie
.Document
.setShowScrollbars(On
)
295 raise PDFWindowError()
297 def setShowToolbar(self
, On
):
299 Determines whether a toolbar will appear in the application.
302 return self
.ie
.Document
.setShowToolbar(On
)
304 raise PDFWindowError()
306 def setView(self
, viewMode
):
308 Determines how the page will fit in the current view.
309 viewMode possible values:
311 ======== ==============================================
312 'Fit' fits whole page within the window both vertically
314 'FitH' fits the width of the page within the window.
315 'FitV' fits the height of the page within the window.
316 'FitB' fits bounding box within the window both vertically
318 'FitBH' fits the width of the bounding box within the window.
319 'FitBV' fits the height of the bounding box within the window.
320 ======== ==============================================
323 return self
.ie
.Document
.setView(viewMode
)
325 raise PDFWindowError()
327 def setViewRect(self
, left
, top
, width
, height
):
329 Sets the view rectangle according to the specified coordinates.
331 :param left: The upper left horizontal coordinate.
332 :param top: The vertical coordinate in the upper left corner.
333 :param width: The horizontal width of the rectangle.
334 :param height: The vertical height of the rectangle.
337 return self
.ie
.Document
.setViewRect(left
, top
, width
, height
)
339 raise PDFWindowError()
341 def setViewScroll(self
, viewMode
, offset
):
343 Sets the view of a page according to the specified string.
344 Depending on the view mode, the page is either scrolled to
345 the right or scrolled down by the amount specified in
346 offset. Possible values of viewMode are as in setView
347 above. offset is the horizontal or vertical coordinate
348 positioned either at the left or top edge.
351 return self
.ie
.Document
.setViewScroll(viewMode
, offset
)
353 raise PDFWindowError()
355 def setZoom(self
, percent
):
357 Sets the magnification according to the specified value
358 expressed as a percentage (float)
361 return self
.ie
.Document
.setZoom(percent
)
363 raise PDFWindowError()
365 def setZoomScroll(self
, percent
, left
, top
):
367 Sets the magnification according to the specified value,
368 and scrolls the page view both horizontally and vertically
369 according to the specified amounts.
371 :param left: the horizontal coordinate positioned at the left edge.
372 :param top: the vertical coordinate positioned at the top edge.
375 return self
.ie
.Document
.setZoomScroll(percent
, left
, top
)
377 raise PDFWindowError()
384 clsID
= '{CA8A9780-280D-11CF-A24D-444553540000}'
385 progID
= 'AcroPDF.PDF.1'
388 # Create eventTypes and event binders
389 wxEVT_Error
= wx
.activex
.RegisterActiveXEvent('OnError')
390 wxEVT_Message
= wx
.activex
.RegisterActiveXEvent('OnMessage')
392 EVT_Error
= wx
.PyEventBinder(wxEVT_Error
, 1)
393 EVT_Message
= wx
.PyEventBinder(wxEVT_Message
, 1)
396 # Derive a new class from ActiveXWindow
397 class PDFWindow(wx
.activex
.ActiveXWindow
):
398 def __init__(self
, parent
, ID
=-1, pos
=wx
.DefaultPosition
,
399 size
=wx
.DefaultSize
, style
=0, name
='PDFWindow'):
400 wx
.activex
.ActiveXWindow
.__init
__(self
, parent
,
401 wx
.activex
.CLSID('{CA8A9780-280D-11CF-A24D-444553540000}'),
402 ID
, pos
, size
, style
, name
)
404 # Methods exported by the ActiveX object
405 def QueryInterface(self
, riid
):
406 return self
.CallAXMethod('QueryInterface', riid
)
409 return self
.CallAXMethod('AddRef')
412 return self
.CallAXMethod('Release')
414 def GetTypeInfoCount(self
):
415 return self
.CallAXMethod('GetTypeInfoCount')
417 def GetTypeInfo(self
, itinfo
, lcid
):
418 return self
.CallAXMethod('GetTypeInfo', itinfo
, lcid
)
420 def GetIDsOfNames(self
, riid
, rgszNames
, cNames
, lcid
):
421 return self
.CallAXMethod('GetIDsOfNames', riid
, rgszNames
, cNames
, lcid
)
423 def Invoke(self
, dispidMember
, riid
, lcid
, wFlags
, pdispparams
):
424 return self
.CallAXMethod('Invoke', dispidMember
, riid
, lcid
, wFlags
, pdispparams
)
426 def LoadFile(self
, fileName
):
427 return self
.CallAXMethod('LoadFile', fileName
)
429 def setShowToolbar(self
, On
):
430 return self
.CallAXMethod('setShowToolbar', On
)
432 def gotoFirstPage(self
):
433 return self
.CallAXMethod('gotoFirstPage')
435 def gotoLastPage(self
):
436 return self
.CallAXMethod('gotoLastPage')
438 def gotoNextPage(self
):
439 return self
.CallAXMethod('gotoNextPage')
441 def gotoPreviousPage(self
):
442 return self
.CallAXMethod('gotoPreviousPage')
444 def setCurrentPage(self
, n
):
445 return self
.CallAXMethod('setCurrentPage', n
)
447 def goForwardStack(self
):
448 return self
.CallAXMethod('goForwardStack')
450 def goBackwardStack(self
):
451 return self
.CallAXMethod('goBackwardStack')
453 def setPageMode(self
, pageMode
):
454 return self
.CallAXMethod('setPageMode', pageMode
)
456 def setLayoutMode(self
, layoutMode
):
457 return self
.CallAXMethod('setLayoutMode', layoutMode
)
459 def setNamedDest(self
, namedDest
):
460 return self
.CallAXMethod('setNamedDest', namedDest
)
463 return self
.CallAXMethod('Print')
465 def printWithDialog(self
):
466 return self
.CallAXMethod('printWithDialog')
468 def setZoom(self
, percent
):
469 return self
.CallAXMethod('setZoom', percent
)
471 def setZoomScroll(self
, percent
, left
, top
):
472 return self
.CallAXMethod('setZoomScroll', percent
, left
, top
)
474 def setView(self
, viewMode
):
475 return self
.CallAXMethod('setView', viewMode
)
477 def setViewScroll(self
, viewMode
, offset
):
478 return self
.CallAXMethod('setViewScroll', viewMode
, offset
)
480 def setViewRect(self
, left
, top
, width
, height
):
481 return self
.CallAXMethod('setViewRect', left
, top
, width
, height
)
483 def printPages(self
, from_
, to
):
484 return self
.CallAXMethod('printPages', from_
, to
)
486 def printPagesFit(self
, from_
, to
, shrinkToFit
):
487 return self
.CallAXMethod('printPagesFit', from_
, to
, shrinkToFit
)
490 return self
.CallAXMethod('printAll')
492 def printAllFit(self
, shrinkToFit
):
493 return self
.CallAXMethod('printAllFit', shrinkToFit
)
495 def setShowScrollbars(self
, On
):
496 return self
.CallAXMethod('setShowScrollbars', On
)
498 def GetVersions(self
):
499 return self
.CallAXMethod('GetVersions')
501 def setCurrentHighlight(self
, a
, b
, c
, d
):
502 return self
.CallAXMethod('setCurrentHighlight', a
, b
, c
, d
)
504 def postMessage(self
, strArray
):
505 return self
.CallAXMethod('postMessage', strArray
)
507 # Getters, Setters and properties
509 return self
.GetAXProp('src')
510 def _set_src(self
, src
):
511 self
.SetAXProp('src', src
)
512 src
= property(_get_src
, _set_src
)
514 def _get_messageHandler(self
):
515 return self
.GetAXProp('messageHandler')
516 def _set_messageHandler(self
, messageHandler
):
517 self
.SetAXProp('messageHandler', messageHandler
)
518 messagehandler
= property(_get_messageHandler
, _set_messageHandler
)
522 # --------------------
524 # type:string arg:string canGet:True canSet:True
527 # type:VT_VARIANT arg:VT_VARIANT canGet:True canSet:True
533 # --------------------
538 # in:True out:False optional:False type:unsupported type 29
540 # in:False out:True optional:False type:unsupported type 26
552 # in:False out:True optional:False type:int
558 # in:True out:False optional:False type:int
560 # in:True out:False optional:False type:int
562 # in:False out:True optional:False type:unsupported type 26
568 # in:True out:False optional:False type:unsupported type 29
570 # in:True out:False optional:False type:unsupported type 26
572 # in:True out:False optional:False type:int
574 # in:True out:False optional:False type:int
576 # in:False out:True optional:False type:int
582 # in:True out:False optional:False type:int
584 # in:True out:False optional:False type:unsupported type 29
586 # in:True out:False optional:False type:int
588 # in:True out:False optional:False type:int
590 # in:True out:False optional:False type:unsupported type 29
592 # in:False out:True optional:False type:VT_VARIANT
594 # in:False out:True optional:False type:unsupported type 29
596 # in:False out:True optional:False type:int
602 # in:True out:False optional:False type:string
608 # in:True out:False optional:False type:bool
626 # in:True out:False optional:False type:int
638 # in:True out:False optional:False type:string
644 # in:True out:False optional:False type:string
650 # in:True out:False optional:False type:string
662 # in:True out:False optional:False type:double
668 # in:True out:False optional:False type:double
670 # in:True out:False optional:False type:double
672 # in:True out:False optional:False type:double
678 # in:True out:False optional:False type:string
684 # in:True out:False optional:False type:string
686 # in:True out:False optional:False type:double
692 # in:True out:False optional:False type:double
694 # in:True out:False optional:False type:double
696 # in:True out:False optional:False type:double
698 # in:True out:False optional:False type:double
704 # in:True out:False optional:False type:int
706 # in:True out:False optional:False type:int
712 # in:True out:False optional:False type:int
714 # in:True out:False optional:False type:int
716 # in:True out:False optional:False type:bool
725 # in:True out:False optional:False type:bool
731 # in:True out:False optional:False type:bool
734 # retType: VT_VARIANT
736 # setCurrentHightlight
740 # in:True out:False optional:False type:int
742 # in:True out:False optional:False type:int
744 # in:True out:False optional:False type:int
746 # in:True out:False optional:False type:int
748 # setCurrentHighlight
752 # in:True out:False optional:False type:int
754 # in:True out:False optional:False type:int
756 # in:True out:False optional:False type:int
758 # in:True out:False optional:False type:int
764 # in:True out:False optional:False type:VT_VARIANT
770 # --------------------