-
-
Notifications
You must be signed in to change notification settings - Fork 189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#578] Add binding for AcroFormDocumentHelper::setFormFieldName method #580
Open
amargerandhw
wants to merge
15
commits into
pikepdf:main
Choose a base branch
from
amargerandhw:feature/#578
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d2642a9
[#578] Add binding for AcroFormDocumentHelper::setFormFieldName method
amargerandhw f8fe4ae
CS
amargerandhw ab0dbcf
Text
amargerandhw dda534f
Remove
amargerandhw e1c3d0e
fix
amargerandhw 44f7920
fix
amargerandhw f336528
Fix
amargerandhw 10247d4
WIP
amargerandhw 1d1e6d4
Fix
amargerandhw aebd660
Fix
amargerandhw 598dbe5
Fix
amargerandhw feeb723
fix
amargerandhw 8a674d2
Add get form fields method
amargerandhw 4822afa
Fix
amargerandhw b6785cd
Add test
amargerandhw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// SPDX-FileCopyrightText: 2022 James R. Barlow | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
#include <qpdf/QPDFAcroFormDocumentHelper.hh> | ||
|
||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
|
||
#include "pikepdf.h" | ||
|
||
void init_acroform(py::module_ &m) | ||
{ | ||
py::class_<QPDFAcroFormDocumentHelper, | ||
std::shared_ptr<QPDFAcroFormDocumentHelper>, | ||
QPDFDocumentHelper>(m, "AcroFormDocument") | ||
.def( | ||
py::init([](QPDF &q) { | ||
QPDFAcroFormDocumentHelper afdh(q); | ||
|
||
return afdh; | ||
}), | ||
py::keep_alive<0, 1>(), // LCOV_EXCL_LINE | ||
py::arg("q") | ||
) | ||
.def( | ||
"set_form_field_name", | ||
[](QPDFAcroFormDocumentHelper &afdh, QPDFObjectHandle annot, std::string const& name) { | ||
QPDFFormFieldObjectHelper ffh = afdh.getFieldForAnnotation(annot); | ||
auto ffh_oh = ffh.getObjectHandle(); | ||
if (ffh_oh.hasKey("/Parent")) { | ||
QPDFObjectHandle parent = ffh_oh.getKey("/Parent"); | ||
QPDFFormFieldObjectHelper ph(parent); | ||
afdh.setFormFieldName(ph, name); | ||
} else { | ||
afdh.setFormFieldName(ffh, name); | ||
} | ||
} | ||
) | ||
.def( | ||
"get_form_fields", | ||
[](QPDFAcroFormDocumentHelper &afdh) { | ||
return afdh.getFormFields(); | ||
}, | ||
py::return_value_policy::reference_internal | ||
); | ||
|
||
py::class_<QPDFFormFieldObjectHelper, | ||
std::shared_ptr<QPDFFormFieldObjectHelper>, | ||
QPDFObjectHelper>(m, "FormField"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
#include <qpdf/QPDFObjectHandle.hh> | ||
#include <qpdf/QPDF.hh> | ||
#include <qpdf/QPDFWriter.hh> | ||
#include <qpdf/QPDFDocumentHelper.hh> | ||
|
||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
|
@@ -895,6 +896,18 @@ void init_object(py::module_ &m) | |
.def_property_readonly("obj", [](QPDFObjectHelper &poh) -> QPDFObjectHandle { | ||
return poh.getObjectHandle(); | ||
}); | ||
py::class_<QPDFDocumentHelper, std::shared_ptr<QPDFDocumentHelper>>(m, "DocumentHelper"); | ||
// .def( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this commented out? It breaks existing functionality. |
||
// "__eq__", | ||
// [](QPDFObjectHelper &self, QPDFObjectHelper &other) { | ||
// // Object helpers are equal if their object handles are equal | ||
// return objecthandle_equal( | ||
// self.getObjectHandle(), other.getObjectHandle()); | ||
// }, | ||
// py::is_operator()) | ||
// .def_property_readonly("obj", [](QPDFObjectHelper &poh) -> QPDFObjectHandle { | ||
// return poh.getObjectHandle(); | ||
// }); | ||
|
||
m.def("_encode", [](py::handle handle) { return objecthandle_encode(handle); }); | ||
m.def("unparse", [](py::object obj) -> py::bytes { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# SPDX-FileCopyrightText: 2022 James R. Barlow | ||
# SPDX-License-Identifier: CC0-1.0 | ||
|
||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
from pikepdf import AcroFormDocument, Pdf | ||
|
||
|
||
@pytest.fixture | ||
def form(resources): | ||
with Pdf.open(resources / 'form.pdf') as pdf: | ||
yield pdf | ||
|
||
|
||
def test_set_form_field_name(form): | ||
afd = AcroFormDocument(form) | ||
field = form.Root.AcroForm.Fields[0] | ||
assert field.T == 'Text1' | ||
afd.set_form_field_name(field, 'new_field_name') | ||
assert field.T == 'new_field_name' | ||
|
||
def test_get_form_fields(form): | ||
afd = AcroFormDocument(form) | ||
assert len(afd.get_form_fields()) == 4 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this to be useful to anyone who say, wants to know what form fields are present before changing their names, you'll need to bind QPDFFormFieldObjectHelper as well. Then one should be able to retrieve form fields from the form and manipulate the field name from there.
I'm not going to accept a PR that solves your issue alone and isn't helpful to others. It needs to be of general use. All of the methods of QPDFFormFieldObjectHelper and QPDFAcroFormDocumentHelper should be exposed. You're probably going to need them anyway.
Please bind get/set functions as
def_property
which is equivalent to setting a@property
with a pythonic getter and setter. Then one would useform.fields[...].name = 'MyName'
etc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the method getFormFields, although it is mandatory for the usage that I solve. You could simplify use it as I showed in the test.
I dont know how I should do the binding with def_property.
If I understand you would me to add it to the binding of the class QPDFFormFieldObjectHelper but the method is on the class QPDFAcroFormDocumentHelper.
Implementing all the methods would take me too much time as I'm completely a novice in python and just discovered how pybind11 works, we could merge the base that I implemented and someone could improve it in the future