Advertisement
coasterka

#1PDFDeckFinal

May 9th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. import java.io.FileOutputStream;
  2. import java.io.IOException;
  3.  
  4. import com.itextpdf.text.BaseColor;
  5. import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.Font;
  8. import com.itextpdf.text.FontFactory;
  9. import com.itextpdf.text.pdf.BaseFont;
  10. import com.itextpdf.text.pdf.PdfPTable;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13.  
  14. public class DeckOfCardsPDF{
  15.    
  16.     public static void main(String[] args) throws DocumentException, IOException {
  17.        
  18.         //declaring variables
  19.         String card = "";
  20.         char color = ' ';
  21.         char clubs = '\u2663';
  22.             char diamonds = '\u2666';
  23.             char hearts = '\u2665';
  24.             char spades = '\u2660';
  25.             String jack = "J";
  26.             String queen = "Q";
  27.             String knight = "K";
  28.             String ace = "A";
  29.        
  30.             try {
  31.            
  32.                 //creating a new document
  33.                 Document document = new Document();
  34.                 //setting the name of the document
  35.                 PdfWriter.getInstance(document, new FileOutputStream("DeckOfCards.pdf"));
  36.                 //opening the document
  37.                 document.open();
  38.                 //creating a table to put the cards in it
  39.                 PdfPTable table = new PdfPTable(4);
  40.                 //table preferences
  41.                 table.setWidthPercentage(100);
  42.                 table.getDefaultCell().setFixedHeight(180);
  43.                 //base font of the whole table (currently using Arial as it is a default font
  44.                 BaseFont baseFont = BaseFont.createFont("arial.ttf", BaseFont.IDENTITY_H, true);
  45.            
  46.                 //creating some font instances which we will be using later
  47.                 Font black = new Font (baseFont, 70f, 0, BaseColor.BLACK);
  48.                 Font red = new Font (baseFont, 70f, 0, BaseColor.RED);
  49.            
  50.                 //printing the cards in the table
  51.                 for (int i = 2; i <= 14; i++){
  52.                         for (int j = 1; j <= 4; j++){
  53.                             switch (i){
  54.                                 case 10: card = "10"; break;
  55.                                 case 11: card = " " + jack; break;
  56.                                 case 12: card = " " + queen; break;                    
  57.                                 case 13: card = " " + knight; break;
  58.                                 case 14: card = " " + ace; break;
  59.                                 default: card = " " + i; break;
  60.                             }
  61.                             switch(j){
  62.                                 case 1:{
  63.                                 color = clubs;
  64.                             table.addCell(new Paragraph(card + color + " ", black));
  65.                             break;
  66.                                 }
  67.                                 case 2:{
  68.                                     color = diamonds;
  69.                                     table.addCell(new Paragraph(card + color + " ", red));
  70.                                     break;
  71.                                 }
  72.                                 case 3:{
  73.                                     color = hearts;
  74.                                     table.addCell(new Paragraph(card + color + " ", red));
  75.                                     break;
  76.                                 }
  77.                                 case 4:{
  78.                                     color = spades;
  79.                                     table.addCell(new Paragraph(card + color + " ", black));
  80.                                     break;
  81.                                 }
  82.                             }
  83.                     }
  84.             }
  85.            
  86.             //adding the table to the document
  87.             document.add(table);
  88.            
  89.             //close the document
  90.             document.close();
  91.         }
  92.         catch (Exception e){
  93.             e.printStackTrace();
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement