Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileOutputStream;
- import java.io.File;
- import java.io.*;
- import com.itextpdf.text.BadElementException;
- import com.itextpdf.text.BaseColor;
- import com.itextpdf.text.Document;
- import com.itextpdf.text.DocumentException;
- import com.itextpdf.text.Element;
- import com.itextpdf.text.Font;
- import com.itextpdf.text.Phrase;
- import com.itextpdf.text.pdf.PdfPCell;
- import com.itextpdf.text.pdf.PdfPTable;
- import com.itextpdf.text.pdf.PdfWriter;
- public class PdfCards {
- private static File file = new File("c:/test/PdfCards.pdf");
- private static Font blackFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD);
- private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.NORMAL, BaseColor.RED);
- public static void main(String[] args) throws IOException, DocumentException{
- Document document = new Document();
- PdfWriter.getInstance(document, new FileOutputStream(file));
- try {
- document.open();
- addMetaData(document);
- addContent(document);
- document.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private static void addMetaData(Document document) {
- document.addTitle("PDF Playing Cards");
- document.addSubject("Homework");
- document.addKeywords("Java, PDF, iText");
- document.addAuthor("Pavel Despodov");
- document.addCreator("Pavel Despodov");
- }
- private static void addContent(Document document) throws DocumentException {
- //Setup card numbers
- String[] cards = new String[] {"A","2","3","4","5","6","7","8","9","10","Q","K","J"};
- // add a table
- for(int q=0; q<cards.length; q++){
- createTable(document, cards[q]);
- }
- }
- private static void createTable(Document subCatPart, String symbol)
- throws BadElementException, DocumentException {
- //Setup card colors
- char hearts = '♥';
- char diamond = '♦';
- char clubs = '♣';
- char spades = '♠';
- char[] cardSymbols = new char[]{hearts, diamond, clubs, spades};
- Font fontToUse = null;
- PdfPTable table = new PdfPTable(4);
- for(int i=0; i< cardSymbols.length; i++){
- if(cardSymbols[i] == '♥' || cardSymbols[i] == '♦'){
- fontToUse = redFont;
- }
- else{
- fontToUse = blackFont;
- }
- char whatToShow = cardSymbols[i];
- table.setWidthPercentage(100);
- table.getDefaultCell().setFixedHeight(180);
- PdfPCell c1 = new PdfPCell(new Phrase(whatToShow+""+symbol, fontToUse));
- c1.setFixedHeight(100f);
- c1.setHorizontalAlignment(Element.ALIGN_CENTER);
- table.addCell(c1);
- }
- subCatPart.add(table);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement