Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
JorjMcKie authored Jan 15, 2019
2 parents 56e501d + e1c7ae6 commit 87fbb58
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 656 deletions.
12 changes: 6 additions & 6 deletions demo/decrypt.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python2
#this demo will open an encrypted PDF document
#decrypt it with the provided password
#and save as a new PDF document
#usage: removePass.py <input file> <password> <output file>
"""
This demo will open an encrypted PDF document, decrypt it with the provided
password and save as a new PDF document.
usage: removePass.py <input file> <password> <output file>
"""
import fitz
import sys

Expand All @@ -20,4 +20,4 @@
'cannot decrypt %s with password "%s"' % (sys.argv[1], sys.argv[2])

#save as a new PDF
doc.save(sys.argv[3])
doc.save(sys.argv[3], decrypt=True) # decrypt=False is default
Binary file added demo/new-annots.pdf
Binary file not shown.
Binary file removed demo/new-annots.png
Binary file not shown.
68 changes: 46 additions & 22 deletions demo/new-annots.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
It contains the following annotation types:
Text ("sticky note"), FreeText, text markers (underline, strike-out,
highlight), Circle, Square, Line, PolyLine, Polygon and FileAttachment.
highlight), Circle, Square, Line, PolyLine, Polygon, FileAttachment and Stamp.
Notes
-----
Expand All @@ -28,86 +28,102 @@
PyMuPDF v1.13.13
-------------------------------------------------------------------------------
"""
text = "text in line\ntext in line\ntext in line"
text = "text in line\ntext in line\ntext in line\ntext in line"
red = (1, 0, 0)
blue = (0, 0, 1)
yellow = (1, 1, 0)
colors = {"stroke": red, "fill": yellow}
border = {"width": 1, "dashes": [1, 3]}
gold = (1, 1, 0)
colors = {"stroke": blue, "fill": gold}
border = {"width": 0.3, "dashes": [2]}
displ = fitz.Rect(0, 50, 0, 50)
pos = fitz.Point(50, 100)
t1 = u"têxt üsès Lätiñ charß,\nEuro: €, mu: µ, superscripts: ²³!"
r = fitz.Rect(50, 100, 220, 135)
t1 = u"têxt üsès Lätiñ charß,\nEUR: €, mu: µ, super scripts: ²³!"

def print_descr(rect, annot):
"""Print a short description to the right of an annotation."""
page = annot.parent
page.insertText(rect.br + (10, -3),
"""Print a short description to the right of an annot rect."""
annot.parent.insertText(rect.br + (10, 0),
"'%s' annotation" % annot.type[1], color = red)

doc = fitz.open()
page = doc.newPage()
annot = page.addFreetextAnnot(pos, t1, color = blue)
annot = page.addFreetextAnnot(r, t1, rotate = 90)
annot.setBorder(border)
annot.update(fontsize = 10, border_color=red, fill_color=gold, text_color=blue)

print_descr(annot.rect, annot)
r = annot.rect + displ
print("added 'FreeText'")

annot = page.addTextAnnot(r.tl, t1)
print_descr(annot.rect, annot)
pos = annot.rect.tl + displ.tl * 2
print("added 'Sticky Note'")

page.insertText(pos, text, fontsize=11)
rl = page.searchFor("text in line")
pos = annot.rect.tl + displ.tl

# first insert 4 rotated text lines
page.insertText(pos, text, fontsize=11, morph = (pos, fitz.Matrix(-15)))
# now search text to get the quads
rl = page.searchFor("text in line", quads = True)
r0 = rl[0]
r1 = rl[1]
r2 = rl[2]
r3 = rl[3]
annot = page.addHighlightAnnot(r0)
print_descr(r0, annot)
# need to convert quad to rect for descriptive text ...
print_descr(r0.rect, annot)
print("added 'HighLight'")

annot = page.addStrikeoutAnnot(r1)
print_descr(r1, annot)
print_descr(r1.rect, annot)
print("added 'StrikeOut'")

annot = page.addUnderlineAnnot(r2)
print_descr(r2, annot)
print_descr(r2.rect, annot)
print("added 'Underline'")

r = r2 + displ
annot = page.addSquigglyAnnot(r3)
print_descr(r3.rect, annot)
print("added 'Squiggly'")

r = r3.rect + displ
annot = page.addPolylineAnnot([r.bl, r.tr, r.br, r.tl])
annot.setLineEnds(fitz.ANNOT_LE_Circle, fitz.ANNOT_LE_Diamond)
annot.setBorder(border)
annot.setColors(colors)
annot.setLineEnds(fitz.ANNOT_LE_ClosedArrow, fitz.ANNOT_LE_RClosedArrow)
annot.update()
print_descr(annot.rect, annot)
print("added 'PolyLine'")

r+= displ
annot = page.addPolygonAnnot([r.bl, r.tr, r.br, r.tl])
annot.setBorder(border)
annot.setColors(colors)
annot.setLineEnds(fitz.ANNOT_LE_Circle, fitz.ANNOT_LE_Diamond)
annot.setLineEnds(fitz.ANNOT_LE_Diamond, fitz.ANNOT_LE_Circle)
annot.update()
print_descr(annot.rect, annot)
print("added 'Polygon'")

r+= displ
annot = page.addLineAnnot(r.tr, r.bl)
annot.setLineEnds(fitz.ANNOT_LE_Circle, fitz.ANNOT_LE_Diamond)
annot.setBorder(border)
annot.setColors(colors)
annot.setLineEnds(fitz.ANNOT_LE_Diamond, fitz.ANNOT_LE_Circle)
annot.update()
print_descr(annot.rect, annot)
print("added 'Line'")

r+= displ
annot = page.addRectAnnot(r)
annot.setBorder(border)
annot.setColors(colors)
annot.update()
print_descr(annot.rect, annot)
print("added 'Square'")

r+= displ
annot = page.addCircleAnnot(r)
annot.setBorder(border)
annot.setColors(colors)
annot.update()
print_descr(annot.rect, annot)
print("added 'Circle'")

Expand All @@ -116,4 +132,12 @@ def print_descr(rect, annot):
print_descr(annot.rect, annot)
print("added 'FileAttachment'")

doc.save("new-annots.pdf", garbage=4, deflate=True, clean=True)
r+= displ
annot = page.addStampAnnot(r, stamp = 10)
annot.setColors(colors)
annot.setOpacity(0.5)
annot.update()
print_descr(annot.rect, annot)
print("added 'Stamp'")

doc.save("new-annots.pdf", expand=255)
8 changes: 4 additions & 4 deletions demo/numpy2fitz.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#! /usr/bin/python
from __future__ import print_function
import numpy as np
import PIL
from PIL import Image
import fitz
import sys, time
print("Python:", sys.version)
Expand All @@ -24,7 +24,7 @@
'''
height = 2048 # choose whatever you want here; image will consist
width = 1024 # of 256 x 256 sized tiles, each colored as follows
width = 2028 # of 256 x 256 sized tiles, each colored as follows

image = np.ndarray((height, width, 3), dtype=np.uint8)

Expand All @@ -42,10 +42,10 @@
pix.writePNG("numpy2fitz.png")
ttab.append((time.perf_counter(), "fitz"))

pix = PIL.Image.frombuffer("RGB", [width, height], samples,
pix = Image.frombuffer("RGB", [width, height], samples,
"raw", "RGB", 0, 1)
pix.save("numpy2PIL.png")
ttab.append((time.perf_counter(), "PIL"))

for i, t in enumerate(ttab):
if i > 0: print(t[0] - ttab[i-1][0], t[1])
if i > 0: print("storing with %s: %g sec." % (t[1], t[0] - ttab[i-1][0]))
9 changes: 6 additions & 3 deletions demo/piechart2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
#==============================================================================
from fitz.utils import getColor # for getting RGB colors by name
doc = fitz.open() # new empty PDF
doc.insertPage() # creates an ISO-A4 page
page = doc[-1] # this is the page
img = page.newShape()
page = doc.newPage() # creates an ISO-A4 page

img = page.newShape() # start a Shape (canvas) for the page

# title of the page
title = "Sitzverteilung nach der Bundestagswahl 2013"

# pie chart center and point of 1st data pie
center = fitz.Point(200, 250)
point = fitz.Point(100, 250) # will cycle through table data

# this is the radius
radius = abs(point - center)

Expand Down
Binary file modified doc/PyMuPDF.pdf
Binary file not shown.
Binary file modified doc/html.zip
Binary file not shown.
62 changes: 0 additions & 62 deletions examples/draw-pencil.py

This file was deleted.

Loading

0 comments on commit 87fbb58

Please sign in to comment.