Мне было поручено создать скрипт Python, который добавит поле формы в существующий файл PDF. Казалось бы, довольно прямолинейно, но я уперся в стену. Поле формы добавляется (видимо не правильно), но оно просто не отображается, когда я открываю pdf. Ни заголовок формы, ни ее текстовые данные не видны. Любые рекомендации по этому поводу будут очень признательны. Текущая версия блокнота, который я использую:
import PyPDF2
from PyPDF2 import PdfReader, PdfWriter
from PyPDF2.generic import (
NameObject, DictionaryObject, ArrayObject, FloatObject, TextStringObject
)
def add_form_field_to_pdf(input_pdf_path, output_pdf_path, field_name, field_x, field_y, field_width, field_height, page_number=0):
# Read the input PDF
reader = PdfReader(input_pdf_path)
writer = PdfWriter()
set_need_appearances_writer(writer)
# Copy pages from the original PDF to the writer
for page in reader.pages:
writer.add_page(page)
# Get the specified page
page = writer.pages[page_number]
# Create a new AcroForm if it doesn't exist
if "/AcroForm" not in writer._root_object:
writer._root_object.update({
NameObject("/AcroForm"): DictionaryObject({
NameObject("/Fields"): ArrayObject()
})
})
# Define the form field
field = DictionaryObject()
field.update({
NameObject("/FT"): NameObject("/Tx"), # Text field type
NameObject("/T"): TextStringObject(field_name), # Field name
NameObject("/Rect"): ArrayObject([ # Field rectangle
FloatObject(field_x),
FloatObject(field_y),
FloatObject(field_x + field_width),
FloatObject(field_y + field_height)
]),
NameObject("/V"): TextStringObject("Hello"), # Default value
NameObject("/Ff"): FloatObject(0), # Field flags
NameObject("/P"): page # Page where the field is located
})
# Add the field to the AcroForm
acro_form = writer._root_object["/AcroForm"]
fields = acro_form["/Fields"]
fields.append(field)
# Write the modified PDF to a new file
with open(output_pdf_path, "wb") as output_file:
writer.write(output_file)
def set_need_appearances_writer(writer):
try:
catalog = writer._root_object
# get the AcroForm tree and add "/NeedAppearances attribute
if "/AcroForm" not in catalog:
writer._root_object.update(
{
NameObject("/AcroForm"): DictionaryObject(
len(writer._objects), 0, writer
)
}
)
need_appearances = NameObject("/NeedAppearances")
writer._root_object["/AcroForm"][need_appearances] = BooleanObject(True)
return writer
except Exception as e:
print("set_need_appearances_writer() catch : ", repr(e))
return writer
input_pdf = "sampleForm.pdf"
output_pdf = "FormAttemp-2.pdf"
add_form_field_to_pdf(input_pdf, output_pdf, "SampleField4", 100, 500, 200, 20)
Подробнее здесь: https://stackoverflow.com/questions/793 ... ith-python
Как добавить поле формы в существующий PDF-файл с помощью Python ⇐ Python
Программы на Python
-
Anonymous
1737230968
Anonymous
Мне было поручено создать скрипт Python, который добавит поле формы в существующий файл PDF. Казалось бы, довольно прямолинейно, но я уперся в стену. Поле формы добавляется (видимо не правильно), но оно просто не отображается, когда я открываю pdf. Ни заголовок формы, ни ее текстовые данные не видны. Любые рекомендации по этому поводу будут очень признательны. Текущая версия блокнота, который я использую:
import PyPDF2
from PyPDF2 import PdfReader, PdfWriter
from PyPDF2.generic import (
NameObject, DictionaryObject, ArrayObject, FloatObject, TextStringObject
)
def add_form_field_to_pdf(input_pdf_path, output_pdf_path, field_name, field_x, field_y, field_width, field_height, page_number=0):
# Read the input PDF
reader = PdfReader(input_pdf_path)
writer = PdfWriter()
set_need_appearances_writer(writer)
# Copy pages from the original PDF to the writer
for page in reader.pages:
writer.add_page(page)
# Get the specified page
page = writer.pages[page_number]
# Create a new AcroForm if it doesn't exist
if "/AcroForm" not in writer._root_object:
writer._root_object.update({
NameObject("/AcroForm"): DictionaryObject({
NameObject("/Fields"): ArrayObject()
})
})
# Define the form field
field = DictionaryObject()
field.update({
NameObject("/FT"): NameObject("/Tx"), # Text field type
NameObject("/T"): TextStringObject(field_name), # Field name
NameObject("/Rect"): ArrayObject([ # Field rectangle
FloatObject(field_x),
FloatObject(field_y),
FloatObject(field_x + field_width),
FloatObject(field_y + field_height)
]),
NameObject("/V"): TextStringObject("Hello"), # Default value
NameObject("/Ff"): FloatObject(0), # Field flags
NameObject("/P"): page # Page where the field is located
})
# Add the field to the AcroForm
acro_form = writer._root_object["/AcroForm"]
fields = acro_form["/Fields"]
fields.append(field)
# Write the modified PDF to a new file
with open(output_pdf_path, "wb") as output_file:
writer.write(output_file)
def set_need_appearances_writer(writer):
try:
catalog = writer._root_object
# get the AcroForm tree and add "/NeedAppearances attribute
if "/AcroForm" not in catalog:
writer._root_object.update(
{
NameObject("/AcroForm"): DictionaryObject(
len(writer._objects), 0, writer
)
}
)
need_appearances = NameObject("/NeedAppearances")
writer._root_object["/AcroForm"][need_appearances] = BooleanObject(True)
return writer
except Exception as e:
print("set_need_appearances_writer() catch : ", repr(e))
return writer
input_pdf = "sampleForm.pdf"
output_pdf = "FormAttemp-2.pdf"
add_form_field_to_pdf(input_pdf, output_pdf, "SampleField4", 100, 500, 200, 20)
Подробнее здесь: [url]https://stackoverflow.com/questions/79367781/how-to-add-a-form-field-to-an-existing-pdf-with-python[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия