本文整理汇总了Java中org.apache.pdfbox.pdmodel.interactive.form.PDField类的典型用法代码示例。如果您正苦于以下问题:Java PDField类的具体用法?Java PDField怎么用?Java PDField使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PDField类属于org.apache.pdfbox.pdmodel.interactive.form包,在下文中一共展示了PDField类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: removeWidgets
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入依赖的package包/类
void removeWidgets(PDField targetField) throws IOException {
if (targetField instanceof PDTerminalField) {
List<PDAnnotationWidget> widgets = ((PDTerminalField)targetField).getWidgets();
for (PDAnnotationWidget widget : widgets) {
PDPage page = widget.getPage();
if (page != null) {
List<PDAnnotation> annotations = page.getAnnotations();
boolean removed = false;
for (PDAnnotation annotation : annotations) {
if (annotation.getCOSObject().equals(widget.getCOSObject()))
{
removed = annotations.remove(annotation);
break;
}
}
if (!removed)
System.out.println("Inconsistent annotation definition: Page annotations do not include the target widget.");
} else {
System.out.println("Widget annotation does not have an associated page; cannot remove widget.");
// TODO: In this case iterate all pages and try to find and remove widget in all of them
}
}
} else if (targetField instanceof PDNonTerminalField) {
List<PDField> childFields = ((PDNonTerminalField)targetField).getChildren();
for (PDField field : childFields)
removeWidgets(field);
} else {
System.out.println("Target field is neither terminal nor non-terminal; cannot remove widgets.");
}
}
示例2: getAcroFormComponentAt
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入依赖的package包/类
public PDField getAcroFormComponentAt(Point2D point, CoordinateSpace sourceSpace){
Point2D pdfPoint = sourceSpace.convertPointTo(point, this.getBounds(), CoordinateSpace.PDF);
if(_document == null) return null;
PDDocumentCatalog cat = _document.getDocumentCatalog();
if(cat == null) return null;
PDAcroForm acroForm = cat.getAcroForm();
if(acroForm == null) return null;
try {
List<PDField> fields = acroForm.getFields();
for(PDField field : fields){
PDRectangle rect = PDFields.getPDRectangleForField(field);
if(rect.contains((float)pdfPoint.getX(), (float)pdfPoint.getY())){
return field;
}
}
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return null;
}
示例3: sanitizeAcroFormActions
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入依赖的package包/类
public void sanitizeAcroFormActions(BleachSession session, PDAcroForm acroForm) {
LOGGER.trace("Checking AcroForm Actions");
if (acroForm == null) {
LOGGER.debug("No AcroForms found");
return;
}
Iterator<PDField> fields = acroForm.getFieldIterator();
fields.forEachRemaining(field -> {
// Sanitize annotations
field.getWidgets().forEach(annotation -> sanitizeAnnotation(session, annotation));
// Sanitize field actions
PDFormFieldAdditionalActions fieldActions = field.getActions();
if (fieldActions == null) {
return;
}
sanitizeFieldAdditionalActions(session, fieldActions);
});
}
示例4: testAFieldTwice
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入依赖的package包/类
/**
* <a href="https://stackoverflow.com/questions/22074449/how-to-know-if-a-field-is-on-a-particular-page">
* how to know if a field is on a particular page?
* </a>
* <p>
* This sample document does not contain the optional page entry in its annotations.
* Thus, the fast method fails in contrast to the safe one.
* </p>
*/
@Test
public void testAFieldTwice() throws IOException
{
System.out.println("aFieldTwice.pdf\n=================");
try ( InputStream resource = getClass().getResourceAsStream("aFieldTwice.pdf") )
{
PDDocument document = PDDocument.load(resource);
PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
if (acroForm != null)
{
for (PDField field : acroForm.getFieldTree())
{
System.out.println(field.getFullyQualifiedName());
for (PDAnnotationWidget widget : field.getWidgets())
{
System.out.print(widget.getAnnotationName() != null ? widget.getAnnotationName() : "(NN)");
System.out.printf(" - fast: %s", determineFast(document, widget));
System.out.printf(" - safe: %s\n", determineSafe(document, widget));
}
}
}
}
System.out.println();
}
示例5: testTestDuplicateField2
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入依赖的package包/类
/**
* <a href="https://stackoverflow.com/questions/22074449/how-to-know-if-a-field-is-on-a-particular-page">
* how to know if a field is on a particular page?
* </a>
* <p>
* This sample document contains the optional page entry in its annotations.
* Thus, the fast method returns the same result as the safe one.
* </p>
*/
@Test
public void testTestDuplicateField2() throws IOException
{
System.out.println("test_duplicate_field2.pdf\n=================");
try ( InputStream resource = getClass().getResourceAsStream("test_duplicate_field2.pdf") )
{
PDDocument document = PDDocument.load(resource);
PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
if (acroForm != null)
{
for (PDField field : acroForm.getFieldTree())
{
System.out.println(field.getFullyQualifiedName());
for (PDAnnotationWidget widget : field.getWidgets())
{
System.out.print(widget.getAnnotationName() != null ? widget.getAnnotationName() : "(NN)");
System.out.printf(" - fast: %s", determineFast(document, widget));
System.out.printf(" - safe: %s\n", determineSafe(document, widget));
}
}
}
}
System.out.println();
}
示例6: getFormFieldNames
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/39574021/how-can-the-internal-labels-of-the-editable-fields-in-an-acroform-pdf-be-found">
* How can the internal labels of the editable fields in an acroform .pdf be found and listed?
* </a>
* <p>
* This method retrieves the form field names from the given {@link PDDocument}.
* </p>
*/
List<String> getFormFieldNames(PDDocument pdDocument)
{
PDAcroForm pdAcroForm = pdDocument.getDocumentCatalog().getAcroForm();
if (pdAcroForm == null)
return Collections.emptyList();
List<String> result = new ArrayList<>();
for (PDField pdField : pdAcroForm.getFieldTree())
{
if (pdField instanceof PDTerminalField)
{
result.add(pdField.getFullyQualifiedName());
}
}
return result;
}
示例7: testReadFormOptions
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/36964496/pdfbox-2-0-overcoming-dictionary-key-encoding">
* PDFBox 2.0: Overcoming dictionary key encoding
* </a>
* <br/>
* <a href="http://www.stockholm.se/PageFiles/85478/KYF%20211%20Best%C3%A4llning%202014.pdf">
* KYF 211 Beställning 2014.pdf
* </a>
*
* <p>
* Indeed, the special characters in the names are replaced by the Unicode replacement
* character. PDFBox, when parsing a PDF name, immediately interprets its bytes as UTF-8
* encoded which fails in the document at hand.
* </p>
*/
@Test
public void testReadFormOptions() throws IOException
{
try ( InputStream originalStream = getClass().getResourceAsStream("KYF 211 Best\u00e4llning 2014.pdf") )
{
PDDocument pdfDocument = PDDocument.load(originalStream);
PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
PDField field = acroForm.getField("Krematorier");
List<PDAnnotationWidget> widgets = field.getWidgets();
System.out.println("Field Name: " + field.getPartialName() + " (" + widgets.size() + ")");
for (PDAnnotationWidget annot : widgets) {
PDAppearanceDictionary ap = annot.getAppearance();
Set<COSName> keys = ((COSDictionary)(ap.getCOSObject().getDictionaryObject("N"))).keySet();
ArrayList<String> keyList = new ArrayList<>(keys.size());
for (COSName cosKey : keys) {keyList.add(cosKey.getName());}
System.out.println(String.join("|", keyList));
}
}
}
示例8: testOpenCG_PARTANCE_701018_0415
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/44042244/pdf-failing-to-be-loaded-because-it-cant-be-read-by-pddocument">
* PDF failing to be loaded because it can't be read by PDDocument
* </a>
* <br/>
* <a href="https://drive.google.com/open?id=0B57cf1nqGbC4SEYtemxwQjFQNkU">
* CG_PARTANCE_701018_0415.pdf
* </a>
* <p>
* The PDF could properly be loaded. The code then choked on <code>acroForm.getFields()</code>
* which threw a {@link NullPointerException}, and it did so for the simple reason that the PDF
* contains no form definition and, therefore, acroForm was <code>null</code>.
* </p>
*/
@Test
public void testOpenCG_PARTANCE_701018_0415() throws IOException
{
try ( InputStream file = getClass().getResourceAsStream("CG_PARTANCE_701018_0415.pdf") )
{
PDDocument pdfTemplate = PDDocument.load(file);
PDDocumentCatalog docCatalog = pdfTemplate.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
// Get field names
List<PDField> fieldList = acroForm.getFields();
List<PDPage> pages = pdfTemplate.getDocumentCatalog().getAllPages();
//process.processPage(company, templateFile, pdfTemplate, acroForm, fieldList, pages, args);
//COSDictionary acroFormDict = acroForm.getDictionary();
//((COSArray) acroFormDict.getDictionaryObject("Fields")).clear();
}
}
示例9: setField
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入依赖的package包/类
public static void setField(PDDocument _pdfDocument, String name, String value) throws IOException
{
PDDocumentCatalog docCatalog = _pdfDocument.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDField field = acroForm.getField(name);
COSDictionary dict = ((PDField) field).getDictionary();
COSString defaultAppearance = (COSString) dict
.getDictionaryObject(COSName.DA);
if (defaultAppearance != null)
{
dict.setString(COSName.DA, "/Helv 10 Tf 0 g");
if (name.equalsIgnoreCase("Field1")) {
dict.setString(COSName.DA, "/Helv 12 Tf 0 g");
}
}
if (field instanceof PDTextbox)
{
field = new PDTextbox(acroForm, dict);
((PDField) field).setValue(value);
}
}
示例10: setFieldBold
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入依赖的package包/类
public static void setFieldBold(PDDocument _pdfDocument, String name, String value) throws IOException
{
PDDocumentCatalog docCatalog = _pdfDocument.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDField field = acroForm.getField(name);
COSDictionary dict = ((PDField) field).getDictionary();
COSString defaultAppearance = (COSString) dict
.getDictionaryObject(COSName.DA);
if (defaultAppearance != null)
{
dict.setString(COSName.DA, "/Helv 10 Tf 2 Tr .5 w 0 g");
if (name.equalsIgnoreCase("Field1")) {
dict.setString(COSName.DA, "/Helv 12 Tf 0 g");
}
}
if (field instanceof PDTextbox)
{
field = new PDTextbox(acroForm, dict);
((PDField) field).setValue(value);
}
}
示例11: listFieldsOf
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入依赖的package包/类
private void listFieldsOf(String file) throws IOException, CryptographyException {
try (PDDocument document = PDDocument.load(file)) {
if (document.isEncrypted()) {
document.decrypt("");
document.setAllSecurityToBeRemoved(true);
}
final PDDocumentCatalog documentCatalog = document.getDocumentCatalog();
final PDAcroForm acrobatForm = documentCatalog.getAcroForm();
if (acrobatForm == null) return;
List<?> fields = acrobatForm.getFields();
List<String> fieldNames = new ArrayList<>();
for (Object o : fields) {
PDField field = (PDField) o;
fieldNames.add(field.getFullyQualifiedName());
}
System.out.println(StringUtils.join(fieldNames, separator));
}
}
示例12: extractAcroForm
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入依赖的package包/类
private void extractAcroForm(PDDocument pdf, XHTMLContentHandler handler)
throws IOException, SAXException {
// Thank you, Ben Litchfield, for
// org.apache.pdfbox.examples.fdf.PrintFields
// this code derives from Ben's code
PDDocumentCatalog catalog = pdf.getDocumentCatalog();
if (catalog == null)
return;
PDAcroForm form = catalog.getAcroForm();
if (form == null)
return;
@SuppressWarnings("rawtypes")
List fields = form.getFields();
if (fields == null)
return;
@SuppressWarnings("rawtypes")
ListIterator itr = fields.listIterator();
if (itr == null)
return;
handler.startElement("div", "class", "acroform");
handler.startElement("ol");
while (itr.hasNext()) {
Object obj = itr.next();
if (obj != null && obj instanceof PDField) {
processAcroField((PDField) obj, handler, 0);
}
}
handler.endElement("ol");
handler.endElement("div");
}
示例13: mouseClicked
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入依赖的package包/类
public void mouseClicked(MouseEvent me) {
me = SwingUtilities.convertMouseEvent(me.getComponent(), me, formView);
Component comp = SwingUtilities.getDeepestComponentAt(formView, me.getX(), me.getY());
if (overView != null) {
overView.clearAnnotations();
if (comp != null && comp != formView) {
PDField foundField = formView.getAcroFormComponentAt(me.getX(), me.getY(), CoordinateSpace.SWING);
if (foundField != null && foundField instanceof PDTextbox) {
overView.addAnnotation(CreatorKit.createAnnotationFor(comp.getBounds()));
NotificationCenter.INSTANCE.postNotification("Signature Found",
SelectionForSignatureConversion.TYPE,
foundField, formView);
}
else{
NotificationCenter.INSTANCE.postNotification("No signature found.",
SelectionForSignatureConversion.TYPE);
}
}
}
super.mouseClicked(me);
}
示例14: TextComponentSwingAdapter
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入依赖的package包/类
public TextComponentSwingAdapter(PDField textField){
super(textField, new JTextArea());
DocumentListenerAdapterForAction updateListener = new DocumentListenerAdapterForAction(new AbstractAction(){
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(actionEvent.getSource() instanceof DocumentEvent){
String text = ((DocumentEvent)actionEvent.getSource()).getDocument().toString();
try {
getField().setValue(text);
} catch (IOException e) {
Logger.getLogger(PDFieldSwingAdapters.class.getName()).log(Level.SEVERE, null, e);
}
}
}
});
compatibleUI().getDocument().addDocumentListener(updateListener);
}
示例15: fromDocument
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入依赖的package包/类
public static Function<PDDocument, List<PDField>> fromDocument(){
return new Function<PDDocument, List<PDField>>(){
@Override
public List<PDField> apply(@Nullable PDDocument pdDocument) {
if(pdDocument != null &&
pdDocument.getDocumentCatalog() != null &&
pdDocument.getDocumentCatalog().getAcroForm() != null) {
try {
return pdDocument.getDocumentCatalog().getAcroForm().getFields();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
};
}