Advertisement
Guest User

3 Hours of my life

a guest
May 6th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. import com.itextpdf.text.BaseColor;
  2. import com.itextpdf.text.Document;
  3. import com.itextpdf.text.Font;
  4. import com.itextpdf.text.Paragraph;
  5. import com.itextpdf.text.Rectangle;
  6. import com.itextpdf.text.pdf.BaseFont;
  7. import com.itextpdf.text.pdf.PdfPCell;
  8. import com.itextpdf.text.pdf.PdfPTable;
  9. import com.itextpdf.text.pdf.PdfWriter;
  10.  
  11. import java.io.FileOutputStream;
  12.  
  13. public class DeckOfCards {
  14.    
  15.     public static void main(String[] args) {
  16.         try {
  17.             Document document = new Document();
  18.             PdfWriter.getInstance(document, new FileOutputStream("DeckOfCards.pdf"));          
  19.             document.open();
  20.            
  21.             PdfPTable table = new PdfPTable(4);
  22.             table.setWidthPercentage(100);
  23.             table.getDefaultCell().setFixedHeight(180);
  24.             table.getDefaultCell().setBorder(0);//Removes parent table border.
  25.            
  26.             BaseFont baseFont = BaseFont.createFont("times.ttf", BaseFont.IDENTITY_H, true);
  27.             Font black = new Font(baseFont, 40f, 0, BaseColor.BLACK);
  28.             Font red = new Font(baseFont, 40f, 0, BaseColor.RED);
  29.             char color =' ';
  30.             String card = " ";
  31.             for (int i = 2; i <= 14; i++) {
  32.                 switch (i) {
  33.                 case 10: card = "10"; break;
  34.                 case 11: card = " J"; break;
  35.                 case 12: card = " Q"; break;
  36.                 case 13: card = " K"; break;
  37.                 case 14: card = " A"; break;
  38.                 default: card = " " + i; break;
  39.                 }
  40.                 for (int j = 1; j <= 4; j++) {
  41.                     PdfPCell cell1 = new PdfPCell();
  42.                     cell1.setBorder(Rectangle.NO_BORDER);  //Removes parent cell border
  43.                     PdfPCell cell2 = new PdfPCell();
  44.                     cell2.setBorder(Rectangle.NO_BORDER);  
  45.                     PdfPCell cell3 = new PdfPCell();
  46.                     cell3.setBorder(Rectangle.NO_BORDER);  
  47.                     PdfPCell cell4 = new PdfPCell();
  48.                     cell4.setBorder(Rectangle.NO_BORDER);  
  49.                     PdfPTable nestedTable = new PdfPTable(1);//A separate table used to create the card shape and spacing between cards(is inserted in a cell of the parent table)
  50.                     nestedTable.getDefaultCell().setPaddingTop(56);      //
  51.                     nestedTable.getDefaultCell().setPaddingBottom(64);//
  52.                     nestedTable.getDefaultCell().setPaddingLeft(17);//Centers the card number and color
  53.                     nestedTable.getDefaultCell().setPaddingRight(17);//
  54.                     nestedTable.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY); //sets card background color
  55.                     switch (j) { //Tr00peR rulz.
  56.                     case 1: color = '♣'; nestedTable.addCell(new Paragraph(card + color + " ", black));
  57.                     cell1.addElement(nestedTable);table.addCell(cell1);//Ads a cell to the nested table
  58.                     break;
  59.                     case 2: color = '♦'; nestedTable.addCell(new Paragraph(card + color + " ", red));
  60.                     cell2.addElement(nestedTable);table.addCell(cell2);
  61.                     break;
  62.                     case 3: color = '♠'; nestedTable.addCell(new Paragraph(card + color + " ", black));
  63.                     cell3.addElement(nestedTable);table.addCell(cell3);
  64.                     break;
  65.                     case 4: color = '♥'; nestedTable.addCell(new Paragraph(card + color + " ", red));
  66.                     cell4.addElement(nestedTable);table.addCell(cell4);
  67.                     break;
  68.                     }
  69.                 }
  70.             }
  71.             document.add(table);
  72.             document.close();          
  73.         }
  74.         catch (Exception e) {
  75.             e.printStackTrace();
  76.         }
  77.     }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement