Skip to content

Commit cf6d57a

Browse files
Merge pull request #160 from chinapandaman/PPF-159
PPF-159: update docs for radio button fill
2 parents e9f2fe4 + 5741d4c commit cf6d57a

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

PyPDFForm/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
from .middleware.wrapper import PyPDFForm
44

5-
__version__ = "0.2.2"
5+
__version__ = "0.3.0"

docs/v2/api_reference.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ Its keys have to be `string` and need to match the annotated names of elements.
118118
Its values currently support the following:
119119
1) A `string`, which will be printed on the corresponding `text` element.
120120
2) A `boolean`, which will check the corresponding `checkbox` element.
121-
3) A valid image `bytes` stream, which will be drawn on the corresponding `image` element.
121+
3) An `integer`, which will select the corresponding option of a group of radio buttons with the same name.
122+
NOTE: Only groups of radio buttons with the same name are supported. If there is only one
123+
radio button with a name, please consider using `checkbox` instead.
124+
4) A valid image `bytes` stream, which will be drawn on the corresponding `image` element.
122125

123126
* **editable** - only available if `simple_mode` is `True`, enabling this will allow the filled PDF to be still
124127
editable. NOTE: `image` elements that are filled via the `fill` method will NOT be editable even if this is `True`.
@@ -159,13 +162,14 @@ such as font size and text wrap length.
159162
* **element_name** - a string which represents the annotated name of the element.
160163

161164
* **element_type** - an enum which represents the type of the element, currently supporting
162-
`text`, `checkbox` and `image`.
165+
`text`, `checkbox`, `radio` and `image`.
163166

164167
* **element_value** - this is the value that's used to fill this element.
165168
It currently supports the following based on the type of the element:
166169
1) A `string`, if the element is a `text`.
167170
2) A `boolean`, if the element is a `checkbox`.
168-
3) A valid image `bytes` stream, if the element is an `image`.
171+
3) An `integer`, if the element is a `radio`.
172+
4) A valid image `bytes` stream, if the element is an `image`.
169173

170174
### **name**
171175

@@ -174,7 +178,7 @@ A string which represents the annotated name of the element. Readonly.
174178
### **type**
175179

176180
An enum value which represents the type of the element, currently supporting
177-
`text`, `checkbox` and `image`. Readonly.
181+
`text`, `checkbox`, `radio` and `image`. Readonly.
178182

179183
### **value**
180184

@@ -183,7 +187,8 @@ It currently supports the following based on the type of the element:
183187

184188
1) A `string`, if the element is a `text`.
185189
2) A `boolean`, if the element is a `checkbox`.
186-
3) A valid image `bytes` stream, if the element is an `image`.
190+
3) An `integer`, if the element is a `radio`.
191+
4) A valid image `bytes` stream, if the element is an `image`.
187192

188193
### **font** = *None*
189194

docs/v2/examples.md

+34
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,40 @@ with open(PATH_TO_DOWNLOADED_SAMPLE_PDF_FORM, "rb+") as template:
441441
Link to this example: https://github.com/chinapandaman/PyPDFForm/blob/master/examples/fill_customized_elements.py
442442

443443

444+
## Fill a PDF form with radio buttons
445+
446+
This example uses this [template](https://github.com/chinapandaman/PyPDFForm/blob/master/pdf_samples/v2/sample_template_with_radio_button.pdf).
447+
It demos filling a PDF form's radio button elements.
448+
449+
```python
450+
import os
451+
452+
from PyPDFForm import PyPDFForm
453+
454+
PATH_TO_DOWNLOADED_SAMPLE_PDF_FORM = os.path.join(
455+
os.path.expanduser("~/Downloads"), "sample_template_with_radio_button.pdf"
456+
) # Change this to where you downloaded the sample PDF form
457+
458+
PATH_TO_FILLED_PDF_FORM = os.path.join(
459+
os.path.expanduser("~"), "output.pdf"
460+
) # Change this to where you wish to put your filled PDF form
461+
462+
with open(PATH_TO_DOWNLOADED_SAMPLE_PDF_FORM, "rb+") as template:
463+
filled_pdf = PyPDFForm(template.read()).fill(
464+
{
465+
"radio_1": 0,
466+
"radio_2": 1,
467+
"radio_3": 2,
468+
},
469+
)
470+
471+
with open(PATH_TO_FILLED_PDF_FORM, "wb+") as output:
472+
output.write(filled_pdf.stream)
473+
```
474+
475+
Link to this example: https://github.com/chinapandaman/PyPDFForm/blob/master/examples/simple_fill_radio.py
476+
477+
444478
## Fill a PDF form with image elements
445479

446480
This example uses this [template](https://github.com/chinapandaman/PyPDFForm/blob/master/pdf_samples/v2/sample_template_with_image_field.pdf)

examples/simple_fill_radio.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import os
2+
3+
from PyPDFForm import PyPDFForm
4+
5+
PATH_TO_DOWNLOADED_SAMPLE_PDF_FORM = os.path.join(
6+
os.path.expanduser("~/Downloads"), "sample_template_with_radio_button.pdf"
7+
) # Change this to where you downloaded the sample PDF form
8+
9+
PATH_TO_FILLED_PDF_FORM = os.path.join(
10+
os.path.expanduser("~"), "output.pdf"
11+
) # Change this to where you wish to put your filled PDF form
12+
13+
with open(PATH_TO_DOWNLOADED_SAMPLE_PDF_FORM, "rb+") as template:
14+
filled_pdf = PyPDFForm(template.read()).fill(
15+
{
16+
"radio_1": 0,
17+
"radio_2": 1,
18+
"radio_3": 2,
19+
},
20+
)
21+
22+
with open(PATH_TO_FILLED_PDF_FORM, "wb+") as output:
23+
output.write(filled_pdf.stream)

0 commit comments

Comments
 (0)