Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import com.pdfjet.*;
- public class GeneratePDFDeck {
- /*
- * 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. A few examples are shown below:
- * A ♠ * 2 ♥ * Q ♦ * K ♣ * J ♦ * 9 ♦ * 7 ♠
- * You are free to choose the size of each card, the spacing between the cards,
- * how many cards to put in each line, etc.
- * Note: you will need to use an external Java library for creating PDF documents.
- * Find some in Internet. Put your JAR files in a folder called "lib"
- * (this is a standard approach in Java projects) and reference them in the build path.
- * Hint: solve the problem step by step:
- * 1. Generate the deck of cards and print it at the console (the console in Eclipse fully supports Unicode).
- * 2. Find a Java library for generating PDF documents and play with it.
- * Try to print some string in a PDF document.
- * 3. Print the cards in PDF file (without the rectangular border).
- * 4. Try to change the colors of the red cards.
- * 5. Try to add the rectangular border around each card, e.g. by putting tables in the PDF.
- */
- public GeneratePDFDeck() throws Exception {
- PDF pdf = new PDF(
- new BufferedOutputStream(
- new FileOutputStream("Deck.pdf")));
- Page page = new Page(pdf, A4.LANDSCAPE);
- Font f1 = new Font(pdf, "KozMinProVI-Regular", CodePage.UNICODE);
- f1.setSize(20f);
- String[] faces = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
- String[] suites = {"\u2663", "\u2666", "\u2665", "\u2660"};
- for (int i = 0; i < faces.length; i++) {
- for (int j = 0; j < suites.length; j++) {
- Box cardback = new Box();
- cardback.setLocation((i + 1) * 65f - 55f, (j + 1) * 100f - 20f);
- cardback.setSize(40f, 55f);
- cardback.setColor(Color.black);
- cardback.drawOn(page);
- TextLine face = new TextLine(f1,faces[i]);
- face.setLocation((i + 1) * 65f -50f, (j + 1) * 100f);
- TextLine suite = new TextLine(f1,suites[j]);
- suite.setLocation((i + 1) * 65f - 30f, (j + 1) * 100f + 30f);
- if (j == 1 || j == 2) {
- face.setColor(Color.red);
- suite.setColor(Color.red);
- }
- face.drawOn(page);
- suite.drawOn(page);
- }
- }
- pdf.close();
- }
- public static void main(String[] args) {
- try {
- new GeneratePDFDeck();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement