You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2015/10/18 10:39:49 UTC

svn commit: r1709243 - in /pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive: ./ form/ form/AddBorderToField.java form/CreateSimpleForm.java form/UpdateFieldOnDocumentOpen.java form/package-info.java

Author: msahyoun
Date: Sun Oct 18 08:39:49 2015
New Revision: 1709243

URL: http://svn.apache.org/viewvc?rev=1709243&view=rev
Log:
PDFBOX-3030: add new samples for interactive.form

Added:
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/AddBorderToField.java   (with props)
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateSimpleForm.java   (with props)
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/UpdateFieldOnDocumentOpen.java   (with props)
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/package-info.java   (with props)

Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/AddBorderToField.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/AddBorderToField.java?rev=1709243&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/AddBorderToField.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/AddBorderToField.java Sun Oct 18 08:39:49 2015
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.pdfbox.examples.interactive.form;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
+import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary;
+import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
+import org.apache.pdfbox.pdmodel.interactive.form.PDField;
+
+/**
+ * Add a border to an existing field.
+ * 
+ * This sample adds a border to a field.
+ * 
+ * This sample builds on the form generated by @link CreateSimpleForm so you need to run that first.
+ * 
+ */
+public final class AddBorderToField
+{
+    private AddBorderToField()
+    {
+    }
+    
+    public static void main(String[] args) throws IOException
+    {
+        // Load the PDF document created by SimpleForm.java
+        PDDocument document = PDDocument.load(new File("target/SimpleForm.pdf"));
+        PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
+        
+        // Get the field and the widget associated to it.
+        // Note: there might be multiple widgets
+        PDField field = acroForm.getField("SampleField");
+        PDAnnotationWidget widget = field.getWidgets().get(0);
+        
+        // Create the definition for a green border
+        PDAppearanceCharacteristicsDictionary fieldAppearance = 
+                new PDAppearanceCharacteristicsDictionary(new COSDictionary());
+        PDColor green = new PDColor(new float[] { 0, 1, 0 }, PDDeviceRGB.INSTANCE);
+        fieldAppearance.setBorderColour(green);
+        
+        // Set the information to be used by the widget which is responsible
+        // for the visual style of the form field.
+        widget.setAppearanceCharacteristics(fieldAppearance);
+        
+        document.save("target/AddBorderToField.pdf");
+        document.close();
+    }
+}

Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/AddBorderToField.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateSimpleForm.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateSimpleForm.java?rev=1709243&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateSimpleForm.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateSimpleForm.java Sun Oct 18 08:39:49 2015
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.pdfbox.examples.interactive.form;
+
+import java.io.IOException;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.PDResources;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
+import org.apache.pdfbox.pdmodel.font.PDFont;
+import org.apache.pdfbox.pdmodel.font.PDType1Font;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
+import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
+import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;
+
+/**
+ * An example of creating an AcroForm and a form field from scratch.
+ * 
+ * The form field is created with properties similar to creating
+ * a form with default settings in Adobe Acrobat.
+ * 
+ */
+public final class CreateSimpleForm
+{
+    private CreateSimpleForm()
+    {
+    }
+    
+    public static void main(String[] args) throws IOException
+    {
+        // Create a new document with an empty page.
+        PDDocument document = new PDDocument();
+        PDPage page = new PDPage(PDRectangle.A4);
+        document.addPage(page);
+        
+        // Adobe Acrobat uses Helvetica as a default font and 
+        // stores that under the name '/Helv' in the resources dictionary
+        PDFont font = PDType1Font.HELVETICA;
+        PDResources resources = new PDResources();
+        resources.put(COSName.getPDFName("Helv"), font);
+        
+        // Add a new AcroForm and add that to the document
+        PDAcroForm acroForm = new PDAcroForm(document);
+        document.getDocumentCatalog().setAcroForm(acroForm);
+        
+        // Add and set the resources and default appearance at the form level
+        acroForm.setDefaultResources(resources);
+        
+        // Acrobat sets the font size on the form level to be
+        // auto sized as default. This is done by setting the font size to '0'
+        String defaultAppearanceString = "/Helv 0 Tf 0 g";
+        acroForm.setDefaultAppearance(defaultAppearanceString);
+        
+        // Add a form field to the form.
+        PDTextField textBox = new PDTextField(acroForm);
+        textBox.setPartialName("SampleField");
+        // Acrobat sets the font size to 12 as default
+        // This is done by setting the font size to '12' on the
+        // field level.
+        defaultAppearanceString = "/Helv 12 Tf 0 g";
+        textBox.setDefaultAppearance(defaultAppearanceString);
+        
+        // add the field to the acroform
+        acroForm.getFields().add(textBox);
+        
+        // Specify the annotation associated with the field
+        PDAnnotationWidget widget = textBox.getWidgets().get(0);
+        PDRectangle rect = new PDRectangle(50, 750, 200, 50);
+        widget.setRectangle(rect);
+        
+        // Add the annotation to the page
+        page.getAnnotations().add(widget);
+        
+        // set the field value
+        textBox.setValue("Sample field");
+
+        document.save("target/SimpleForm.pdf");
+        document.close();
+    }
+}

Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateSimpleForm.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/UpdateFieldOnDocumentOpen.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/UpdateFieldOnDocumentOpen.java?rev=1709243&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/UpdateFieldOnDocumentOpen.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/UpdateFieldOnDocumentOpen.java Sun Oct 18 08:39:49 2015
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.pdfbox.examples.interactive.form;
+
+import java.io.File;
+import java.io.IOException;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript;
+
+/**
+ * Update field automatically when the document is opened.
+ * 
+ * This sample adds document action to automatically update a 
+ * field value with the current date when the document is opened.
+ * 
+ * This sample builds on the form generated by @link CreateSimpleForm so you need to run that first.
+ * 
+ */
+public final class UpdateFieldOnDocumentOpen
+{
+    private UpdateFieldOnDocumentOpen()
+    {
+    }
+    
+    public static void main(String[] args) throws IOException
+    {
+        // Load the PDF document created by SimpleForm.java
+        PDDocument document = PDDocument.load(new File("target/SimpleForm.pdf"));
+        
+        // Note that the JavaScript will depend on the reader application.
+        // The classes and methods available to Adobe Reader and Adobe Acrobat
+        // are documented in the Acrobat SDK.
+        String javaScript = "var now = util.printd('yyyy-mm-dd', new Date());"
+                + "var oField = this.getField('SampleField');"
+                + "oField.value = now;";
+        
+        // Create an action as JavaScript action
+        PDActionJavaScript jsAction = new PDActionJavaScript();
+        jsAction.setAction(javaScript);
+        
+        // Set the action to be executed when the document is opened
+        document.getDocumentCatalog().setOpenAction(jsAction);
+        
+        document.save("target/UpdateFieldOnDocumentOpen.pdf");
+        document.close();
+    }
+}

Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/UpdateFieldOnDocumentOpen.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/package-info.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/package-info.java?rev=1709243&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/package-info.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/package-info.java Sun Oct 18 08:39:49 2015
@@ -0,0 +1,23 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * AcroForms samples for PDFBox.
+ *
+ */
+
+package org.apache.pdfbox.examples.interactive.form;
\ No newline at end of file

Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/package-info.java
------------------------------------------------------------------------------
    svn:eol-style = native