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 | wx.EVT_CLOSE(self.parent, self.OnClose) |
---|
49 | |
---|
50 | def OnOpenButton(self, event): |
---|
51 | """ |
---|
52 | Open file button |
---|
53 | """ |
---|
54 | # make sure you have PDF files available on your drive |
---|
55 | dlg = wx.FileDialog(self, wildcard="*.pdf") |
---|
56 | dlg.SetDirectory(os.path.dirname(self.path)) |
---|
57 | if dlg.ShowModal() == wx.ID_OK: |
---|
58 | wx.BeginBusyCursor() |
---|
59 | file = dlg.GetPath() |
---|
60 | self.pdf.LoadFile(file) |
---|
61 | self.parent.SetTitle(os.path.basename(file.split('.')[0])) |
---|
62 | wx.EndBusyCursor() |
---|
63 | dlg.Destroy() |
---|
64 | # Let Panel know the file changed: Avoiding C++ error |
---|
65 | self.Update() |
---|
66 | |
---|
67 | def OnLoad(self, event=None, path=None): |
---|
68 | """ |
---|
69 | Load a pdf file |
---|
70 | |
---|
71 | : Param path: full path to the file |
---|
72 | """ |
---|
73 | self.pdf.LoadFile(path) |
---|
74 | |
---|
75 | |
---|
76 | def OnPrevPageButton(self, event): |
---|
77 | """ |
---|
78 | Goes to Previous page |
---|
79 | """ |
---|
80 | self.pdf.gotoPreviousPage() |
---|
81 | |
---|
82 | def OnNextPageButton(self, event): |
---|
83 | """ |
---|
84 | Goes to Next page |
---|
85 | """ |
---|
86 | self.pdf.gotoNextPage() |
---|
87 | |
---|
88 | def OnClose(self, event): |
---|
89 | """ |
---|
90 | Close panel |
---|
91 | """ |
---|
92 | self.parent.Destroy() |
---|
93 | |
---|
94 | class PDFFrame(wx.Frame): |
---|
95 | """ |
---|
96 | Frame for PDF panel |
---|
97 | """ |
---|
98 | def __init__(self, parent, id, title, path): |
---|
99 | """ |
---|
100 | Init |
---|
101 | |
---|
102 | :param parent: parent panel/container |
---|
103 | :param path: full path of the pdf file |
---|
104 | """ |
---|
105 | # Initialize the Frame object |
---|
106 | wx.Frame.__init__(self, parent, id, title, |
---|
107 | wx.DefaultPosition, wx.Size(600, 830)) |
---|
108 | # make an instance of the class |
---|
109 | PDFPanel(self, path) |
---|
110 | |
---|
111 | class ViewApp(wx.App): |
---|
112 | def OnInit(self): |
---|
113 | path = None |
---|
114 | frame = PDFFrame(None, -1, "PDFView", path=path) |
---|
115 | |
---|
116 | frame.Show(True) |
---|
117 | #self.SetTopWindow(frame) |
---|
118 | |
---|
119 | return True |
---|
120 | |
---|
121 | if __name__ == "__main__": |
---|
122 | app = ViewApp(0) |
---|
123 | app.MainLoop() |
---|