ValueError: key must be PdfObject #2028
Closed
aptara-pawan
started this conversation in
General
Replies: 1 comment
-
This is already addressed in an issue. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
i am getting error while adding checkbox in PDF.
ValueError: key must be PdfObject
Below is my code, Please help me on this
import chardet
import PyPDF2
import re
from PyPDF2 import PdfFileWriter, PdfFileReader
from PyPDF2.generic import TextStringObject, BooleanObject, NameObject, DictionaryObject, ArrayObject,PdfObject
def add_checkbox_to_page(page, x, y, width, height):
checkbox = {
"/Type": "/Annot",
"/Subtype": "/Widget",
"/FT": "/Btn",
"/T": TextStringObject("Checkbox"),
"/Rect": [x, y, x + width, y + height],
"/V": TextStringObject("Yes"),
"/AP": DictionaryObject(
{
"/N": DictionaryObject({NameObject("/Off"): TextStringObject("[]"), NameObject("/Yes"): TextStringObject("[/On]")}),
"/D": DictionaryObject({NameObject("/Off"): TextStringObject("0 0 R"), NameObject("/Yes"): TextStringObject("1 0 R")}),
}
),
"/AS": NameObject("/Yes"),
"/H": TextStringObject("N"),
}
#page[NameObject("/Annots")].append(DictionaryObject(checkbox))
page[NameObject("/Annots")] = ArrayObject([DictionaryObject(checkbox)])
def add_checkboxes_to_pdf(input_pdf, output_pdf):
pdf_reader = PdfFileReader(input_pdf)
pdf_writer = PdfFileWriter()
Call the function to add checkboxes to the PDF
add_checkboxes_to_pdf("D:\Pawan\temp\25\check\test.pdf", "D:\Pawan\temp\25\check\AJC26740_OUT.pdf")
Beta Was this translation helpful? Give feedback.
All reactions