fork(3) download
  1. package org.kodejava.example.itextpdf;
  2.  
  3. import com.itextpdf.text.Document;
  4. import com.itextpdf.text.DocumentException;
  5. import com.itextpdf.text.Font;
  6. import com.itextpdf.text.PageSize;
  7. import com.itextpdf.text.Phrase;
  8. import com.itextpdf.text.pdf.PdfPCell;
  9. import com.itextpdf.text.pdf.PdfPTable;
  10. import com.itextpdf.text.pdf.PdfWriter;
  11.  
  12. import java.io.FileNotFoundException;
  13. import java.io.FileOutputStream;
  14. import java.io.File;
  15.  
  16. public class PDFTableDemo {
  17. public static void main(String[] args) {
  18. String[] headers = new String[]{
  19. "NO", "USERNAME", "FIRST NAME", "LAST NAME"
  20. };
  21. String[][] data = new String[][]{
  22. {"1", "JDOW", "JOHN", "DOW"},
  23. {"2", "STIGER", "SCOTT", "TIGER"},
  24. {"3", "FBAR", "FOO", "BAR"}
  25. };
  26.  
  27. //
  28. // Create a new document.
  29. //
  30. Document document = new Document(PageSize.LETTER.rotate());
  31.  
  32. try {
  33. //
  34. // Get an instance of PdfWriter and create a Table.pdf file
  35. // as an output.
  36. //
  37. PdfWriter.getInstance(document,
  38. new FileOutputStream(new File("Table.pdf")));
  39. document.open();
  40.  
  41. //
  42. // Create an instance of PdfPTable. After that we transform
  43. // the header and data array into a PdfPCell object. When
  44. // each table row is complete we have to call the
  45. // table.completeRow() method.
  46. //
  47. // For better presentation we also set the cell font name,
  48. // size and weight. And we also define the background fill
  49. // for the cell.
  50. //
  51. PdfPTable table = new PdfPTable(headers.length);
  52. for (int i = 0; i < headers.length; i++) {
  53. String header = headers[i];
  54. PdfPCell cell = new PdfPCell();
  55. cell.setGrayFill(0.9f);
  56. cell.setPhrase(new Phrase(header.toUpperCase(),
  57. new Font(Font.FontFamily.HELVETICA, 10,
  58. Font.BOLD)));
  59. table.addCell(cell);
  60. }
  61. table.completeRow();
  62.  
  63. for (int i = 0; i < data.length; i++) {
  64. for (int j = 0; j < data[i].length; j++) {
  65. String datum = data[i][j];
  66. PdfPCell cell = new PdfPCell();
  67. cell.setPhrase(new Phrase(datum.toUpperCase(),
  68. new Font(Font.FontFamily.HELVETICA, 10,
  69. Font.NORMAL)));
  70. table.addCell(cell);
  71. }
  72. table.completeRow();
  73. }
  74.  
  75. document.addTitle("Table Demo");
  76. document.add(table);
  77. } catch (DocumentException e) {
  78. e.printStackTrace();
  79. } catch (FileNotFoundException e) {
  80. e.printStackTrace();
  81. } finally {
  82. document.close();
  83. }
  84. }
  85. }
  86.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:16: class PDFTableDemo is public, should be declared in a file named PDFTableDemo.java
