Advertisement
viraldim

Untitled

May 5th, 2014
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. /*Write a program to generate a PDF document called Deck-of-Cards.pdf
  2. and print in it a standard deck of 52 cards, following one after
  3. another. Each card should be a rectangle holding its face and suit.*/
  4.  
  5. //imports from 1.IntroductionToJavaHW/lib
  6. //right+click on the current Project-->Libraries-->add external jars
  7. //downloaded from http://sourceforge.net/projects/itext/
  8. import com.itextpdf.text.*;
  9. import com.itextpdf.text.pdf.*;
  10.  
  11. import java.io.FileOutputStream;
  12.  
  13. public class GenerateAPDF {
  14.  
  15.     public static void main(String[] args) {
  16.             //creating two string for adding each card in a cell
  17.             String[] suits = {"\u2660","\u2665","\u2666","\u2663"};
  18.             String[] ranks = {"2","3","4","5","6","7","8","9","10","J","K","Q","A"};
  19.            
  20.             //structure to make our pdf file
  21.             try {
  22.                 Document document = new Document();
  23.                 PdfWriter.getInstance(document, new FileOutputStream("Deck-of-Cards.pdf"));                      
  24.                 document.open();
  25.                //table params
  26.                 PdfPTable table = new PdfPTable(13);
  27.                 table.setWidthPercentage(90);
  28.                 table.getDefaultCell().setFixedHeight(60);
  29.                
  30.                 //times.ttf to see the suits loc-/1.IntroductionToJavaHW
  31.                 //downloded from http://www.2shared.com/complete/7WJc2uEn/times.html
  32.                 BaseFont baseFont = BaseFont.createFont("times.ttf", BaseFont.IDENTITY_H, true);
  33.                 Font deckFont = new Font(baseFont, 12f);
  34.                
  35.                 for (int i = 0; i < suits.length; i++){            
  36.                     for (int j = 0; j < ranks.length; j++){
  37.                         //setting the red suits
  38.                          if(i == 1 || i ==2)
  39.                          {
  40.                              deckFont.setColor(BaseColor.RED);                           
  41.                          }
  42.                          //adding content in each cell
  43.                          table.addCell(new Paragraph(ranks[j]+suits[i],deckFont));
  44.                          //space between each row
  45.                                                  table.setSpacingAfter(5);                       
  46.                     }
  47.                     document.add(table);
  48.                     table.deleteLastRow();
  49.                     //resets the color
  50.                     deckFont.setColor(BaseColor.BLACK);
  51.                 }              
  52.                 document.close();                      
  53.         }
  54.         catch (Exception e) {
  55.                 e.printStackTrace();
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement