iText PDF template

阅读更多
useful materials
http://svn.assembla.com/svn/jarbilling/billing/trunk/billing/vendor/pdfmanipulator/Fdf.java
http://svn.assembla.com/svn/jarbilling/billing/trunk/billing/vendor/pdfmanipulator/test/BatchFormFill.java
http://www.coderanch.com/t/63861/open-source/iText-Silent-Print
http://threebit.net/mail-archive/itext-questions/msg02068.html

part4.chapter13.PrintPreferencesExample:
http://itextpdf.com/examples/iia.php?id=230

http://comments.gmane.org/gmane.comp.java.lib.itext.general/28053

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Map.Entry;
import java.util.Set;

import com.itextpdf.text.Document;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.AcroFields.Item;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

public class PdfTest2 {
	public static void main(String[] args) throws Exception {
		// Create output PDF
		Document document = new Document(PageSize.A4);

		document.open();

		// Load existing PDF
		PdfReader reader = new PdfReader(new FileInputStream(
				"Sample application form_ENG edit.pdf"));
		// PdfImportedPage page = writer.getImportedPage(reader, 1);

		// we'll create the file in memory
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		// Now we create the PDF
		PdfStamper stamper = new PdfStamper(reader, baos);
		// We alter the fields of the existing PDF
		AcroFields fields = stamper.getAcroFields();

		 BaseFont baseFont = FontFactory.getFont(FontFactory.TIMES_ITALIC,BaseFont.IDENTITY_H, new Float(12)).getBaseFont();

		Set> entrySet = fields.getFields().entrySet();
		for (Entry entry : entrySet) {
			String key = entry.getKey();
			System.out.println(key);
			fields.setField(key, key);
//			if (fields.getFieldType(key) == AcroFields.FIELD_TYPE_TEXT) {
				fields.setFieldProperty(key, "textfont", baseFont, null);
				fields.setFieldProperty(key, "textsize", new Float(16), null);
//			}
		}
		fields.setField("tradeID", "007");
		// close the stamper
		fields.setField("acNo", "9527");
		 
	 
		stamper.close();

		document.close();

		// let's write the file in memory to a file anyway
		FileOutputStream fos = new FileOutputStream("HelloWorld-Gavin.pdf");
		fos.write(baos.toByteArray());
		fos.close();

	}
}

你可能感兴趣的:(iText,PDF,template,PdfStamper)