Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileOutputStream;
- import com.itextpdf.text.BaseColor;
- import com.itextpdf.text.Document;
- import com.itextpdf.text.Element;
- import com.itextpdf.text.Font;
- import com.itextpdf.text.Phrase;
- import com.itextpdf.text.Rectangle;
- import com.itextpdf.text.pdf.BaseFont;
- import com.itextpdf.text.pdf.PdfPCell;
- import com.itextpdf.text.pdf.PdfPTable;
- import com.itextpdf.text.pdf.PdfWriter;
- public class PrintCards {
- public static void main(String[] args) {
- try {
- Document document = new Document();
- PdfWriter.getInstance(document, new FileOutputStream(
- "DeckOfCards.pdf"));
- document.open();
- String faces[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9",
- "10", "J", "Q", "K" };
- String suits[] = { "\u2660", "\u2665", "\u2666", "\u2663" };
- String faceAndSuit;
- int countCells = 0;
- BaseFont bf = BaseFont.createFont("arial.ttf", BaseFont.IDENTITY_H,
- BaseFont.EMBEDDED);
- Font blackFont = new Font(bf, 10, Font.NORMAL, BaseColor.BLACK);
- Font redFont = new Font(bf, 10, Font.NORMAL, BaseColor.RED);
- Font currentFont;
- PdfPTable table = new PdfPTable(8);
- table.setWidthPercentage(80);
- float[] columnWidths = { 10f, 2f, 10f, 2f, 10f, 2f, 10f, 2f };
- table.setWidths(columnWidths);
- PdfPCell emptyCell = new PdfPCell();
- emptyCell.setFixedHeight(30f);
- emptyCell.setBorder(Rectangle.NO_BORDER);
- for (int i = 0; i < suits.length; i++) {
- for (int j = 0; j < faces.length; j++) {
- switch (i) {
- case 1:
- case 2:
- currentFont = redFont;
- break;
- default:
- currentFont = blackFont;
- break;
- }
- faceAndSuit = faces[j] + suits[i];
- table.addCell(createAndFormatCell(faceAndSuit, currentFont));
- table.addCell(emptyCell);
- countCells++;
- if (countCells % 4 == 0) {
- for (int row = 0; row < columnWidths.length; row++) {
- table.addCell(emptyCell);
- }
- }
- }
- }
- document.add(table);
- document.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static PdfPCell createAndFormatCell(String cardFace, Font currentFont) {
- PdfPCell cardCell = new PdfPCell(new Phrase(cardFace, currentFont));
- cardCell.setFixedHeight(100f);
- cardCell.setHorizontalAlignment(Element.ALIGN_CENTER);
- cardCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
- return cardCell;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement