Advertisement
Guest User

Untitled

a guest
May 7th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. import java.io.FileOutputStream;
  2. import java.io.File;
  3. import java.io.*;
  4.  
  5. import com.itextpdf.text.BadElementException;
  6. import com.itextpdf.text.BaseColor;
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Element;
  10. import com.itextpdf.text.Font;
  11. import com.itextpdf.text.Phrase;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPTable;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16.  
  17. public class PdfCards {
  18.     private static File file = new File("c:/test/PdfCards.pdf");
  19.     private static Font blackFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD);
  20.     private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.NORMAL, BaseColor.RED);
  21.    
  22.     public static void main(String[] args) throws IOException, DocumentException{
  23.         Document document = new Document();
  24.         PdfWriter.getInstance(document, new FileOutputStream(file));
  25.         try {
  26.             document.open();
  27.             addMetaData(document);
  28.             addContent(document);
  29.             document.close();
  30.         } catch (Exception e) {
  31.             e.printStackTrace();
  32.         }
  33.     }
  34.     private static void addMetaData(Document document) {
  35.         document.addTitle("PDF Playing Cards");
  36.         document.addSubject("Homework");
  37.         document.addKeywords("Java, PDF, iText");
  38.         document.addAuthor("Pavel Despodov");
  39.         document.addCreator("Pavel Despodov");
  40.      }
  41.    
  42.     private static void addContent(Document document) throws DocumentException {
  43.         //Setup card numbers
  44.         String[] cards = new String[] {"A","2","3","4","5","6","7","8","9","10","Q","K","J"};
  45.         // add a table
  46.         for(int q=0; q<cards.length; q++){
  47.             createTable(document, cards[q]);
  48.         }
  49.       }
  50.  
  51.       private static void createTable(Document subCatPart, String symbol)
  52.           throws BadElementException, DocumentException {
  53.         //Setup card colors
  54.         char hearts = '♥';
  55.         char diamond = '♦';
  56.         char clubs = '♣';
  57.         char spades = '♠';
  58.         char[] cardSymbols = new char[]{hearts, diamond, clubs, spades};
  59.         Font fontToUse = null;
  60.         PdfPTable table = new PdfPTable(4);
  61.         for(int i=0; i< cardSymbols.length; i++){
  62.             if(cardSymbols[i] == '♥' || cardSymbols[i] == '♦'){
  63.                 fontToUse = redFont;
  64.             }
  65.             else{
  66.                 fontToUse = blackFont;
  67.             }
  68.             char whatToShow = cardSymbols[i];
  69.             table.setWidthPercentage(100);
  70.             table.getDefaultCell().setFixedHeight(180);
  71.             PdfPCell c1 = new PdfPCell(new Phrase(whatToShow+""+symbol, fontToUse));
  72.             c1.setFixedHeight(100f);
  73.             c1.setHorizontalAlignment(Element.ALIGN_CENTER);
  74.             table.addCell(c1);
  75.         }
  76.         subCatPart.add(table);
  77.  
  78.       }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement