查看PDF的问题

时间:2011-01-24 01:50:32

标签: python interface gtk pygtk glade

平台: Linux,GTK +

工具: Python,PyGTK和Glade。

问题:

  • 我想编写一个能够显示PDF文件的程序。

问题:

  • 我需要哪些小部件和Python模块?

感谢您的任何建议!

3 个答案:

答案 0 :(得分:2)

查看python poppler绑定。

我以简单的方式呈现pdf文件。我复制了示例中用于python poppler gtk绑定的方法

def load_pdf(self):
    self.doc = poppler.document_new_from_file (uri, None)
    # the number of pages in the pdf
    self.n_pgs = self.document.get_n_pgs()
    # the current page of the pdf
    self.curr_pg = 0
    # the current page being displayed
    self.curr_pg_disp = self.document.get_page(self.curr_pg)
    # the scale of the page
    self.scale = 1
    # the document width and height
    self.doc_width, self.doc_height = self.curr_pg_disp.get_size()


def render_pdf(self):
    cr = self.pdfda.window.cairo_create()
    cr.set_source_rgb(1, 1, 1)
    if self.scale != 1:
        cr.scale(self.scale, self.scale)
    cr.rectangle(0, 0, self.doc_width, self.doc_height)
    cr.fill()
    self.curr_pg_disp.render(cr)

def on_next_btn_clicked(self, widget, data=None):
    if self.curr_pg < self.n_pgs:
        self.curr_pg = self.curr_pg + 1
        self.curr_pg_disp = self.doc.get_page(self.curr_pg)
        self.render_page()

def on_prev_btn_clicked(self, widget, data=None):
    if self.curr_pg > 0:
        self.curr_pg = self.curr_pg - 1
        self.curr_pg_disp = self.doc.get_page(self.curr_pg)
        self.render_page()

它不是最好的或最漂亮但它的确有效。我仍然需要添加如何使其可滚动或居中在绘图区域和类似的东西,但有一个开始。

你也可以查看evince python绑定,我相信它们有一个小部件可以用来使pdf渲染更容易。我正在开发Windows,所以如果有的话,我没有使用它。

答案 1 :(得分:0)

不是GTK而是wxPython:

  

此示例显示了一个PDF Viewer类,它处理Zoom和Scrolling之类的内容。它需要python-poppler和wxPython&gt; = 2.8.9。

答案 2 :(得分:-1)

相关问题