Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package problem9;
- /**
- *
- * @author bas
- */
- import java.io.FileOutputStream;
- import java.io.IOException;
- import com.itextpdf.text.BaseColor;
- import com.itextpdf.text.Document;
- import com.itextpdf.text.DocumentException;
- import com.itextpdf.text.Font.FontFamily;
- import com.itextpdf.text.Paragraph;
- import com.itextpdf.text.Font;
- import com.itextpdf.text.pdf.PdfPCell;
- import com.itextpdf.text.pdf.PdfPTable;
- import com.itextpdf.text.pdf.PdfWriter;
- public class DeckOfCardsiText {
- public static final String RESULT = "deckOfCards.pdf";
- public static void main(String[] args) throws DocumentException,
- IOException {
- new DeckOfCardsiText().createPdf(RESULT);
- }
- /**
- * Creates a PDF document.
- *
- * @param filename
- * the path to the new PDF document
- * @throws DocumentException
- * @throws IOException
- */
- public static final Font NORMAL = new Font(FontFamily.SYMBOL, 12);
- public static final Font RED = new Font(FontFamily.SYMBOL, 12);
- public void createPdf(String filename) throws DocumentException,
- IOException {
- // step 1
- Document document = new Document();
- // step 2
- PdfWriter.getInstance(document, new FileOutputStream(filename));
- // step 3
- document.open();
- // step 4
- document.add(createTable());
- // step 5
- document.close();
- }
- public PdfPTable createTable() {
- String cardFace = "";
- PdfPTable cards = new PdfPTable(8);
- cards.setWidthPercentage(40);
- cards.getDefaultCell().setFixedHeight(45);
- cards.getDefaultCell().setPadding(2);
- for (int i = 2; i <= 14; i++) {
- switch (i) {
- case 11:
- cardFace = "J";
- break;
- case 12:
- cardFace = "Q";
- break;
- case 13:
- cardFace = "K";
- break;
- case 14:
- cardFace = "A";
- break;
- default:
- cardFace = String.format("%d", i);
- break;
- }
- PdfPCell cell = new PdfPCell();
- cell.setBorder(PdfPCell.NO_BORDER);
- char p = 167;
- cards.addCell((new Paragraph("\n "+ cardFace + p, NORMAL)));
- cards.addCell(cell);
- char q = 170;
- cards.addCell(new Paragraph("\n " + cardFace + q, NORMAL));
- cards.addCell(cell);
- for (char j = 168; j < 170; j++) {
- RED.setColor(BaseColor.RED);
- cards.addCell(new Paragraph("\n " + cardFace + j, RED));
- cards.addCell(cell);
- }
- }
- return cards;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement