Using the wx.lib.pdfwin.PDFWindow one can read Adobe PDF files (.pdf) with wxPython. The PDF file format is popular document file format allowing mixing of text and graphics. The GUI toolkit wxPython's newer wx.activex module allows one to use the ActiveX control, as if it would be one wx.Window. It actually embeds the Adobe Acrobat Reader into the wx.Window.
Read PDF Files with wxPython
# read PDF files (.pdf) with wxPython
# using wx.lib.pdfwin.PDFWindow class ActiveX control
# from wxPython's new wx.activex module, this allows one
# to use an ActiveX control, as if it would be wx.Window
# it embeds the Adobe Acrobat Reader
# as far as HB knows this works only with Windows
# tested with Python24 and wxPython26 by HB
import wx
if wx.Platform == '__WXMSW__':
from wx.lib.pdfwin import PDFWindow
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, id=-1)
self.pdf = None
sizer = wx.BoxSizer(wx.VERTICAL)
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
self.pdf = PDFWindow(self, style=wx.SUNKEN_BORDER)
sizer.Add(self.pdf, proportion=1, flag=wx.EXPAND)
btn = wx.Button(self, wx.NewId(), "Open PDF File")
self.Bind(wx.EVT_BUTTON, self.OnOpenButton, btn)
btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5)
btn = wx.Button(self, wx.NewId(), "Previous Page")
self.Bind(wx.EVT_BUTTON, self.OnPrevPageButton, btn)
btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5)
btn = wx.Button(self, wx.NewId(), "Next Page")
self.Bind(wx.EVT_BUTTON, self.OnNextPageButton, btn)
btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5)
btnSizer.Add((50,-1), proportion=2, flag=wx.EXPAND)
sizer.Add(btnSizer, proportion=0, flag=wx.EXPAND)
self.SetSizer(sizer)
self.SetAutoLayout(True)
def OnOpenButton(self, event):
# make sure you have PDF files available on your drive
dlg = wx.FileDialog(self, wildcard="*.pdf")
if dlg.ShowModal() == wx.ID_OK:
wx.BeginBusyCursor()
self.pdf.LoadFile(dlg.GetPath())
wx.EndBusyCursor()
dlg.Destroy()
def OnPrevPageButton(self, event):
self.pdf.gotoPreviousPage()
def OnNextPageButton(self, event):
self.pdf.gotoNextPage()
app = wx.PySimpleApp()
# create window/frame, no parent, -1 is default ID, title, size
frame = wx.Frame(None, -1, "PDFWindow", size = (640, 480))
# make an instance of the class
MyPanel(frame)
# show the frame
frame.Show(True)
# start the event loop
app.MainLoop()
richardtreier 0 Newbie Poster
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
richardtreier 0 Newbie Poster
Dilip101 0 Newbie Poster
srujana_1 0 Newbie Poster
Gribouillis 1,391 Programming Explorer Team Colleague
bucefala 0 Newbie Poster
Gribouillis 1,391 Programming Explorer Team Colleague
bucefala 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.