import java.io.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
public class Project04_A {
public static void main(String[] args) {
//iText API
String[] title = new String[] {"","",""," URL"};
String[][] rows = new String[][] {
{" "," ", "", "https://bookthumb-phinf.pstatic.net/cover/100/365/10036542.jpg"},
{"Java ","", "", "https://bookthumb-phinf.pstatic.net/cover/100/365/10036542.jpg"},
{" ","", "", "https://bookthumb-phinf.pstatic.net/cover/100/365/10036542.jpg"},
};
Document doc=new Document(PageSize.A4);
try {
PdfWriter.getInstance(doc, new FileOutputStream(new File("book.pdf")));
doc.open();
//
BaseFont bf=BaseFont.createFont("H2MJRE.TTF",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font fontTitle = new Font(bf,12);
Font fontRows = new Font(bf,10);
PdfPTable table=new PdfPTable(title.length);
table.setWidthPercentage(100);
float[] colwidth=new float[]{20f,15f,15f,30f};
table.setWidths(colwidth);
for(String header:title) {
PdfPCell cell=new PdfPCell();
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPadding(10);
cell.setGrayFill(0.9f);
cell.setPhrase(new Phrase(header,fontTitle));
table.addCell(cell);
}
table.completeRow();
for(String[] row:rows) {
for(String data:row) {
Phrase phrase = new Phrase(data,fontRows);
PdfPCell cell = new PdfPCell(phrase);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPaddingTop(20);
cell.setPaddingRight(30);
cell.setPaddingBottom(20);
cell.setPaddingLeft(30);
table.addCell(cell);
}
table.completeRow();
}
PdfPCell cell4=new PdfPCell(new Phrase("Cell 5"));
cell4.setColspan(2);
PdfPCell cell5=new PdfPCell(new Phrase("Cell 6"));
cell5.setColspan(2);
table.addCell(cell4);
table.addCell(cell5);
doc.addTitle("PDF Table Demo");
doc.add(table);
System.out.println("table ");
} catch (Exception e) {
e.printStackTrace();
} finally {
doc.close();
}
}
}