Advertisement
dimipan80

Generate a PDF by External Library

Sep 3rd, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 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 another.
  3.  * Each card should be a rectangle holding its face and suit.
  4.  * You are free to choose the size of each card, the spacing between the cards,
  5.  * how many cards to put in each line, etc.
  6.  * Note: you will need to use an external Java library for creating PDF documents.
  7.  * Put your JAR files in a folder called "lib" (this is a standard approach
  8.  * in Java projects) and reference them in the build path. */
  9.  
  10. import java.io.FileOutputStream;
  11. import java.util.Locale;
  12. import java.util.Random;
  13.  
  14. import com.itextpdf.text.BaseColor;
  15. import com.itextpdf.text.Document;
  16. import com.itextpdf.text.Font;
  17. import com.itextpdf.text.Paragraph;
  18. import com.itextpdf.text.pdf.BaseFont;
  19. import com.itextpdf.text.pdf.PdfPTable;
  20. import com.itextpdf.text.pdf.PdfWriter;
  21.  
  22. public class _09_GeneratePDFByExternalLibrary {
  23.  
  24.     public static void main(String[] args) {
  25.         // TODO Auto-generated method stub
  26.         Locale.setDefault(Locale.ROOT);
  27.  
  28.                 /*
  29.          * First we must import necessary External Java library for generating
  30.          * PDF files. Here will using "iText®, a JAVA PDF library" from URL:
  31.          * "http://sourceforge.net/". Next extract only "itextpdf-5.5.2.jar"
  32.          * from whole download .zip file and put it in "lib" folder in our
  33.          * project! The finally we must configure Build Path to location of that
  34.          * .jar file!
  35.          */
  36.         Document document = new Document();
  37.         try {
  38.             PdfWriter.getInstance(document, new FileOutputStream(
  39.                     "Deck-of-Cards.pdf"));
  40.             document.open();
  41.  
  42.             PdfPTable table = new PdfPTable(4);
  43.             table.setWidthPercentage(100);
  44.             table.getDefaultCell().setFixedHeight(180);
  45.  
  46.             /*
  47.              * Now will using some True Type system font. Here will using that
  48.              * font "times.ttf":
  49.              */
  50.             BaseFont baseFont = BaseFont.createFont("FreeSerifBold.ttf",
  51.                     BaseFont.IDENTITY_H, true);
  52.             Font blackColorFont = new Font(baseFont, 65f, 0, BaseColor.BLACK);
  53.             Font redColorFont = new Font(baseFont, 65f, 0, BaseColor.RED);
  54.  
  55.             String[] deckOfCards = createFullDeckOf52PlayCards();
  56.             String[] randomizedCards = randomizeFullDeckOf52UniqueCards(deckOfCards);
  57.  
  58.             for (String card : randomizedCards) {
  59.                 boolean isRedCard = card.contains("" + '\u2665')
  60.                         || card.contains("" + '\u2666');
  61.                 if (isRedCard) {
  62.                     table.addCell(new Paragraph(card, redColorFont));
  63.                 } else {
  64.                     table.addCell(new Paragraph(card, blackColorFont));
  65.                 }
  66.             }
  67.  
  68.             document.add(table);
  69.             document.close();
  70.         } catch (Exception e) {
  71.             // TODO Auto-generated catch block
  72.             e.printStackTrace();
  73.         }
  74.     }
  75.  
  76.     private static String[] createFullDeckOf52PlayCards() {
  77.         // TODO Auto-generated method stub
  78.         String[] cardsFaces = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
  79.                 "J", "Q", "K", "A" };
  80.  
  81.         char[] suitCards = { '\u2663', '\u2666', '\u2665', '\u2660' };
  82.  
  83.         String[] fullDeck = new String[52];
  84.         int index = 0;
  85.         for (int face = 0; face < cardsFaces.length; face++) {
  86.             for (int suit = 0; suit < suitCards.length; suit++) {
  87.                 fullDeck[index] = cardsFaces[face] + " " + suitCards[suit];
  88.                 index++;
  89.             }
  90.         }
  91.  
  92.         return fullDeck;
  93.     }
  94.  
  95.     private static String[] randomizeFullDeckOf52UniqueCards(String[] deck) {
  96.         // TODO Auto-generated method stub
  97.         boolean[] drawnCards = new boolean[deck.length];
  98.         Random randomGen = new Random();
  99.         String[] randomCards = new String[deck.length];
  100.         for (int i = 0; i < deck.length; i++) {
  101.             int randomCard = randomGen.nextInt(deck.length);
  102.             if (drawnCards[randomCard]) {
  103.                 i--;
  104.             } else {
  105.                 drawnCards[randomCard] = true;
  106.                 randomCards[i] = deck[randomCard];
  107.             }
  108.         }
  109.  
  110.         return randomCards;
  111.     }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement