Beispiel #1
0
 def run(self):
     pages = tools.get_pages_from_ranges(self.ranges)
     document = Poppler.Document.new_from_file('file://' + self.file_in,
                                               None)
     number_of_pages = document.get_n_pages()
     self.emit('start', number_of_pages)
     if number_of_pages > 0:
         temp_pdf = tools.create_temp_file()
         pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
         context = cairo.Context(pdfsurface)
         self.emit('start', number_of_pages)
         for i in range(0, number_of_pages):
             self.emit('todo', '{0}/{1}'.format(i, number_of_pages))
             if i + 1 not in pages:
                 current_page = document.get_page(i)
                 context.save()
                 pdf_width, pdf_height = current_page.get_size()
                 pdfsurface.set_size(pdf_width, pdf_height)
                 current_page.render(context)
                 context.restore()
                 context.show_page()
             if self.stop is True:
                 break
             self.emit('done', '{0}/{1}'.format(i, number_of_pages))
         pdfsurface.flush()
         pdfsurface.finish()
         shutil.copy(temp_pdf, self.file_out)
         os.remove(temp_pdf)
     if self.stop is True:
         self.emit('interrupted')
     else:
         self.emit('finished')
Beispiel #2
0
 def on_apply_clicked(self, widget, clear=False):
     if clear:
         self.reset()
     text = self.textmark.get_text()
     color = self.button_color.get_rgba()
     font = self.button_font.get_font()
     size = int(self.button_font.get_font_desc().get_size()/1000)
     x = self.x
     y = self.y
     if self.check_this.get_active():
         to_update = [ self.no_page]
     elif self.check_all.get_active():
         to_update = range(0, self.document.get_n_pages())
     elif self.check_range.get_active():
         to_update = get_pages_from_ranges(
             get_ranges(self.range.get_text()))
     for i in to_update:
         if clear and str(i) in self.pages.keys():
             del self.pages[str(i)]
         else:
             self.pages[str(i)] = PageOptions(text_text=text,
                 text_color=color, text_font=font, text_size=size,
                 text_x=x, text_y=y)
     if self.no_page in to_update:
         self.preview()
Beispiel #3
0
 def on_apply_clicked(self, widget, clear=False):
     if clear:
         self.reset()
     flip_horizontal = self.check_horizontal.get_active()
     flip_vertical = self.check_vertical.get_active()
     if self.rotate_90.get_active():
         rotation_angle = 1.0
     elif self.rotate_180.get_active():
         rotation_angle = 2.0
     elif self.rotate_270.get_active():
         rotation_angle = 3.0
     else:
         rotation_angle = 0.0
     to_update = []
     if self.check_this.get_active():
         to_update = [ self.no_page ]
     elif self.check_all.get_active():
         to_update = range(0, self.document.get_n_pages())
     elif self.check_range.get_active():
         to_update = get_pages_from_ranges(
             get_ranges(self.range.get_text()))
     for i in to_update:
         if clear and str(i) in self.pages.keys():
             del self.pages[str(i)]
         else:
             self.pages[str(i)] = PageOptions(rotation_angle,
                                              flip_horizontal,
                                              flip_vertical)
     if self.no_page in to_update:
         self.preview()
Beispiel #4
0
def rotate_ranges_in_pdf(file_in,
                         file_out,
                         degrees,
                         ranges,
                         flip_horizontal=False,
                         flip_vertical=False):
    pages = tools.get_pages_from_ranges(ranges)
    document = Poppler.Document.new_from_file('file://' + file_in, None)
    if document.get_n_pages() > 0:
        temp_pdf = tools.create_temp_file()
        pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
        context = cairo.Context(pdfsurface)
        for i in range(0, document.get_n_pages()):
            current_page = document.get_page(i)
            if i + 1 in pages:
                if degrees == ROTATE_000 or degrees == ROTATE_180:
                    pdf_width, pdf_height = current_page.get_size()
                else:
                    pdf_height, pdf_width = current_page.get_size()
                pdfsurface.set_size(pdf_width, pdf_height)
                context.save()
                mtr = cairo.Matrix()
                mtr.rotate(degrees / 180.0 * math.pi)
                context.transform(mtr)
                if degrees == ROTATE_090:
                    context.translate(0.0, -pdf_width)
                elif degrees == ROTATE_180:
                    context.translate(-pdf_width, -pdf_height)
                elif degrees == ROTATE_270:
                    context.translate(-pdf_height, 0.0)
                if flip_vertical:
                    context.scale(1, -1)
                    if degrees == ROTATE_000 or degrees == ROTATE_180:
                        context.translate(0, -pdf_height)
                    else:
                        context.translate(0, -pdf_width)
                if flip_horizontal:
                    context.scale(-1, 1)
                    if degrees == ROTATE_000 or degrees == ROTATE_180:
                        context.translate(-pdf_width, 0)
                    else:
                        context.translate(-pdf_height, 0)
                current_page.render(context)
                context.restore()
            else:
                context.save()
                pdf_width, pdf_height = current_page.get_size()
                pdfsurface.set_size(pdf_width, pdf_height)
                current_page.render(context)
                context.restore()
            context.show_page()
        pdfsurface.flush()
        pdfsurface.finish()
        shutil.copy(temp_pdf, file_out)
        os.remove(temp_pdf)
Beispiel #5
0
def remove_ranges(file_in_name, file_out_name, ranges):
    file_in = open(file_in_name, 'rb')
    pages = tools.get_pages_from_ranges(ranges)
    reader = PyPDF2.PdfFileReader(file_in)
    writer = PyPDF2.PdfFileWriter()
    for i in range(0, reader.getNumPages()):
        if i + 1 not in pages:
            page = reader.getPage(i)
            writer.addPage(page)
    file_out = open(file_out_name, 'wb')
    writer.write(file_out)
    file_out.close()
    file_in.close()
Beispiel #6
0
def rotate_ranges_in_pdf(file_in, file_out, degrees, ranges,
                         flip_horizontal=False, flip_vertical=False):
    pages = tools.get_pages_from_ranges(ranges)
    document = Poppler.Document.new_from_file('file://' + file_in, None)
    if document.get_n_pages() > 0:
        temp_pdf = tools.create_temp_file()
        pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
        context = cairo.Context(pdfsurface)
        for i in range(0, document.get_n_pages()):
            current_page = document.get_page(i)
            if i+1 in pages:
                if degrees == ROTATE_000 or degrees == ROTATE_180:
                    pdf_width, pdf_height = current_page.get_size()
                else:
                    pdf_height, pdf_width = current_page.get_size()
                pdfsurface.set_size(pdf_width, pdf_height)
                context.save()
                mtr = cairo.Matrix()
                mtr.rotate(degrees/180.0*math.pi)
                context.transform(mtr)
                if degrees == ROTATE_090:
                        context.translate(0.0, -pdf_width)
                elif degrees == ROTATE_180:
                        context.translate(-pdf_width, -pdf_height)
                elif degrees == ROTATE_270:
                        context.translate(-pdf_height, 0.0)
                if flip_vertical:
                    context.scale(1, -1)
                    if degrees == ROTATE_000 or degrees == ROTATE_180:
                        context.translate(0, -pdf_height)
                    else:
                        context.translate(0, -pdf_width)
                if flip_horizontal:
                    context.scale(-1, 1)
                    if degrees == ROTATE_000 or degrees == ROTATE_180:
                        context.translate(-pdf_width, 0)
                    else:
                        context.translate(-pdf_height, 0)
                current_page.render(context)
                context.restore()
            else:
                context.save()
                pdf_width, pdf_height = current_page.get_size()
                pdfsurface.set_size(pdf_width, pdf_height)
                current_page.render(context)
                context.restore()
            context.show_page()
        pdfsurface.flush()
        pdfsurface.finish()
        shutil.copy(temp_pdf, file_out)
        os.remove(temp_pdf)
Beispiel #7
0
def remove_ranges(file_in, file_out, ranges):
    pages = tools.get_pages_from_ranges(ranges)
    document = Poppler.Document.new_from_file('file://' + file_in, None)
    number_of_pages = document.get_n_pages()
    temp_pdf = tools.create_temp_file()
    pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
    context = cairo.Context(pdfsurface)
    for i in range(0, number_of_pages):
        if i + 1 not in pages:
            current_page = document.get_page(i)
            context.save()
            pdf_width, pdf_height = current_page.get_size()
            pdfsurface.set_size(pdf_width, pdf_height)
            current_page.render(context)
            context.restore()
            context.show_page()
    pdfsurface.flush()
    pdfsurface.finish()
    shutil.copy(temp_pdf, file_out)
    os.remove(temp_pdf)
Beispiel #8
0
def remove_ranges(file_in, file_out, ranges):
    pages = tools.get_pages_from_ranges(ranges)
    document = Poppler.Document.new_from_file('file://' + file_in, None)
    number_of_pages = document.get_n_pages()
    temp_pdf = tools.create_temp_file()
    pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
    context = cairo.Context(pdfsurface)
    for i in range(0, number_of_pages):
        if i+1 not in pages:
            current_page = document.get_page(i)
            context.save()
            pdf_width, pdf_height = current_page.get_size()
            pdfsurface.set_size(pdf_width, pdf_height)
            current_page.render(context)
            context.restore()
            context.show_page()
    pdfsurface.flush()
    pdfsurface.finish()
    shutil.copy(temp_pdf, file_out)
    os.remove(temp_pdf)
Beispiel #9
0
 def run(self):
     total_documents = len(self.files)
     pages = tools.get_pages_from_ranges(self.ranges)
     if total_documents > 0 and pages:
         for index, file_in in enumerate(self.files):
             self.emit('todo', file_in)
             document = Poppler.Document.new_from_file(
                 'file://' + file_in, None)
             number_of_pages = document.get_n_pages()
             if number_of_pages > 1:
                 filename, _ = os.path.splitext(file_in)
                 file_out = filename + '_extracted_pages.pdf'
                 temp_pdf = tools.create_temp_file()
                 pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
                 context = cairo.Context(pdfsurface)
                 for i in range(0, number_of_pages):
                     if i + 1 in pages:
                         current_page = document.get_page(i)
                         context.save()
                         pdf_width, pdf_height = current_page.get_size()
                         pdfsurface.set_size(pdf_width, pdf_height)
                         current_page.render(context)
                         context.restore()
                         context.show_page()
                     self.emit(
                         'donef',
                         (float(index) + float(i) / float(number_of_pages))
                         / float(total_documents))
                     if self.stop is True:
                         break
                 pdfsurface.flush()
                 pdfsurface.finish()
                 shutil.copy(temp_pdf, file_out)
                 os.remove(temp_pdf)
             if self.stop is True:
                 break
     if self.stop is True:
         self.emit('interrupted')
     else:
         self.emit('finished')
Beispiel #10
0
 def on_apply_clicked(self, widget, clear=False):
     if clear:
         self.reset()
     file_watermark = self.file_entry.get_label()
     if not os.path.exists(file_watermark):
         file_watermark = None
     zoom = float(self.zoom_entry.get_value() / 100.0)
     if self.check_this.get_active():
         to_update = [ self.no_page]
     elif self.check_all.get_active():
         to_update = range(0, self.document.get_n_pages())
     elif self.check_range.get_active():
         to_update = get_pages_from_ranges(
             get_ranges(self.range.get_text()))
     for i in to_update:
         if clear and str(i) in self.pages.keys():
             del self.pages[str(i)]
         else:
             self.pages[str(i)] = PageOptions(image_x=self.x,
                 image_y=self.y, image_zoom=zoom, image_file=file_watermark)
     if self.no_page in to_update:
         self.preview()
Beispiel #11
0
def rotate_ranges_in_pdf(file_in_name,
                         file_out_name,
                         degrees,
                         ranges,
                         flip_horizontal=False,
                         flip_vertical=False):
    file_in = open(file_in_name, 'rb')
    pages = tools.get_pages_from_ranges(ranges)
    reader = PyPDF2.PdfFileReader(file_in)
    writer = PyPDF2.PdfFileWriter()
    for i in range(0, reader.getNumPages()):
        page = reader.getPage(i)
        if i + 1 in pages:
            page.rotateClockwise(degrees)
            if flip_horizontal is True:
                page.scale(-1, 1)
            if flip_vertical is True:
                page.scale(1, -1)
        writer.addPage(page)
    file_out = open(file_out_name, 'wb')
    writer.write(file_out)
    file_out.close()
    file_in.close()