/*
 * 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);
        widget.setPage(page);
        
        // make sure the annotation is visible on screen and paper
        widget.setPrinted(true);
        
        // 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();
    }
}

@@ -879,16 +879,60 @@
 eption;%0A
+import org.apache.pdfbox.cos.COSDictionary;%0A
 import o
@@ -1280,49 +1280,262 @@
 del.
-interactive.annotation.PDAnnotationWidget
+graphics.color.PDColor;%0Aimport org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;%0Aimport org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;%0Aimport org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary
 ;%0Aim
@@ -3384,16 +3384,145 @@
 d level.
+ %0A        // The text color is set to blue in this example.%0A        // To use black, replace %220 0 1 rg%22 with %220 0 0 rg%22 or %220 g%22.
 %0A       
@@ -3559,24 +3559,29 @@
 elv 12 Tf 0 
+0 1 r
 g%22;%0A        
@@ -3985,32 +3985,33 @@
 Page(page);%0A
+%0A
         
 %0A        // 
@@ -3990,32 +3990,533 @@
 page);%0A%0A        
+// set green border and yellow background%0A        // if you prefer defaults, just delete this code block%0A        PDAppearanceCharacteristicsDictionary fieldAppearance%0A                = new PDAppearanceCharacteristicsDictionary(new COSDictionary());%0A        fieldAppearance.setBorderColour(new PDColor(new float%5B%5D%7B0,1,0%7D, PDDeviceRGB.INSTANCE));%0A        fieldAppearance.setBackground(new PDColor(new float%5B%5D%7B1,1,0%7D, PDDeviceRGB.INSTANCE));%0A        widget.setAppearanceCharacteristics(fieldAppearance);%0A
 %0A        // make

improve example with non standard text, border and background color