Java/PDF RTF/Form Fill
Версия от 18:01, 31 мая 2010; (обсуждение)
Fill in a simple registration form
import java.io.FileOutputStream;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
// SimpleRegistrationForm.pdf is from http://itextdocs.lowagie.ru/tutorial/
/**
* Fill in a simple registration form.
*/
public class FormFillPDF {
public static void main(String[] args) {
try {
PdfReader reader = new PdfReader("SimpleRegistrationForm.pdf");
// filling in the form
PdfStamper stamp1 = new PdfStamper(reader, new FileOutputStream("FormFillPDF.pdf"));
AcroFields form1 = stamp1.getAcroFields();
form1.setField("name", "your name");
form1.setField("address", "Your Address");
form1.setField("postal_code", "1111");
form1.setField("email", "email");
stamp1.close();
} catch (Exception de) {
de.printStackTrace();
}
}
}
Form Fill and Make it Flatten
import java.io.FileOutputStream;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class FormFillAndMakeItFlattenPDF {
public static void main(String[] args) {
try {
PdfReader reader = new PdfReader("SimpleRegistrationForm.pdf");
PdfStamper stamp2 = new PdfStamper(reader, new FileOutputStream("FormFillAndMakeItFlattenPDF.pdf"));
AcroFields form2 = stamp2.getAcroFields();
form2.setField("name", "your name");
form2.setField("address", "address");
form2.setField("postal_code", "code");
form2.setField("email", "youremail");
stamp2.setFormFlattening(true);
stamp2.close();
}
catch (Exception de) {
de.printStackTrace();
}
}
}
Form Signature
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.Paragraph;
import com.lowagie.text.pdf.PdfAcroForm;
import com.lowagie.text.pdf.PdfWriter;
public class FormSignaturePDF {
public static void main(String[] args) {
Document document = new Document(PageSize.A4);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("FormSignaturePDF.pdf"));
document.open();
PdfAcroForm acroForm = writer.getAcroForm();
document.add(new Paragraph("Hello World"));
acroForm.addSignature("sig", 73, 705, 149, 759);
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}