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