Java/PDF RTF/Form Control

Материал из Java эксперт
Перейти к: навигация, поиск

Form Combo

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfAnnotation;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfFormField;
import com.lowagie.text.pdf.PdfWriter;
public class FormComboPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("FormComboPDF.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      cb.moveTo(0, 0);
      String options[] = { "A", "B", "C" };
      PdfFormField field = PdfFormField.createCombo(writer, true, options, 0);
      field.setWidget(new Rectangle(100, 700, 180, 720), PdfAnnotation.HIGHLIGHT_INVERT);
      field.setFieldName("ACombo");
      field.setValueAsString("B");
      writer.addAnnotation(field);
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Form List Control

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfAnnotation;
import com.lowagie.text.pdf.PdfAppearance;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfFormField;
import com.lowagie.text.pdf.PdfWriter;
public class FormListPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("FormListPDF.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      cb.moveTo(0, 0);
      String options[] = { "A", "B", "C" };
      PdfFormField field = PdfFormField.createList(writer, options, 0);
      field.setWidget(new Rectangle(100, 700, 180, 760), PdfAnnotation.HIGHLIGHT_OUTLINE);
      field.setFieldName("AList");
      field.setValueAsString("B");
      writer.addAnnotation(field);
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





TextFields Demo

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfBorderDictionary;
import com.lowagie.text.pdf.PdfFormField;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.TextField;
public class TextFieldsPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4.rotate());
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TextFieldsPDF.pdf"));
      document.open();
      TextField tf = new TextField(writer, new Rectangle(100, 300, 100 + 100, 300 + 50), "asdf");
      tf.setBackgroundColor(Color.WHITE);
      tf.setBorderColor(Color.BLACK);
      tf.setBorderWidth(1);
      tf.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
      tf.setText("text...");
      tf.setAlignment(Element.ALIGN_CENTER);
      tf.setOptions(TextField.MULTILINE | TextField.REQUIRED);
      tf.setRotation(90);
      PdfFormField field = tf.getTextField();
      writer.addAnnotation(field);
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}