Advertisement
soxa

PdfCards

May 12th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. import java.io.FileNotFoundException;
  2. import java.io.FileOutputStream;
  3.  
  4. import com.itextpdf.text.BaseColor;
  5. import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.Font;
  8. import com.itextpdf.text.FontFactory;
  9. import com.itextpdf.text.Phrase;
  10. import com.itextpdf.text.pdf.BaseFont;
  11. import com.itextpdf.text.pdf.FontSelector;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPTable;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. public class Ex09_GeneratePdfDeckOfCard {
  17.  
  18.     public static void main(String[] args) throws FileNotFoundException,
  19.             DocumentException {
  20.         Document document = new Document();
  21.  
  22.         PdfWriter.getInstance(document, new FileOutputStream("TestPdf.pdf"));
  23.         // Create pdf file
  24.         document.open(); // Open stream
  25.  
  26.         PdfPTable table = createTable1();
  27.         table.setSpacingBefore(5);
  28.         table.setSpacingAfter(5);
  29.         document.add(table);
  30.  
  31.         document.close(); // Close stream
  32.     }
  33.  
  34.     // Create font for cards
  35.     public static Phrase ColoredText(String text, String color) {
  36.         FontSelector selector = new FontSelector();
  37.         Font usedColor;
  38.         if (color == "red") {
  39.             usedColor = FontFactory.getFont(
  40.                     "/usr/share/fonts/truetype/msttcorefonts/arial.ttf",
  41.             BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  42.                     // if doesn't work find arial.ttf in your computer
  43.             usedColor.setColor(BaseColor.RED);
  44.         }
  45.          else {
  46.             usedColor = FontFactory.getFont(
  47.                     "/usr/share/fonts/truetype/msttcorefonts/arial.ttf",
  48.                     BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  49.             usedColor.setColor(BaseColor.BLACK);
  50.         }
  51.  
  52.         selector.addFont(usedColor);
  53.         Phrase ph = selector.process(text);
  54.         return ph;
  55.     }
  56.  
  57.     public static PdfPTable createTable1() throws DocumentException {
  58.  
  59.         // Table size
  60.         PdfPTable table = new PdfPTable(4);
  61.         table.setWidthPercentage(500 / 5.23f);
  62.         table.getDefaultCell().setFixedHeight(100);
  63.         table.setWidths(new int[] { 3, 3, 3, 3 });
  64.         PdfPCell cell;
  65.         cell = new PdfPCell(new Phrase("Cards"));
  66.         cell.setColspan(4); // number of cols
  67.         table.addCell(cell);
  68.         cell.setRowspan(13); // number of rols
  69.  
  70.         String[] cardsNameStrings = new String[] { "Ace", "2", "3", "4", "5",
  71.                 "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
  72.                                                          
  73.         String[] cardsTypeStrings = new String[] { "\u2666", "\u2665",
  74.                 "\u2660", "\u2663" };
  75.         // Imput data
  76.         for (int name = 0; name < cardsNameStrings.length; name++) {
  77.  
  78.             for (int type = 0; type < cardsTypeStrings.length; type++) {
  79.  
  80.                 if (type <= 1) {
  81.                     table.addCell(ColoredText(cardsNameStrings[name]
  82.                             + cardsTypeStrings[type], "red"));
  83.                 } else {
  84.                     table.addCell(ColoredText(cardsNameStrings[name]
  85.                             + cardsTypeStrings[type], "black"));
  86.                 }
  87.             }
  88.         }
  89.         return table;
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement