Advertisement
bbbbas

DeckOfCardsiText

May 6th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  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 String symbolsCheck() {
  62. String result = "";
  63. for (char i = 167; i < 171; i++) {
  64. result += i;
  65. }
  66. return result;
  67. }
  68.  
  69. public PdfPTable createTable() {
  70.  
  71. String cardFace = "";
  72. PdfPTable cards = new PdfPTable(8);
  73.  
  74. cards.setWidthPercentage(40);
  75. cards.getDefaultCell().setFixedHeight(45);
  76. cards.getDefaultCell().setPadding(2);
  77.  
  78.  
  79. for (int i = 2; i <= 14; i++) {
  80. switch (i) {
  81. case 11:
  82. cardFace = "J";
  83. break;
  84. case 12:
  85. cardFace = "Q";
  86. break;
  87. case 13:
  88. cardFace = "K";
  89. break;
  90. case 14:
  91. cardFace = "A";
  92. break;
  93. default:
  94. cardFace = String.format("%d", i);
  95. break;
  96. }
  97.  
  98. PdfPCell cell = new PdfPCell();
  99. cell.setBorder(PdfPCell.NO_BORDER);
  100.  
  101. char p = 167;
  102. cards.addCell((new Paragraph("\n "+ cardFace + p, NORMAL)));
  103. cards.addCell(cell);
  104.  
  105. char q = 170;
  106. cards.addCell(new Paragraph("\n " + cardFace + q, NORMAL));
  107. cards.addCell(cell);
  108.  
  109. for (char j = 168; j < 170; j++) {
  110.  
  111. RED.setColor(BaseColor.RED);
  112. cards.addCell(new Paragraph("\n " + cardFace + j, RED));
  113. cards.addCell(cell);
  114.  
  115. }
  116. }
  117.  
  118. return cards;
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement