/*
 * $Id: Chap13_form.java,v 1.5 2002/04/26 11:04:55 blowagie Exp $
 * $Name:  $
 *
 * This code is free software. It may only be copied or modified
 * if you include the following copyright notice
 *
 * --> Copyright 2002 by Bruno Lowagie <--
 *
 * This code is part of the 'iText Tutorial'.
 * You can find the complete tutorial at the following address:
 * http://www.lowagie.com/iText/tutorial/
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * itext@lowagie.com
 */
 
import java.io.*;
import java.awt.Color;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class Test_form extends java.lang.Object {

/**
	* @param args the command line arguments
	*/
public static void main(String args[]) {

	
	// step 1: creation of a document-object
	Document document = new Document();
	try {

		// step 1: we creation of a reader for blankcrf
		PdfReader reader = new PdfReader("Source.pdf");

		// we retrieve the total number of pages
		int n = reader.getNumberOfPages();
		int rotation;

		// step 2: we create a writer that listens to the document
		PdfWriter writer = 
			PdfWriter.getInstance(document, new FileOutputStream("Destin.pdf")); 

		// step 3: we open the document
		document.open();

		// step 4: we add content
		PdfContentByte cb = writer.getDirectContent();
		int i = 0;
		System.out.println("There are " + n + " pages in the document.");

		while (i < n) {

			i++;
			document.setPageSize(reader.getPageSizeWithRotation(i));
			document.newPage();
			PdfImportedPage page1 = writer.getImportedPage(reader, i);

			// step 5: create form field
			PdfAcroForm acroForm = writer.getAcroForm();
				 BaseFont helv = 
				BaseFont.createFont(
					BaseFont.TIMES_BOLD, 
					BaseFont.WINANSI, 
					BaseFont.NOT_EMBEDDED);

			float fontSize = 12;

			if (i == 1) {
				acroForm.addSingleLineTextField(
					"testds", 
					"ds=test", 
					helv, 
					fontSize, 
					171, 
					770, 
					350, 
					790); 
			} else {
				acroForm.addSingleLineTextField(
					"vartest", 
					"<vartest>", 
					helv, 
					fontSize, 
					171, 
					770, 
					350, 
					790); 
			}

			rotation = reader.getPageRotation(i);
			if (rotation == 90 || rotation == 270) {
				cb.addTemplate(
					page1, 
					0, 
					-1f, 
					1f, 
					0, 
					0, 
					reader.getPageSizeWithRotation(i).height()); 
			} else {
				cb.addTemplate(page1, 1f, 0, 0, 1f, 0, 0);
			}

			System.out.println("processed page " + i);

		}

	} catch (Exception de) {
		de.printStackTrace();
	}

	// step 6: close the document
	document.close();
}
}
