1 | # Read PDF files by embeding the Adobe Acrobat Reader |
---|
2 | # wx.activex module uses class ActiveX control |
---|
3 | |
---|
4 | import wx |
---|
5 | import os |
---|
6 | if wx.Platform == '__WXMSW__': |
---|
7 | from wx.lib.pdfwin import PDFWindow |
---|
8 | |
---|
9 | class PDFPanel(wx.Panel): |
---|
10 | """ |
---|
11 | Panel that contains the pdf reader |
---|
12 | """ |
---|
13 | def __init__(self, parent, path=None): |
---|
14 | """ |
---|
15 | """ |
---|
16 | wx.Panel.__init__(self, parent, id=-1) |
---|
17 | |
---|
18 | self.parent = parent |
---|
19 | self.path = path |
---|
20 | sizer = wx.BoxSizer(wx.VERTICAL) |
---|
21 | btnSizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
22 | |
---|
23 | self.pdf = PDFWindow(self, style=wx.SUNKEN_BORDER) |
---|
24 | |
---|
25 | sizer.Add(self.pdf, proportion=1, flag=wx.EXPAND) |
---|
26 | |
---|
27 | btn = wx.Button(self, wx.NewId(), "Open PDF File") |
---|
28 | self.Bind(wx.EVT_BUTTON, self.OnOpenButton, btn) |
---|
29 | btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5) |
---|
30 | |
---|
31 | self.pdf.LoadFile(self.path) |
---|
32 | btn = wx.Button(self, wx.NewId(), "Previous Page") |
---|
33 | self.Bind(wx.EVT_BUTTON, self.OnPrevPageButton, btn) |
---|
34 | btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5) |
---|
35 | |
---|
36 | btn = wx.Button(self, wx.NewId(), "Next Page") |
---|
37 | self.Bind(wx.EVT_BUTTON, self.OnNextPageButton, btn) |
---|
38 | btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5) |
---|
39 | |
---|
40 | btn = wx.Button(self, wx.NewId(), "Close") |
---|
41 | self.Bind(wx.EVT_BUTTON, self.OnClose, btn) |
---|
42 | btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5) |
---|
43 | btnSizer.Add((50,-1), proportion=2, flag=wx.EXPAND) |
---|
44 | sizer.Add(btnSizer, proportion=0, flag=wx.EXPAND) |
---|
45 | |
---|
46 | self.SetSizer(sizer) |
---|
47 | self.SetAutoLayout(True) |
---|
48 | |
---|
49 | def OnOpenButton(self, event): |
---|
50 | """ |
---|
51 | Open file button |
---|
52 | """ |
---|
53 | # make sure you have PDF files available on your drive |
---|
54 | dlg = wx.FileDialog(self, wildcard="*.pdf") |
---|
55 | dlg.SetDirectory(os.path.dirname(self.path)) |
---|
56 | if dlg.ShowModal() == wx.ID_OK: |
---|
57 | wx.BeginBusyCursor() |
---|
58 | file = dlg.GetPath() |
---|
59 | self.pdf.LoadFile(file) |
---|
60 | self.parent.SetTitle(os.path.basename(file.split('.')[0])) |
---|
61 | wx.EndBusyCursor() |
---|
62 | dlg.Destroy() |
---|
63 | |
---|
64 | def OnLoad(self, event=None, path=None): |
---|
65 | """ |
---|
66 | Load a pdf file |
---|
67 | |
---|
68 | : Param path: full path to the file |
---|
69 | """ |
---|
70 | self.pdf.LoadFile(path) |
---|
71 | |
---|
72 | |
---|
73 | def OnPrevPageButton(self, event): |
---|
74 | """ |
---|
75 | Goes to Previous page |
---|
76 | """ |
---|
77 | self.pdf.gotoPreviousPage() |
---|
78 | |
---|
79 | def OnNextPageButton(self, event): |
---|
80 | """ |
---|
81 | Goes to Next page |
---|
82 | """ |
---|
83 | self.pdf.gotoNextPage() |
---|
84 | |
---|
85 | def OnClose(self, event): |
---|
86 | """ |
---|
87 | Close panel |
---|
88 | """ |
---|
89 | self.parent.Destroy() |
---|
90 | |
---|
91 | class PDFFrame(wx.Frame): |
---|
92 | """ |
---|
93 | Frame for PDF panel |
---|
94 | """ |
---|
95 | def __init__(self, parent, id, title, path): |
---|
96 | """ |
---|
97 | Init |
---|
98 | |
---|
99 | :param parent: parent panel/container |
---|
100 | :param path: full path of the pdf file |
---|
101 | """ |
---|
102 | # Initialize the Frame object |
---|
103 | wx.Frame.__init__(self, parent, id, title, |
---|
104 | wx.DefaultPosition, wx.Size(600, 830)) |
---|
105 | # make an instance of the class |
---|
106 | PDFPanel(self, path) |
---|
107 | |
---|
108 | class ViewApp(wx.App): |
---|
109 | def OnInit(self): |
---|
110 | path = None |
---|
111 | frame = PDFFrame(None, -1, "PDFView", path=path) |
---|
112 | |
---|
113 | frame.Show(True) |
---|
114 | #self.SetTopWindow(frame) |
---|
115 | |
---|
116 | return True |
---|
117 | |
---|
118 | if __name__ == "__main__": |
---|
119 | app = ViewApp(0) |
---|
120 | app.MainLoop() |
---|