public class PDFTableDemo {
       ^
Main.java:3: package com.itextpdf.text does not exist
import com.itextpdf.text.Document;
                        ^
Main.java:4: package com.itextpdf.text does not exist
import com.itextpdf.text.DocumentException;
                        ^
Main.java:5: package com.itextpdf.text does not exist
import com.itextpdf.text.Font;
                        ^
Main.java:6: package com.itextpdf.text does not exist
import com.itextpdf.text.PageSize;
                        ^
Main.java:7: package com.itextpdf.text does not exist
import com.itextpdf.text.Phrase;
                        ^
Main.java:8: package com.itextpdf.text.pdf does not exist
import com.itextpdf.text.pdf.PdfPCell;
                            ^
Main.java:9: package com.itextpdf.text.pdf does not exist
import com.itextpdf.text.pdf.PdfPTable;
                            ^
Main.java:10: package com.itextpdf.text.pdf does not exist
import com.itextpdf.text.pdf.PdfWriter;
                            ^
Main.java:30: cannot find symbol
symbol  : class Document
location: class org.kodejava.example.itextpdf.PDFTableDemo
        Document document = new Document(PageSize.LETTER.rotate());
        ^
Main.java:30: cannot find symbol
symbol  : class Document
location: class org.kodejava.example.itextpdf.PDFTableDemo
        Document document = new Document(PageSize.LETTER.rotate());
                                ^
Main.java:30: package PageSize does not exist
        Document document = new Document(PageSize.LETTER.rotate());
                                                 ^
Main.java:37: cannot find symbol
symbol  : variable PdfWriter
location: class org.kodejava.example.itextpdf.PDFTableDemo
            PdfWriter.getInstance(document,
            ^
Main.java:51: cannot find symbol
symbol  : class PdfPTable
location: class org.kodejava.example.itextpdf.PDFTableDemo
            PdfPTable table = new PdfPTable(headers.length);
            ^
Main.java:51: cannot find symbol
symbol  : class PdfPTable
location: class org.kodejava.example.itextpdf.PDFTableDemo
            PdfPTable table = new PdfPTable(headers.length);
                                  ^
Main.java:54: cannot find symbol
symbol  : class PdfPCell
location: class org.kodejava.example.itextpdf.PDFTableDemo
                PdfPCell cell = new PdfPCell();
                ^
Main.java:54: cannot find symbol
symbol  : class PdfPCell
location: class org.kodejava.example.itextpdf.PDFTableDemo
                PdfPCell cell = new PdfPCell();
                                    ^
Main.java:56: cannot find symbol
symbol  : class Phrase
location: class org.kodejava.example.itextpdf.PDFTableDemo
                cell.setPhrase(new Phrase(header.toUpperCase(),
                                   ^
Main.java:57: cannot find symbol
symbol  : class Font
location: class org.kodejava.example.itextpdf.PDFTableDemo
                        new Font(Font.FontFamily.HELVETICA, 10,
                            ^
Main.java:57: package Font does not exist
                        new Font(Font.FontFamily.HELVETICA, 10,
                                     ^
Main.java:58: cannot find symbol
symbol  : variable Font
location: class org.kodejava.example.itextpdf.PDFTableDemo
                                Font.BOLD)));
                                ^
Main.java:66: cannot find symbol
symbol  : class PdfPCell
location: class org.kodejava.example.itextpdf.PDFTableDemo
                    PdfPCell cell = new PdfPCell();
                    ^
Main.java:66: cannot find symbol
symbol  : class PdfPCell
location: class org.kodejava.example.itextpdf.PDFTableDemo
                    PdfPCell cell = new PdfPCell();
                                        ^
Main.java:67: cannot find symbol
symbol  : class Phrase
location: class org.kodejava.example.itextpdf.PDFTableDemo
                    cell.setPhrase(new Phrase(datum.toUpperCase(),
                                       ^
Main.java:68: cannot find symbol
symbol  : class Font
location: class org.kodejava.example.itextpdf.PDFTableDemo
                            new Font(Font.FontFamily.HELVETICA, 10,
                                ^
Main.java:68: package Font does not exist
                            new Font(Font.FontFamily.HELVETICA, 10,
                                         ^
Main.java:69: cannot find symbol
symbol  : variable Font
location: class org.kodejava.example.itextpdf.PDFTableDemo
                                    Font.NORMAL)));
                                    ^
Main.java:77: cannot find symbol
symbol  : class DocumentException
location: class org.kodejava.example.itextpdf.PDFTableDemo
        } catch (DocumentException e) {
                 ^
28 errors
stdout
Standard output is empty