Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileOutputStream;
- import java.io.IOException;
- import com.itextpdf.text.BaseColor;
- import com.itextpdf.text.Document;
- import com.itextpdf.text.DocumentException;
- import com.itextpdf.text.Font;
- import com.itextpdf.text.pdf.BaseFont;
- import com.itextpdf.text.pdf.PdfPTable;
- import com.itextpdf.text.Font.FontFamily;
- import com.itextpdf.text.Paragraph;
- import com.itextpdf.text.pdf.PdfWriter;
- public class DeckOfCardsPDF{
- public static final String RESULT
- = "C:/Users/Hristina/workspace/Intro-Java-Homework/Deck-of-Cards.pdf";
- public static final Font NORMAL = new Font(FontFamily.SYMBOL, 12);
- public static void main(String[] args) throws DocumentException, IOException {
- new DeckOfCardsTest().createPdf(RESULT);
- }
- public void createPdf(String filename) throws DocumentException, IOException {
- String result = "";
- char color = ' ';
- char clubs = '\u2663';
- char diamonds = '\u2666';
- char hearts = '\u2665';
- char spades = '\u2660';
- String jack = "J";
- String queen = "Q";
- String knight = "K";
- String ace = "A";
- try{
- Document document = new Document();
- PdfWriter.getInstance(document, new FileOutputStream(filename));
- document.open();
- PdfPTable table = new PdfPTable(4);
- table.setWidthPercentage(100);
- table.getDefaultCell().setFixedHeight(180);
- BaseFont baseFont = BaseFont.createFont("C:/Windows/Fonts/Arial.ttf", BaseFont.IDENTITY_H, true);
- Font black = new Font(baseFont, 70f, 0, BaseColor.BLACK);
- Font red = new Font(baseFont, 70f, 0, BaseColor.RED);
- for (int i = 2; i <= 14; i++){
- for (int j = 1; j <= 4; j++){
- switch (i){
- case 10: result = "10"; break;
- case 11: result = " " + jack; break;
- case 12: result = " " + queen; break;
- case 13: result = " " + knight; break;
- case 14: result = " " + ace; break;
- default: result = " " + i; break;
- }
- switch(j){
- case 1:{
- color = clubs;
- table.addCell(new Paragraph(result + color + " ", black));
- break;
- }
- case 2:{
- color = diamonds;
- table.addCell(new Paragraph(result + color + " ", red));
- break;
- }
- case 3:{
- color = hearts;
- table.addCell(new Paragraph(result + color + " ", red));
- break;
- }
- case 4:{
- color = spades;
- table.addCell(new Paragraph(result + color + " ", black));
- break;
- }
- }
- }
- }
- document.add(table);
- document.close();
- }
- catch (Exception e){
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement