Advertisement
f1mp3r

Print PDF deck

May 12th, 2014
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. import com.itextpdf.text.BaseColor;
  2. import com.itextpdf.text.Document;
  3. import com.itextpdf.text.DocumentException;
  4. import com.itextpdf.text.Element;
  5. import com.itextpdf.text.Font;
  6. import com.itextpdf.text.Paragraph;
  7. import com.itextpdf.text.pdf.BaseFont;
  8. import com.itextpdf.text.pdf.PdfPTable;
  9. import com.itextpdf.text.pdf.PdfWriter;
  10.  
  11. import java.io.FileOutputStream;
  12. import java.io.FileNotFoundException;
  13. import java.io.IOException;
  14.  
  15. public class GeneratePdf {
  16.  
  17.     public static void main(String[] args) throws IOException {
  18.         Document document = new Document();
  19.         try {
  20.             BaseFont baseFont = BaseFont.createFont("tahoma.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  21.             Font red = new Font(baseFont, 24, Font.NORMAL, BaseColor.RED);
  22.             Font black = new Font(baseFont,24, Font.NORMAL, BaseColor.BLACK);
  23.             String card = "";
  24.             //char[] colors = new char[] {'♣','♦','♥','♠'};
  25.             char hearts = '♥';
  26.             char diamonds = '♦';
  27.             char clubs = '♠';
  28.             char spades = '♣';
  29.            
  30.             PdfWriter.getInstance(document, new FileOutputStream("Deck-of-Cards.pdf"));
  31.             document.open();
  32.             PdfPTable table = new PdfPTable(4);
  33.             table.setWidthPercentage(50);
  34.             table.getDefaultCell().setFixedHeight(100);
  35.             table.getDefaultCell().setBorderColor(BaseColor.BLUE);
  36.             table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
  37.             table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
  38.             table.getDefaultCell().setLeading(15f, 0f);
  39.            
  40.             for(int i = 2; i <=14; i++){
  41.                 switch(i){
  42.                     case 11:
  43.                         card = "J";
  44.                     break;
  45.                     case 12:
  46.                         card = "Q";
  47.                     break;
  48.                     case 13:
  49.                         card = "K";
  50.                     break;
  51.                     case 14:
  52.                         card = "A";
  53.                     break;
  54.                     default:
  55.                         card = Integer.toString(i);
  56.                     break;
  57.                 }
  58.                 table.addCell(new Paragraph(card+spades,black));
  59.                 table.addCell(new Paragraph(card+diamonds,red));
  60.                 table.addCell(new Paragraph(card+hearts,red));
  61.                 table.addCell(new Paragraph(card+clubs,black));
  62.             }
  63.            
  64.             document.add(table);
  65.             document.close();
  66.            
  67.         } catch (DocumentException e) {
  68.             e.printStackTrace();
  69.         } catch (FileNotFoundException e) {
  70.             e.printStackTrace();
  71.         }
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement