Mike Buchanan wrote:
Now I am able to create a Checkbox widget exactly like I wanted to do and, after looking at your example, I was able to change this to a cool looking check in the box instead of a big X.

I'd have done it differently.
(See attachment.)

However, the output only has the Checkbox on the first line.

You gave three different checkboxes the same name.
How are you going to distinguish which one was clicked???

Have a look at the attachment.
br,
Bruno

Attachment: TestPSF2.pdf
Description: Adobe PDF document

package test;

import java.io.*;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.CMYKColor;
import com.lowagie.text.Rectangle;

import java.awt.Color;

class PSFtest
{
	private static float[] colWidths;

	private static int count = 0;
	private static String headAddr[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
	private static int lineNbr = 0;
    private static int highAddr = 0;
	private static int pageSize;

	private static PdfPTable detailBlank;
	private static PdfPTable blankUnder;
	private static PdfPTable tabBlank;

	private static boolean firstPass = true;

	public static void main (String[] args) throws Exception
	{
		// Load the input parameters

		String inputFile = "TestPSF2.txt";
		String outputPdf = "TestPSF2.pdf";

		// Create empty detail lines

		float[] colSizes = { 144f, 144f, 144f, 144f };
		colWidths = colSizes;

		Paragraph emptyPara = new Paragraph();
		emptyPara.setLeading(11f);
		emptyPara.add (new Phrase ("  \n", new Font (Font.HELVETICA, 9, Font.NORMAL, new CMYKColor(0f, 0f, 0f, 0f))));

		detailBlank = new PdfPTable(4);
		detailBlank.setTotalWidth(colWidths);
		detailBlank.setLockedWidth(true);

		PdfPCell cellBlank = new PdfPCell();
		cellBlank.setFixedHeight(15f);
		cellBlank.addElement(emptyPara);
		cellBlank.setBackgroundColor(new CMYKColor(.05f, .01f, .00f, .03f));
		cellBlank.setBorderWidthLeft(1f);
		cellBlank.setBorderWidthRight(1f);
		cellBlank.setBorderWidthTop(0f);
		cellBlank.setBorderWidthBottom(0f);

		detailBlank.addCell(cellBlank);

		cellBlank.setBorderWidthLeft(0f);
		cellBlank.setBorderWidthRight(1f);
		cellBlank.setBorderWidthTop(0f);
		cellBlank.setBorderWidthBottom(0f);

		detailBlank.addCell(cellBlank);
		detailBlank.addCell(cellBlank);
		detailBlank.addCell(cellBlank);

		blankUnder = new PdfPTable(4);
		blankUnder.setTotalWidth(colWidths);
		blankUnder.setLockedWidth(true);

		cellBlank = new PdfPCell();
		cellBlank.setFixedHeight(15f);
		cellBlank.addElement(emptyPara);
		cellBlank.setBackgroundColor(new CMYKColor(.05f, .01f, .00f, .03f));
		cellBlank.setBorderWidthLeft(1f);
		cellBlank.setBorderWidthRight(1f);
		cellBlank.setBorderWidthTop(0f);
		cellBlank.setBorderWidthBottom(1f);

		blankUnder.addCell(cellBlank);

		cellBlank.setBorderWidthLeft(0f);
		cellBlank.setBorderWidthRight(1f);
		cellBlank.setBorderWidthTop(0f);
		cellBlank.setBorderWidthBottom(1f);

		blankUnder.addCell(cellBlank);
		blankUnder.addCell(cellBlank);
		blankUnder.addCell(cellBlank);

        // Create a blank line

		tabBlank = new PdfPTable(1);
		tabBlank.setTotalWidth(576f);
		tabBlank.setLockedWidth(true);

		Paragraph paraBlank = new Paragraph();
		PdfPCell cellWhite = new PdfPCell(paraBlank);
		cellWhite.setFixedHeight(15f);
		cellWhite.setBackgroundColor(new CMYKColor(0f, 0f, 0f, 0f));
		cellWhite.setBorderWidth(0.0f);
		tabBlank.addCell(cellWhite);

		// Define the input and output documents

		FileReader fr = new FileReader(inputFile);
		BufferedReader br = new BufferedReader(fr);

		Document doc = new Document (PageSize.LETTER);
		PdfWriter pdfWriter = PdfWriter.getInstance (doc, new FileOutputStream (outputPdf));
		doc.open();

        // Read the first line and start processing

		String inputLine = br.readLine();
		int lineType = 0;

		while(inputLine != null)
		{
			lineType = Integer.parseInt(inputLine.substring(0, 2));

            if(lineType == 05)
            {
				highAddr = 0;
				firstPass = true;
			}

			if(lineType == 30)
			{
				printDetail(doc, pdfWriter, inputLine);
			}

			inputLine = br.readLine();
		}

        doc.add(blankUnder);

		doc.close();
		br.close();
	}

    private static void printDetail(Document doc, PdfWriter writer, String inputLine) throws Exception
    {
		// Do we need to print the headings?

		if((lineNbr > pageSize) || (firstPass))
		{
			printHeading(doc);
			lineNbr = 0;
			firstPass = false;
		}

		// Parse out the column values

		String CUST_PO = inputLine.substring(3, 28);
		String DUE_DATE = inputLine.substring(49, 57);
		String OPEN_AMT = inputLine.substring(70, 81);

		// Create a table line

		PdfPTable detailTable = new PdfPTable(4);
		detailTable.setTotalWidth(colWidths);
		detailTable.setLockedWidth(true);

		// Select checkbox

		Paragraph workPara = new Paragraph();
		PdfPCell workCell = new PdfPCell(workPara);
		workCell.setHorizontalAlignment(Element.ALIGN_CENTER);
		workCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		workCell.setBorderWidthLeft(1f);
		workCell.setBorderWidthRight(1f);
		workCell.setBorderWidthTop(0f);

		if(lineNbr == pageSize)
		{
			workCell.setBorderWidthBottom(1f);
		}
		else
		{
			workCell.setBorderWidthBottom(0f);
		}

		workCell.setPaddingTop(1);
		workCell.setPaddingBottom(1);
		workCell.setPaddingLeft(3f);
		workCell.setPaddingRight(3f);

		workPara.setLeading(11f);

		workCell.setFixedHeight(15f);
		workCell.setBackgroundColor(new CMYKColor(.05f, .01f, .00f, .03f));
		count++;
        workCell.setCellEvent(new BoxEvent(writer, "box" + count));

        detailTable.addCell(workCell);

		// Customer's Purchase Order Number

		workPara = new Paragraph();
		workCell = new PdfPCell(workPara);
		workCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		workCell.setBorderWidthLeft(0f);
		workCell.setBorderWidthRight(1f);
		workCell.setBorderWidthTop(0f);

		if(lineNbr == pageSize)
		{
		    workCell.setBorderWidthBottom(1f);
		}
		else
		{
			workCell.setBorderWidthBottom(0f);
		}

		workCell.setPaddingTop(1);
		workCell.setPaddingBottom(1);
		workCell.setPaddingLeft(3f);
		workCell.setPaddingRight(3f);

		workPara.setLeading(11f);
		workPara.add (new Phrase (CUST_PO, new Font (Font.COURIER, 11, Font.NORMAL, new CMYKColor(0f, 0f, 0f, 1f))));

		workCell.setFixedHeight(15f);
		workCell.setBackgroundColor(new CMYKColor(.05f, .01f, .00f, .03f));

		detailTable.addCell(workCell);

		// Due Date

		workPara = new Paragraph();
		workCell = new PdfPCell(workPara);
		workCell.setHorizontalAlignment(Element.ALIGN_CENTER);
		workCell.setBorderWidthLeft(0f);
		workCell.setBorderWidthRight(1f);
		workCell.setBorderWidthTop(0f);

		if(lineNbr == pageSize)
		{
		    workCell.setBorderWidthBottom(1f);
		}
		else
		{
			workCell.setBorderWidthBottom(0f);
		}

		workCell.setPaddingTop(1);
		workCell.setPaddingBottom(1);
		workCell.setPaddingLeft(3f);
		workCell.setPaddingRight(3f);

		workPara.setLeading(11f);
		workPara.add (new Phrase (DUE_DATE, new Font (Font.COURIER, 11, Font.NORMAL, new CMYKColor(0f, 0f, 0f, 1f))));
		workCell.setFixedHeight(15f);
		workCell.setBackgroundColor(new CMYKColor(.05f, .01f, .00f, .03f));

		detailTable.addCell(workCell);

		// Open Amount

		String convAmt = checkMinus(OPEN_AMT);

		workPara = new Paragraph();
		workCell = new PdfPCell(workPara);
		workCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		workCell.setBorderWidthLeft(0f);
		workCell.setBorderWidthRight(1f);
		workCell.setBorderWidthTop(0f);

		if(lineNbr == pageSize)
		{
		    workCell.setBorderWidthBottom(1f);
		}
		else
		{
			workCell.setBorderWidthBottom(0f);
		}

		workCell.setPaddingTop(1);
		workCell.setPaddingBottom(1);
		workCell.setPaddingLeft(3f);
		workCell.setPaddingRight(3f);

		workPara.setLeading(11f);
		workPara.add (new Phrase (convAmt, new Font (Font.COURIER, 11, Font.NORMAL, new CMYKColor(0f, 0f, 0f, 1f))));
		workCell.setFixedHeight(15f);
		workCell.setBackgroundColor(new CMYKColor(.05f, .01f, .00f, .03f));

		detailTable.addCell(workCell);

        // Post the detail line

        doc.add(detailTable);
        lineNbr++;
	}

	private static void printHeading(Document doc) throws Exception
	{

		// Print the top line

		doc.newPage();

		PdfPTable tabTitle = new PdfPTable(2);
		tabTitle.setTotalWidth(576f);
		tabTitle.setLockedWidth(true);

		Paragraph paraLeft = new Paragraph();
		PdfPCell cellLeft = new PdfPCell(paraLeft);
		cellLeft.setBorderWidth(0.0f);
		paraLeft.setLeading(7);
		paraLeft.add (new Phrase ("PAYMENT SELECTION FORM", new Font (Font.HELVETICA, 20, Font.BOLD, new CMYKColor(.73f, .44f, .00f, .05f))));
		paraLeft.setAlignment (Element.ALIGN_LEFT);

		tabTitle.addCell(cellLeft);

		Paragraph paraRite = new Paragraph();
		PdfPCell cellRite = new PdfPCell(paraRite);
		cellRite.setBorderWidth(0.0f);
        cellRite.setVerticalAlignment(Table.ALIGN_BOTTOM);
		paraRite.setLeading(7);
		paraRite.add (new Phrase ("  ", new Font (Font.HELVETICA, 8, Font.BOLD, new CMYKColor(.00f, .91f, .91f, .00f))));
		paraRite.setAlignment (Element.ALIGN_RIGHT);

		tabTitle.addCell(cellRite);

		doc.add(tabTitle);

        doc.add(tabBlank);
        doc.add(tabBlank);

        // Print the dealer's address

        int workAddr = 0;
        PdfPTable tablAddr = new PdfPTable(1);
 		tablAddr = new PdfPTable(1);
		tablAddr.setTotalWidth(576f);
		tablAddr.setLockedWidth(true);

        Paragraph paraAddr = new Paragraph();
        PdfPCell  cellAddr = new PdfPCell(paraAddr);
		cellAddr.setBackgroundColor(new CMYKColor(0f, 0f, 0f, 0f));
		cellAddr.setBorderWidth(0.0f);
		cellAddr.setIndent(70);
		cellAddr.setColspan(1);
		cellAddr.setPadding(7);

        String    strAddr1 = " ";
        String    strAddr2 = " ";
        int       intgAddr = 0;

        while(workAddr < highAddr)
        {

            strAddr1 = headAddr[workAddr].trim();
            intgAddr = strAddr1.length();
            strAddr2 = strAddr1.substring(2, intgAddr - 3);
		    strAddr1 = strAddr2.trim().concat("\n");

		    paraAddr.add(new Phrase(strAddr1, new Font (Font.HELVETICA, 11, Font.NORMAL, new CMYKColor(0f, 0f, 0f, 1f))));

		    workAddr++;
	    }

		tablAddr.addCell(cellAddr);
        doc.add(tablAddr);

        // Print a blank line and set the Page Size

		doc.add(tabBlank);

		pageSize = (33 - highAddr);

		// Print the detail column headings

		PdfPTable topDetail = new PdfPTable(4);
		topDetail.setTotalWidth(colWidths);
		topDetail.setLockedWidth(true);

		Paragraph workPara = new Paragraph();
		PdfPCell workCell = new PdfPCell(workPara);
		workCell.setBackgroundColor(new CMYKColor(.73f, .44f, 00f, .05f));
		workCell.setHorizontalAlignment(Element.ALIGN_CENTER);
		workPara.add (new Phrase("Selection\n", new Font (Font.HELVETICA, 9, Font.BOLDITALIC, new CMYKColor(0f, 0f, 0f, 0f))));
        workPara.add (new Phrase ("Boxes", new Font (Font.HELVETICA, 9, Font.BOLDITALIC, new CMYKColor(0f, 0f, 0f, 0f))));

        topDetail.addCell(workCell);

        workPara = new Paragraph();
        workCell = new PdfPCell(workPara);
        workCell.setBackgroundColor(new CMYKColor(.73f, .44f, 00f, .05f));
        workCell.setHorizontalAlignment(Element.ALIGN_CENTER);
        workPara.add (new Phrase ("Customer\n", new Font (Font.HELVETICA, 9, Font.BOLDITALIC, new CMYKColor(0f, 0f, 0f, 0f))));
        workPara.add (new Phrase ("PO Number", new Font (Font.HELVETICA, 9, Font.BOLDITALIC, new CMYKColor(0f, 0f, 0f, 0f))));

        topDetail.addCell(workCell);

        workPara = new Paragraph();
        workCell = new PdfPCell(workPara);
        workCell.setBackgroundColor(new CMYKColor(.73f, .44f, 00f, .05f));
        workCell.setHorizontalAlignment(Element.ALIGN_CENTER);
        workPara.add (new Phrase ("Due\n", new Font (Font.HELVETICA, 9, Font.BOLDITALIC, new CMYKColor(0f, 0f, 0f, 0f))));
        workPara.add (new Phrase ("Date", new Font (Font.HELVETICA, 9, Font.BOLDITALIC, new CMYKColor(0f, 0f, 0f, 0f))));

        topDetail.addCell(workCell);

        workPara = new Paragraph();
        workCell = new PdfPCell(workPara);
        workCell.setBackgroundColor(new CMYKColor(.73f, .44f, 00f, .05f));
        workCell.setHorizontalAlignment(Element.ALIGN_CENTER);
        workPara.add (new Phrase ("Open\n", new Font (Font.HELVETICA, 9, Font.BOLDITALIC, new CMYKColor(0f, 0f, 0f, 0f))));
        workPara.add (new Phrase ("Amount", new Font (Font.HELVETICA, 9, Font.BOLDITALIC, new CMYKColor(0f, 0f, 0f, 0f))));

        topDetail.addCell(workCell);

        doc.add(topDetail);

        doc.add(detailBlank);

	}


	private static String checkMinus(String inputAmt) throws Exception
	{
		int len = inputAmt.length();
		String minusSign = inputAmt.substring(len - 1, len);
		String wrkAmt2 = inputAmt;

		if(minusSign.equals("-"))
		{
		    String wrkAmt1 = inputAmt.substring(0, len - 1);
		    wrkAmt2 = "-" + wrkAmt1.trim();
		}

        return wrkAmt2;
	}

}


class BoxEvent implements PdfPCellEvent
{
	private PdfWriter writer;
	private String name;

	public BoxEvent(PdfWriter writer, String name)
	{
		this.writer = writer;
		this.name = name;
	}

	public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
	{
		float llx = position.left() + ((position.right() -  position.left()) / 2f) - 5f;
		float lly = position.bottom() + ((position.top() -  position.bottom()) / 2f) - 5f;
		try
		{
			RadioCheckField rf = new RadioCheckField(writer, new Rectangle(llx, lly, llx + 10, lly + 10), name, "Yes");

			rf.setCheckType(RadioCheckField.TYPE_CHECK);
			rf.setBorderWidth(BaseField.BORDER_WIDTH_THIN);
			rf.setBorderColor(Color.black);
			rf.setBackgroundColor(Color.white);
			rf.setChecked(true);

			PdfFormField ff = rf.getCheckField();
			writer.addAnnotation(ff);
		}
		catch (Exception e)
		{
			throw new ExceptionConverter(e);
		}
	}
}
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to