Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Write a program to generate a PDF document called Deck-of-Cards.pdf
- and print in it a standard deck of 52 cards, following one after
- another. Each card should be a rectangle holding its face and suit.*/
- //imports from 1.IntroductionToJavaHW/lib
- //right+click on the current Project-->Libraries-->add external jars
- //downloaded from http://sourceforge.net/projects/itext/
- import com.itextpdf.text.*;
- import com.itextpdf.text.pdf.*;
- import java.io.FileOutputStream;
- public class GenerateAPDF {
- public static void main(String[] args) {
- //creating two string for adding each card in a cell
- String[] suits = {"\u2660","\u2665","\u2666","\u2663"};
- String[] ranks = {"2","3","4","5","6","7","8","9","10","J","K","Q","A"};
- //structure to make our pdf file
- try {
- Document document = new Document();
- PdfWriter.getInstance(document, new FileOutputStream("Deck-of-Cards.pdf"));
- document.open();
- //table params
- PdfPTable table = new PdfPTable(13);
- table.setWidthPercentage(90);
- table.getDefaultCell().setFixedHeight(60);
- //times.ttf to see the suits loc-/1.IntroductionToJavaHW
- //downloded from http://www.2shared.com/complete/7WJc2uEn/times.html
- BaseFont baseFont = BaseFont.createFont("times.ttf", BaseFont.IDENTITY_H, true);
- Font deckFont = new Font(baseFont, 12f);
- for (int i = 0; i < suits.length; i++){
- for (int j = 0; j < ranks.length; j++){
- //setting the red suits
- if(i == 1 || i ==2)
- {
- deckFont.setColor(BaseColor.RED);
- }
- //adding content in each cell
- table.addCell(new Paragraph(ranks[j]+suits[i],deckFont));
- //space between each row
- table.setSpacingAfter(5);
- }
- document.add(table);
- table.deleteLastRow();
- //resets the color
- deckFont.setColor(BaseColor.BLACK);
- }
- document.close();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement