AlexKondov

Java Generate PDF Deck of Cards

May 6th, 2014
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 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.pdf.BaseFont;
  6. import com.itextpdf.text.pdf.PdfPTable;
  7. import com.itextpdf.text.pdf.PdfWriter;
  8.  
  9. import java.io.FileOutputStream;
  10.  
  11. public class DeckOfCards {
  12.        
  13.         public static void main(String[] args) {
  14.                 try {
  15.                         Document document = new Document();
  16.                         PdfWriter.getInstance(document, new FileOutputStream("Deck-Of-Cards.pdf"));                      
  17.                         document.open();
  18.                        
  19.                         PdfPTable table = new PdfPTable(4);
  20.                         table.setWidthPercentage(110);
  21.                         table.getDefaultCell().setFixedHeight(180);
  22.                        
  23.                         BaseFont baseFont = BaseFont.createFont("times.ttf", BaseFont.IDENTITY_H, true);
  24.                         Font black = new Font(baseFont, 75f, 0, BaseColor.BLACK);
  25.                         Font red = new Font(baseFont, 75f, 0, BaseColor.RED);
  26.                        
  27.                         char[] suits = new char[] {'♠', '♥', '♦', '♣'};
  28.                         String[] values = new String[] {"A", "2", "3", "4", "5", "6", "7", "8",
  29.                                                                         "9", "10","J", "Q", "K"};
  30.                        
  31.                         for (int i = 0; i < suits.length; i++) {
  32.                                 for (int j = 0; j < values.length; j++) {
  33.                                         if (suits[i] == '♠' || suits[i] == '♣') {
  34.                                                 table.addCell(new Paragraph(suits[i] + " " + values[j] + " ", black));                                
  35.                                         }
  36.                                         else {
  37.                                                 table.addCell(new Paragraph(suits[i] + " " + values[j] + " ", red));
  38.                                         }
  39.                                 }
  40.                         }
  41.                        
  42.                         document.add(table);
  43.                         document.close();                      
  44.                 }
  45.                 catch (Exception e) {
  46.                         e.printStackTrace();
  47.                 }
  48.         }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment