Enlightensoft's Blog

Helping in your each step

  • Categories

  • Authors

Posts Tagged ‘Export to pdf’

Export to .pdf using Core JAVA

Posted by Pankil Patel on June 22, 2012

Required JAR:

itextpdf-5.3.0.jar

itext-xtra-5.3.0.jar

 

 

File: PDFCreator.java

package co.cc.enlightensoft.pdf;

import java.io.FileOutputStream;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class PDFCreator {
public static void main(String[] args) {
try {

// creation of the document with a certain size and certain margins
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
// creation of the different writers
// HtmlWriter.getInstance(document, System.out);
PdfWriter.getInstance(document,
new FileOutputStream(“text.pdf”));
// we add some meta information to the document
document.addAuthor(“Bruno Lowagie”);
document.addSubject(“This is the result of a Test.”);
// we open the document for writing
document.open();

// ##################### Paragraph : Start #####################
document.add(new Paragraph(“Hello world”));
// ##################### Paragraph : End #####################

// ##################### Chunk : Start #####################
Chunk chunk = new Chunk(“Hello world”, FontFactory.getFont(
FontFactory.COURIER, 20, Font.ITALIC, new BaseColor(
255, 0, 0)));
document.add(chunk);
// ##################### Chunk : End #####################

// ##################### Anchor : Start #####################
Anchor anchor = new Anchor(“\nGoogle\n”);
anchor.setName(“Google”);
anchor.setReference(“http://www.google.com”);

document.add(anchor);
// ##################### Anchor : End #####################

// ##################### Image : Start #####################
Image image = Image.getInstance(“Photo.JPG”);
image.scaleAbsolute(100, 100);
document.add(image);
// ##################### Image : End #####################

// ##################### Table : Start #####################
PdfPTable table = createTable1();
table.setSpacingBefore(5);
table.setSpacingAfter(5);
document.add(table);

// ##################### Table : End #####################

System.out.println(“File Created…”);
} catch (DocumentException de) {
System.err.println(de.getMessage());
}
document.close();
} catch (Throwable t) {
t.printStackTrace();
}
}

/** The resulting PDF file. */
public static final String RESULT = “text.pdf”;

/**
* Creates a table; widths are set with setWidths().
*
* @return a PdfPTable
* @throws DocumentException
*/
public static PdfPTable createTable1() throws DocumentException {
PdfPTable table = new PdfPTable(3);
table.setWidthPercentage(288 / 5.23f);
table.setWidths(new int[] { 2, 1, 1 });
PdfPCell cell;
cell = new PdfPCell(new Phrase(“Table 1”));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase(“Cell with rowspan 2”));
cell.setRowspan(2);
table.addCell(cell);
table.addCell(“row 1; cell 1”);
table.addCell(“row 1; cell 2”);
table.addCell(“row 2; cell 1”);
table.addCell(“row 2; cell 2”);
return table;
}

/**
* Creates a table; widths are set with setWidths().
*
* @return a PdfPTable
* @throws DocumentException
*/
public static PdfPTable createTable2() throws DocumentException {
PdfPTable table = new PdfPTable(3);
table.setTotalWidth(288);
table.setLockedWidth(true);
table.setWidths(new float[] { 2, 1, 1 });
PdfPCell cell;
cell = new PdfPCell(new Phrase(“Table 2”));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase(“Cell with rowspan 2”));
cell.setRowspan(2);
table.addCell(cell);
table.addCell(“row 1; cell 1”);
table.addCell(“row 1; cell 2”);
table.addCell(“row 2; cell 1”);
table.addCell(“row 2; cell 2”);
return table;
}

/**
* Creates a table; widths are set in the constructor.
*
* @return a PdfPTable
* @throws DocumentException
*/
public static PdfPTable createTable3() throws DocumentException {
PdfPTable table = new PdfPTable(new float[] { 2, 1, 1 });
table.setWidthPercentage(55.067f);
PdfPCell cell;
cell = new PdfPCell(new Phrase(“Table 3”));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase(“Cell with rowspan 2”));
cell.setRowspan(2);
table.addCell(cell);
table.addCell(“row 1; cell 1”);
table.addCell(“row 1; cell 2”);
table.addCell(“row 2; cell 1”);
table.addCell(“row 2; cell 2”);
return table;
}

/**
* Creates a table; widths are set with special setWidthPercentage() method.
*
* @return a PdfPTable
* @throws DocumentException
*/
public static PdfPTable createTable4() throws DocumentException {
PdfPTable table = new PdfPTable(3);
Rectangle rect = new Rectangle(523, 770);
table.setWidthPercentage(new float[] { 144, 72, 72 }, rect);
PdfPCell cell;
cell = new PdfPCell(new Phrase(“Table 4”));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase(“Cell with rowspan 2”));
cell.setRowspan(2);
table.addCell(cell);
table.addCell(“row 1; cell 1”);
table.addCell(“row 1; cell 2”);
table.addCell(“row 2; cell 1”);
table.addCell(“row 2; cell 2”);
return table;
}

/**
* Creates a table; widths are set with setTotalWidth().
*
* @return a PdfPTable
* @throws DocumentException
*/
public static PdfPTable createTable5() throws DocumentException {
PdfPTable table = new PdfPTable(3);
table.setTotalWidth(new float[] { 144, 72, 72 });
table.setLockedWidth(true);
PdfPCell cell;
cell = new PdfPCell(new Phrase(“Table 5”));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase(“Cell with rowspan 2”));
cell.setRowspan(2);
table.addCell(cell);
table.addCell(“row 1; cell 1”);
table.addCell(“row 1; cell 2”);
table.addCell(“row 2; cell 1”);
table.addCell(“row 2; cell 2”);
return table;
}

}

Posted in Core Java | Tagged: , , , , , , , | 1 Comment »