SIMPLE TABLE CREATION
IN JAVA USING ITEXT PDF
public void PdfGen() throws DocumentException,
FileNotFoundException, ClassNotFoundException, SQLException {
Document
document = new Document();
PdfWriter
writer = PdfWriter.getInstance(document, new
FileOutputStream("D:\\HelloWorld1.pdf"));
document.open();
Paragraph
paragraph = new Paragraph();
Chunk tab =
new Chunk(new VerticalPositionMark(), 50f, false);
//
paragraph.setTabSettings(new TabSettings(tabList));
Chunk CONNECT
= new Chunk(
new LineSeparator(0.5f, 95,
BaseColor.BLUE, Element.ALIGN_CENTER, 3.5f));
LineSeparator
UNDERLINE
= new
LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2);
Chunk tab1 =
new Chunk(new VerticalPositionMark(), 200, true);
Chunk tab2 =
new Chunk(new VerticalPositionMark(), 350, true);
Chunk tab6 =
new Chunk(new VerticalPositionMark(), 50f, false);
Chunk tab3 =
new Chunk(new DottedLineSeparator(), 450, true);
Date d=new
Date();
document.add(new
Paragraph("STATUS REPORT GENERATION.."+d.toString()));
document.add(new Paragraph(new Chunk(tab1)));
PdfPTable
table = new PdfPTable(8); // 6 columns.
table.setWidthPercentage(100);
table.setWidths(new int[]{ 1, 2, 2, 2, 2,2,2,2});
//
List<String> cols=new ArrayList<String>();
List<String> cols = new
ArrayList<>(Arrays.asList("ID","TITHI","PHASE","STAR","DATE","DAY","TIME","STATUS"));
for(int
i=0;i<cols.size();i++)
{
PdfPCell colum
= new PdfPCell(new Paragraph(cols.get(i)));
colum.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(colum);
}
Statement
stm=new DataBase_Connection().ConnectionGet().createStatement();
ResultSet
rs=stm.executeQuery("select * from
status");
//for(int
i=0;i<cols.size()-1;i++)
while(rs.next())
{
for(int
i=1;i<=8;i++)
{
table.addCell(rs.getString(i));
table.setSpacingBefore(10f);
}
}
document.add(table);
document.close();
writer.close();
JOptionPane.showMessageDialog(rootPane, "DATA IS PRINTED..");
}
--------------------------------------------------------------------------------------------------------------------------
PDF TABLE WITH FORMATING FIELDS
import java.io.FileOutputStream;
import java.text.DecimalFormat;
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.Font.FontFamily;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
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 PrintReport {
public static void main(String[] args) {
String pdfFilename = "";
PrintReport printReport = new PrintReport();
if (args.length < 1)
{
System.err.println("Usage: java "+ printReport.getClass().getName()+
" PDF_Filename");
System.exit(1);
}
pdfFilename = args[0].trim();
printReport.createPDF(pdfFilename);
}
private void createPDF (String pdfFilename){
Document doc = new Document();
PdfWriter docWriter = null;
DecimalFormat df = new DecimalFormat("0.00");
try {
//special font sizes
Font bfBold12 = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0));
Font bf12 = new Font(FontFamily.TIMES_ROMAN, 12);
//file path
String path = "docs/" + pdfFilename;
docWriter = PdfWriter.getInstance(doc , new FileOutputStream(path));
//document header attributes
doc.addAuthor("betterThanZero");
doc.addCreationDate();
doc.addProducer();
doc.addCreator("MySampleCode.com");
doc.addTitle("Report with Column Headings");
doc.setPageSize(PageSize.LETTER);
//open document
doc.open();
//create a paragraph
Paragraph paragraph = new Paragraph("itext ");
//specify column widths
float[] columnWidths = {1.5f, 2f, 5f, 2f};
//create PDF table with the given widths
PdfPTable table = new PdfPTable(columnWidths);
// set table width a percentage of the page width
table.setWidthPercentage(90f);
//insert column headings
insertCell(table, "Order No", Element.ALIGN_RIGHT, 1, bfBold12);
insertCell(table, "Account No", Element.ALIGN_LEFT, 1, bfBold12);
insertCell(table, "Account Name", Element.ALIGN_LEFT, 1, bfBold12);
insertCell(table, "Order Total", Element.ALIGN_RIGHT, 1, bfBold12);
table.setHeaderRows(1);
//insert an empty row
insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12);
//create section heading by cell merging
insertCell(table, "New York Orders ...", Element.ALIGN_LEFT, 4, bfBold12);
double orderTotal, total = 0;
//just some random data to fill
for(int x=1; x<5; x++){
insertCell(table, "10010" + x, Element.ALIGN_RIGHT, 1, bf12);
insertCell(table, "ABC00" + x, Element.ALIGN_LEFT, 1, bf12);
insertCell(table, "This is Customer Number ABC00" + x, Element.ALIGN_LEFT, 1, bf12);
orderTotal = Double.valueOf(df.format(Math.random() * 1000));
total = total + orderTotal;
insertCell(table, df.format(orderTotal), Element.ALIGN_RIGHT, 1, bf12);
}
//merge the cells to create a footer for that section
insertCell(table, "New York Total...", Element.ALIGN_RIGHT, 3, bfBold12);
insertCell(table, df.format(total), Element.ALIGN_RIGHT, 1, bfBold12);
//repeat the same as above to display another location
insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12);
insertCell(table, "California Orders ...", Element.ALIGN_LEFT, 4, bfBold12);
orderTotal = 0;
for(int x=1; x<7; x++){
insertCell(table, "20020" + x, Element.ALIGN_RIGHT, 1, bf12);
insertCell(table, "XYZ00" + x, Element.ALIGN_LEFT, 1, bf12);
insertCell(table, "This is Customer Number XYZ00" + x, Element.ALIGN_LEFT, 1, bf12);
orderTotal = Double.valueOf(df.format(Math.random() * 1000));
total = total + orderTotal;
insertCell(table, df.format(orderTotal), Element.ALIGN_RIGHT, 1, bf12);
}
insertCell(table, "California Total...", Element.ALIGN_RIGHT, 3, bfBold12);
insertCell(table, df.format(total), Element.ALIGN_RIGHT, 1, bfBold12);
//add the PDF table to the paragraph
paragraph.add(table);
// add the paragraph to the document
doc.add(paragraph);
}
catch (DocumentException dex)
{
dex.printStackTrace();
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
if (doc != null){
//close the document
doc.close();
}
if (docWriter != null){
//close the writer
docWriter.close();
}
}
}
private void insertCell(PdfPTable table, String text, int align, int colspan, Font font){
//create a new cell with the specified Text and Font
PdfPCell cell = new PdfPCell(new Phrase(text.trim(), font));
//set the cell alignment
cell.setHorizontalAlignment(align);
//set the cell column span in case you want to merge two or more cells
cell.setColspan(colspan);
//in case there is no text and you wan to create an empty row
if(text.trim().equalsIgnoreCase("")){
cell.setMinimumHeight(10f);
}
//add the call to the table
table.addCell(cell);
}
}
No comments:
Post a Comment