Advertisement
tr00per92

CardDeckPDF

May 5th, 2014
1,249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 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 Generate_PDF {
  12.    
  13.     public static void main(String[] args) {
  14.         try {
  15.             Document document = new Document();
  16.             PdfWriter.getInstance(document, new FileOutputStream("MyFirstFile.pdf"));          
  17.             document.open();
  18.            
  19.             PdfPTable table = new PdfPTable(4);
  20.             table.setWidthPercentage(100);
  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.             String card = "";
  28.             char color = ' ';
  29.            
  30.             for (int i = 2; i <= 14; i++) {
  31.                 switch (i) {
  32.                 case 10: card = "10"; break;
  33.                 case 11: card = " J"; break;
  34.                 case 12: card = " Q"; break;
  35.                 case 13: card = " K"; break;
  36.                 case 14: card = " A"; break;
  37.                 default: card = " " + i; break;
  38.                 }
  39.                 for (int j = 1; j <= 4; j++) {
  40.                     switch (j) {
  41.                     case 1: color = '♣'; table.addCell(new Paragraph(card + color + " ", black)); break;
  42.                     case 2: color = '♦'; table.addCell(new Paragraph(card + color + " ", red)); break;
  43.                     case 3: color = '♠'; table.addCell(new Paragraph(card + color + " ", black)); break;
  44.                     case 4: color = '♥'; table.addCell(new Paragraph(card + color + " ", red)); break;
  45.                     }
  46.                 }
  47.             }
  48.             document.add(table);
  49.             document.close();          
  50.         }
  51.         catch (Exception e) {
  52.             e.printStackTrace();
  53.         }
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement