[60c7011] | 1 | # Read PDF files by embeding the Adobe Acrobat Reader |
---|
| 2 | # wx.activex module uses class ActiveX control |
---|
| 3 | |
---|
| 4 | import wx |
---|
[bc7bc839] | 5 | import os |
---|
[60c7011] | 6 | if wx.Platform == '__WXMSW__': |
---|
| 7 | from wx.lib.pdfwin import PDFWindow |
---|
| 8 | |
---|
[0aca693] | 9 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
| 10 | STYLE = wx.TE_MULTILINE|wx.TE_READONLY|wx.SUNKEN_BORDER|wx.HSCROLL |
---|
| 11 | |
---|
| 12 | class TextPanel(ScrolledPanel): |
---|
| 13 | """ |
---|
| 14 | Panel that contains the text |
---|
| 15 | """ |
---|
| 16 | def __init__(self, parent, text=None): |
---|
| 17 | """ |
---|
| 18 | """ |
---|
| 19 | ScrolledPanel.__init__(self, parent, id=-1) |
---|
| 20 | self.SetupScrolling() |
---|
| 21 | self.parent = parent |
---|
| 22 | self.text = text |
---|
| 23 | sizer = wx.BoxSizer(wx.VERTICAL) |
---|
| 24 | self.textctl = wx.TextCtrl(self, -1, size=(-1, -1), style=STYLE) |
---|
| 25 | self.textctl.SetValue(self.text) |
---|
| 26 | sizer.Add(self.textctl, proportion=1, flag=wx.EXPAND) |
---|
| 27 | self.SetSizer(sizer) |
---|
| 28 | self.SetAutoLayout(True) |
---|
| 29 | wx.EVT_CLOSE(self.parent, self.OnClose) |
---|
| 30 | |
---|
| 31 | def OnClose(self, event): |
---|
| 32 | """ |
---|
| 33 | Close panel |
---|
| 34 | """ |
---|
| 35 | self.parent.Destroy() |
---|
| 36 | |
---|
| 37 | class TextFrame(wx.Frame): |
---|
| 38 | """ |
---|
| 39 | Frame for PDF panel |
---|
| 40 | """ |
---|
| 41 | def __init__(self, parent, id, title, text): |
---|
| 42 | """ |
---|
| 43 | Init |
---|
| 44 | |
---|
| 45 | :param parent: parent panel/container |
---|
| 46 | :param path: full path of the pdf file |
---|
| 47 | """ |
---|
| 48 | # Initialize the Frame object |
---|
| 49 | wx.Frame.__init__(self, parent, id, title, |
---|
| 50 | wx.DefaultPosition, wx.Size(600, 830)) |
---|
| 51 | # make an instance of the class |
---|
| 52 | TextPanel(self, text) |
---|
| 53 | self.SetFocus() |
---|
| 54 | |
---|
[60c7011] | 55 | class PDFPanel(wx.Panel): |
---|
| 56 | """ |
---|
| 57 | Panel that contains the pdf reader |
---|
| 58 | """ |
---|
| 59 | def __init__(self, parent, path=None): |
---|
| 60 | """ |
---|
| 61 | """ |
---|
| 62 | wx.Panel.__init__(self, parent, id=-1) |
---|
| 63 | |
---|
| 64 | self.parent = parent |
---|
[bc7bc839] | 65 | self.path = path |
---|
[60c7011] | 66 | sizer = wx.BoxSizer(wx.VERTICAL) |
---|
| 67 | btnSizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 68 | |
---|
| 69 | self.pdf = PDFWindow(self, style=wx.SUNKEN_BORDER) |
---|
| 70 | |
---|
| 71 | sizer.Add(self.pdf, proportion=1, flag=wx.EXPAND) |
---|
| 72 | |
---|
| 73 | btn = wx.Button(self, wx.NewId(), "Open PDF File") |
---|
| 74 | self.Bind(wx.EVT_BUTTON, self.OnOpenButton, btn) |
---|
| 75 | btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5) |
---|
| 76 | |
---|
[bc7bc839] | 77 | self.pdf.LoadFile(self.path) |
---|
[60c7011] | 78 | btn = wx.Button(self, wx.NewId(), "Previous Page") |
---|
| 79 | self.Bind(wx.EVT_BUTTON, self.OnPrevPageButton, btn) |
---|
| 80 | btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5) |
---|
| 81 | |
---|
| 82 | btn = wx.Button(self, wx.NewId(), "Next Page") |
---|
| 83 | self.Bind(wx.EVT_BUTTON, self.OnNextPageButton, btn) |
---|
| 84 | btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5) |
---|
| 85 | |
---|
| 86 | btn = wx.Button(self, wx.NewId(), "Close") |
---|
| 87 | self.Bind(wx.EVT_BUTTON, self.OnClose, btn) |
---|
| 88 | btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5) |
---|
| 89 | btnSizer.Add((50,-1), proportion=2, flag=wx.EXPAND) |
---|
| 90 | sizer.Add(btnSizer, proportion=0, flag=wx.EXPAND) |
---|
| 91 | |
---|
| 92 | self.SetSizer(sizer) |
---|
| 93 | self.SetAutoLayout(True) |
---|
[03a8b16] | 94 | wx.EVT_CLOSE(self.parent, self.OnClose) |
---|
[60c7011] | 95 | |
---|
| 96 | def OnOpenButton(self, event): |
---|
| 97 | """ |
---|
| 98 | Open file button |
---|
| 99 | """ |
---|
| 100 | # make sure you have PDF files available on your drive |
---|
| 101 | dlg = wx.FileDialog(self, wildcard="*.pdf") |
---|
[bc7bc839] | 102 | dlg.SetDirectory(os.path.dirname(self.path)) |
---|
[60c7011] | 103 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 104 | wx.BeginBusyCursor() |
---|
[bc7bc839] | 105 | file = dlg.GetPath() |
---|
| 106 | self.pdf.LoadFile(file) |
---|
| 107 | self.parent.SetTitle(os.path.basename(file.split('.')[0])) |
---|
[60c7011] | 108 | wx.EndBusyCursor() |
---|
| 109 | dlg.Destroy() |
---|
[03a8b16] | 110 | # Let Panel know the file changed: Avoiding C++ error |
---|
| 111 | self.Update() |
---|
[60c7011] | 112 | |
---|
| 113 | def OnLoad(self, event=None, path=None): |
---|
| 114 | """ |
---|
| 115 | Load a pdf file |
---|
| 116 | |
---|
| 117 | : Param path: full path to the file |
---|
| 118 | """ |
---|
| 119 | self.pdf.LoadFile(path) |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | def OnPrevPageButton(self, event): |
---|
| 123 | """ |
---|
| 124 | Goes to Previous page |
---|
| 125 | """ |
---|
| 126 | self.pdf.gotoPreviousPage() |
---|
| 127 | |
---|
| 128 | def OnNextPageButton(self, event): |
---|
| 129 | """ |
---|
| 130 | Goes to Next page |
---|
| 131 | """ |
---|
| 132 | self.pdf.gotoNextPage() |
---|
| 133 | |
---|
| 134 | def OnClose(self, event): |
---|
| 135 | """ |
---|
| 136 | Close panel |
---|
| 137 | """ |
---|
| 138 | self.parent.Destroy() |
---|
| 139 | |
---|
| 140 | class PDFFrame(wx.Frame): |
---|
| 141 | """ |
---|
| 142 | Frame for PDF panel |
---|
| 143 | """ |
---|
| 144 | def __init__(self, parent, id, title, path): |
---|
| 145 | """ |
---|
| 146 | Init |
---|
| 147 | |
---|
| 148 | :param parent: parent panel/container |
---|
| 149 | :param path: full path of the pdf file |
---|
| 150 | """ |
---|
| 151 | # Initialize the Frame object |
---|
| 152 | wx.Frame.__init__(self, parent, id, title, |
---|
| 153 | wx.DefaultPosition, wx.Size(600, 830)) |
---|
| 154 | # make an instance of the class |
---|
| 155 | PDFPanel(self, path) |
---|
| 156 | |
---|
| 157 | class ViewApp(wx.App): |
---|
| 158 | def OnInit(self): |
---|
| 159 | path = None |
---|
| 160 | frame = PDFFrame(None, -1, "PDFView", path=path) |
---|
| 161 | |
---|
| 162 | frame.Show(True) |
---|
| 163 | #self.SetTopWindow(frame) |
---|
| 164 | |
---|
| 165 | return True |
---|
| 166 | |
---|
| 167 | if __name__ == "__main__": |
---|
| 168 | app = ViewApp(0) |
---|
| 169 | app.MainLoop() |
---|