Advertisement
bbbbas

DeckOfCardsiText

May 6th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package problem9;
  2.  
  3. /**
  4. *
  5. * @author bas
  6. */
  7.  
  8.  
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12.  
  13. import com.itextpdf.text.BaseColor;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Font.FontFamily;
  17. import com.itextpdf.text.Paragraph;
  18. import com.itextpdf.text.Font;
  19. import com.itextpdf.text.pdf.PdfPCell;
  20. import com.itextpdf.text.pdf.PdfPTable;
  21. import com.itextpdf.text.pdf.PdfWriter;
  22.  
  23.  
  24. public class DeckOfCardsiText {
  25.  
  26. public static final String RESULT = "deckOfCards.pdf";
  27.  
  28.  
  29. public static void main(String[] args) throws DocumentException,
  30. IOException {
  31. new DeckOfCardsiText().createPdf(RESULT);
  32. }
  33.  
  34. /**
  35. * Creates a PDF document.
  36. *
  37. * @param filename
  38. * the path to the new PDF document
  39. * @throws DocumentException
  40. * @throws IOException
  41. */
  42. public static final Font NORMAL = new Font(FontFamily.SYMBOL, 12);
  43. public static final Font RED = new Font(FontFamily.SYMBOL, 12);
  44.  
  45. public void createPdf(String filename) throws DocumentException,
  46. IOException {
  47.  
  48. // step 1
  49. Document document = new Document();
  50. // step 2
  51. PdfWriter.getInstance(document, new FileOutputStream(filename));
  52. // step 3
  53. document.open();
  54. // step 4
  55.  
  56. document.add(createTable());
  57. // step 5
  58. document.close();
  59. }
  60.  
  61. public PdfPTable createTable() {
  62.  
  63. String cardFace = "";
  64. PdfPTable cards = new PdfPTable(8);
  65.  
  66. cards.setWidthPercentage(40);
  67. cards.getDefaultCell().setFixedHeight(45);
  68. cards.getDefaultCell().setPadding(2);
  69.  
  70.  
  71. for (int i = 2; i <= 14; i++) {
  72. switch (i) {
  73. case 11:
  74. cardFace = "J";
  75. break;
  76. case 12:
  77. cardFace = "Q";
  78. break;
  79. case 13:
  80. cardFace = "K";
  81. break;
  82. case 14:
  83. cardFace = "A";
  84. break;
  85. default:
  86. cardFace = String.format("%d", i);
  87. break;
  88. }
  89.  
  90. PdfPCell cell = new PdfPCell();
  91. cell.setBorder(PdfPCell.NO_BORDER);
  92.  
  93. char p = 167;
  94. cards.addCell((new Paragraph("\n "+ cardFace + p, NORMAL)));
  95. cards.addCell(cell);
  96.  
  97. char q = 170;
  98. cards.addCell(new Paragraph("\n " + cardFace + q, NORMAL));
  99. cards.addCell(cell);
  100.  
  101. for (char j = 168; j < 170; j++) {
  102. RED.setColor(BaseColor.RED);
  103. cards.addCell(new Paragraph("\n " + cardFace + j, RED));
  104. cards.addCell(cell);
  105. }
  106. }
  107. return cards;
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement