Advertisement
coasterka

#1DeckOfCardsPDF

May 5th, 2014
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 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.pdf.BaseFont;
  9. import com.itextpdf.text.pdf.PdfPTable;
  10. import com.itextpdf.text.Font.FontFamily;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13.  
  14. public class DeckOfCardsPDF{
  15.    
  16.     public static final String RESULT        
  17.     = "C:/Users/Hristina/workspace/Intro-Java-Homework/Deck-of-Cards.pdf";
  18.    
  19.     public static final Font NORMAL = new Font(FontFamily.SYMBOL, 12);
  20.    
  21.     public static void main(String[] args) throws DocumentException, IOException {
  22.         new DeckOfCardsTest().createPdf(RESULT);
  23.     }
  24.    
  25.     public void createPdf(String filename) throws DocumentException, IOException {
  26.  
  27.         String result = "";
  28.         char color = ' ';
  29.  
  30.         char clubs = '\u2663';
  31.             char diamonds = '\u2666';
  32.             char hearts = '\u2665';
  33.             char spades = '\u2660';
  34.  
  35.             String jack = "J";
  36.             String queen = "Q";
  37.             String knight = "K";
  38.             String ace = "A";
  39.        
  40.             try{
  41.                 Document document = new Document();
  42.                 PdfWriter.getInstance(document, new FileOutputStream(filename));
  43.                 document.open();
  44.            
  45.                 PdfPTable table = new PdfPTable(4);
  46.                 table.setWidthPercentage(100);
  47.             table.getDefaultCell().setFixedHeight(180);
  48.            
  49.             BaseFont baseFont = BaseFont.createFont("C:/Windows/Fonts/Arial.ttf", BaseFont.IDENTITY_H, true);
  50.             Font black = new Font(baseFont, 70f, 0, BaseColor.BLACK);
  51.             Font red = new Font(baseFont, 70f, 0, BaseColor.RED);
  52.            
  53.                     for (int i = 2; i <= 14; i++){            
  54.                         for (int j = 1; j <= 4; j++){
  55.                                 switch (i){
  56.                                     case 10: result = "10"; break;
  57.                                     case 11: result = " " + jack; break;
  58.                                     case 12: result = " " + queen; break;                      
  59.                                     case 13: result = " " + knight; break;
  60.                                     case 14: result = " " + ace; break;
  61.                                     default: result = " " + i; break;
  62.                                 }
  63.                                 switch(j){
  64.                                     case 1:{
  65.                                         color = clubs;
  66.                                         table.addCell(new Paragraph(result + color + " ", black));
  67.                                         break;
  68.                                     }
  69.                                     case 2:{
  70.                                         color = diamonds;
  71.                                         table.addCell(new Paragraph(result + color + " ", red));
  72.                                         break;
  73.                                     }
  74.                                     case 3:{
  75.                                         color = hearts;
  76.                                         table.addCell(new Paragraph(result + color + " ", red));
  77.                                         break;
  78.                                     }
  79.                                     case 4:{
  80.                                         color = spades;
  81.                                         table.addCell(new Paragraph(result + color + " ", black));
  82.                                         break;
  83.                                     }
  84.                                 }
  85.                         }
  86.                     }
  87.                     document.add(table);
  88.                     document.close();
  89.             }
  90.             catch (Exception e){
  91.                 e.printStackTrace();
  92.             }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement