package com;
import java.awt.Color;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfPTable;
import com.lowagie.text.Annotation;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
public class main
{
public static List<process> viewRecord(String s[]){
SessionFactory sf=new Configuration().configure().buildSessionFactory();
Session session=sf.openSession();
Criteria c1=session.createCriteria(process.class);
// c1.add(Restrictions.and(Restrictions.eq("id", new Integer(0)),Restrictions.like("username","vipul")));
// c1.addOrder(Order.desc("id"));
// c1.setProjection(Projections.property("username"));
// c1.setProjection(Projections.property("address"));
int len=s.length;
System.out.println(len);
ProjectionList p1=Projections.projectionList();
for(int i=0; i<len; i++)
{
p1.add(Projections.property(s[i]));
System.out.println(s[i]);
// p1.add(Projections.property("address"));
}
c1.setProjection(p1);
List l1 = (List) c1.list();
// Iterator i =l1.iterator();
// while(i.hasNext())
//{
// Object o[]=(Object [])i.next();
// System.out.println(o[0] + "==" + o[1]);
// String str=(String) i.next();
// System.out.println(str);
/*
Object o=i.next();
login l11=(login)o;
System.out.println(l11.getid());
System.out.println(l11.getaddress());
System.out.println(l11.getemail());
System.out.println(l11.getpassword());
System.out.println(l11.getphonenumber());
System.out.println(l11.getusername());
*/
//}
System.out.println("save");
session.close();
sf.close();
return l1;
}
private static Image getImageFromResource(String URI){
Image image = null;
try{
image = Image.getInstance(URI);
}catch(IOException ioe){
//de.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
return image;
}
/* public static HeaderAndFooter() {
protected Phrase header;
protected PdfPTable footer;
header = new Phrase("**** THIS IS HEADER PART OF THIS PDF ****");
footer = new PdfPTable(1);
footer.setTotalWidth(300);
footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
footer.addCell(new Phrase(new Chunk("**** THIS IS FOOTER PART OF THIS PDF ****")
.setAction(new PdfAction(PdfAction.FIRSTPAGE))));
return header;
}
*/
static PdfPCell borderlessCell(String s){
PdfPCell cell = new PdfPCell();
Font f = new Font(Font.COURIER);
cell.setBorder(0);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.addElement(new Paragraph(s,f));
return cell;
}
public static void addEmptyLine(Paragraph p,int line)
{
for( int i=0; i < line; i++ )
p.add( new Paragraph(" ---------------------------------------") );
}
public static void addTextAtXY( String text, PdfContentByte cb, float x, float y )
throws IOException, DocumentException
{
BaseFont labelFont = BaseFont.createFont( BaseFont.TIMES_ROMAN, "Cp1252", true );
cb.beginText();
cb.setColorFill( Color.black);
cb.setFontAndSize( labelFont, 10 );
cb.setTextMatrix( x, y );
cb.showText( text );
cb.endText();
}
public static void addText( String text, Document doc, Font font, int newLines )
throws DocumentException
{
Paragraph paragraph = new Paragraph( text, font );
addEmptyLine( paragraph, newLines );
doc.add( paragraph );
}
}
/*
// page number
outputStream = new ByteArrayOutputStream();
output = new DataOutputStream(outputStream);
document = new Document();
writer = PdfWriter.getInstance(document, output);
document.open();
contentByte = writer.getDirectContent();
....add stuff
document.close();
writer.close();
byte[] output = outputStream.toByteArray();
PdfReader reader = new PdfReader(output);
//reset the output
outputStream = new ByteArrayOutputStream();
output = new DataOutputStream(outputStream);
document = new Document();
writer = PdfWriter.getInstance(document, output);
document.open();
PdfStamper stamper = new PdfStamper(reader, outputStream);
//add the pages
for (int i = 1; i <= pageCount; i++)
{
contentByte = stamper.getOverContent(i);
addParagraph("Page " + i + " of " + pageCount, new Point(500, 30), boldTextFont); // my own paragraph font
}
stamper.close();
To change PDF page background color, use this
Rectangle pageSize = new Rectangle(400,400);
pageSize.setBackgroundColor(new java.awt.Color(0xDF,0xCC,0xFF));
Document d = new Document (pageSize);
and remaining same.
Insert table in PDF,
PdfPTable table=new PdfPTable(2);
table.addCell("Student Name");
table.addCell("Roll No.");
table.addCell("Binod");
table.addCell("110");
table.addCell("Pramod");
table.addCell("120");
d.add(table);
Insert Header in table in PDF
PdfPTable table=new PdfPTable(2);
PdfPCell cell = new PdfPCell(new Paragraph("Student Details"));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBackgroundColor(new Color(20,105,160));
cell.setColspan(2);
table.addCell(cell);
table.addCell("Student Name");
table.addCell("Roll No.");
table.addCell("Binod");
table.addCell("110");
table.addCell("Pramod");
table.addCell("120");
d.add(table);
Font font = new Font();
font.setColor(Color.GREEN);
font.setStyle(Font.UNDERLINE);
docpdf.add(new Chunk("Chapter 1"));
docpdf.add(new Paragraph(new Chunk("Press here to go chapter 2", font).setLocalGoto("2")));// Code 2
docpdf.newPage();
docpdf.add(new Chunk("Chapter 2").setLocalDestination("2"));
docpdf.add(new Paragraph(new Chunk("http://www.maargasystems.com", font).setAnchor("http://www.maargasystems.com")));//Code 3
docpdf.add(new Paragraph( new Chunk("Open CreatePDFInlotus.pdf chapter 3", font).setRemoteGoto("D:\\Ram\\CreatePDFInlotus.pdf", "3")));//Code 4
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import com.itextpdf.text.Annotation;
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.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
public class iTextIntroduction
{
/** Path of the PDF document. */
// public static final String OUTPUT =
// "c:\\temp\\iTextIntorduction.pdf";
// /** Title color using rgb */
// public static final BaseColor TITLE_COLOR =
// new BaseColor(25, 50, 75);
// /** Title font name, size, style and color */
// public static final Font TITLE_FONT =
// FontFactory.getFont( FontFactory.TIMES, 18, Font.BOLD, TITLE_COLOR );
// /** Subtitle font name, size, style and color */
// public static final Font SUBTITLE_FONT =
// FontFactory.getFont( FontFactory.TIMES, 16, Font.BOLD, TITLE_COLOR );
// /** Paragraph color using rgb */
// public static final BaseColor PARAGRAPH_COLOR =
// new BaseColor(75, 50, 75);
// /** Paragraph font name, size, style and color */
// public static final Font PARAGRAPH_FONT =
// FontFactory.getFont( FontFactory.TIMES, 12, Font.BOLD, PARAGRAPH_COLOR );
// /** URL color using rgb */
// public static final BaseColor URL_COLOR =
// new BaseColor( 5, 5, 75);
// /** Paragraph font name, size, style and color */
// public static final Font URL_FONT =
// FontFactory.getFont( FontFactory.TIMES, 12, Font.BOLD, URL_COLOR );
// /** Url string to be added to a paragraph */
// public static final String PARAGRAPH_URL =
// "http://hubpages.com/profile/kievan";
// /** Url string of an image */
// public static final String IMAGE_SRC_URL =
// "http://s2.hubimg.com/u/3954873_177.jpg";
// /** Url string to be added to an image */
// public static final String IMAGE_URL =
// "http://hubpages.com/profile/kievan";
//
//
// public void createPdf(String output) throws IOException, DocumentException
// {
//
// // Create a document object
// Document document = new Document();
//
// // Create a writer that puts a document into a file
// FileOutputStream fos = new FileOutputStream( output );
// PdfWriter writer = PdfWriter.getInstance( document, fos );
//
//
//
// document.open();
//
// // Set document size, margins and meta data
// setUpDocument( document );
//
// // Add a title
// String titleS = "Introduction to iText - Create a PDF Document with " +
// "Text, Hyperlinks and Graphics";
// addText( titleS, document, TITLE_FONT, 2 );
//
// // Add a subtitle
// String subtitleS = "iText rules!";
// addText( subtitleS, document, SUBTITLE_FONT, 2 );
//
// // Add a paragraph
// String paragraphS = "A Sample text from me. Anything that comes to mind " +
// "is simply written here. Where the mighty wind blows,"+
// " there you shall find me.";
// addText( paragraphS, document, PARAGRAPH_FONT, 2 );
//
// // Add a URL
// String urlS = "- kievan";
// addURL( urlS, PARAGRAPH_URL, document, URL_FONT );
//
// // Add image with a label
// addTextAtXY( "Clickable HubPages profile picture.", writer.getDirectContent(), 225f, 530f );
// addImageAtXY( document, new URL( IMAGE_SRC_URL ), new URL( IMAGE_URL ), 225f, 370f );
//
// // Add pie chart
// addTextAtXY( "Very simple pie chart.", writer.getDirectContent(), 260f, 305f );
// addPieChart( writer.getDirectContent() );
//
// // Close a document
// document.close();
//
// System.out.println("Created PDF document!");
//
// }
//
// /**
// * Sets up document size, margins and metadata.
// * iText allows to add metadata to the PDF which can be
// * viewed in your Adobe Reader under File -> Properties
// * @param doc a document that is going to be set up.
// */
// public static void setUpDocument( Document doc )
// {
// doc.addTitle( "Introduction to iText - " +
// "Create a PDF Document with Text, Hyperlinks and Graphics" );
// doc.addSubject( "Introduction to iText" );
// doc.addKeywords( "Java, PDF, iText" );
// doc.addAuthor( "kievan" );
// doc.addCreator( "kievan" );
// // A4 = 210mm x 297mm ~ 605points x 855points
// doc.setPageSize( PageSize.A4 );
// doc.setMargins( 72f, 72f, 72f, 72f );
// }
//
// public static void addText( String text, Document doc, Font font, int newLines )
// throws DocumentException
// {
// Paragraph paragraph = new Paragraph( text, font );
// addEmptyLine( paragraph, newLines );
// doc.add( paragraph );
// }
//
// public static void addURL( String text, String url, Document doc, Font font )
// throws DocumentException, MalformedURLException
// {
// Chunk c = new Chunk( text, font );
// c.setAction( new PdfAction( new URL( url ) ) );
// c.setUnderline(0.8f, -0.8f);
// doc.add( c );
// }
//
// public static void addImageAtXY( Document doc, URL imageSource, URL url, float x, float y )
// throws DocumentException, IOException
// {
// Image img = com.itextpdf.text.Image.getInstance( imageSource );
// img.scaleToFit( 150f, 150f );
// img.setAbsolutePosition( x, y );
// Annotation anno = new Annotation( 0f, 0f, 0f, 0f, url );
// img.setAnnotation( anno );
// doc.add( img );
// }
//
// /**
// * Draws the pie chart.
// * @param directcontent a canvas to which the pie chart has to be drawn.
// * @throws IOException
// */
// public static void addPieChart(PdfContentByte directcontent)
// throws DocumentException
// {
// /** TODO: Automate and improve pie chart creation.
// * 1. Add support for adding pie pieces dynamically
// * from an Array or ArrayList.
// * 2. Calculate pie chart location based on single set
// * of coordinates.
// * 3. Assign each pie piece a random color.
// */
//
// directcontent.setLineWidth(3.2f);
// directcontent.setRGBColorStroke(255, 255, 255);
//
// directcontent.setRGBColorFill(16, 205, 55);
// // llx, lly, urx, ury, begin, extent
// directcontent.arc(200, 100, 400, 300, 0, 120);
// directcontent.lineTo(300,200);
// directcontent.closePathFillStroke();
//
// directcontent.setRGBColorFill(254, 205, 16);
// directcontent.arc(200, 100, 400, 300, 120, 120);
// directcontent.lineTo(300,200);
// directcontent.closePathFillStroke();
//
// directcontent.setRGBColorFill(155, 25, 16);
// directcontent.arc(200, 100, 400, 300, 240, 120);
// directcontent.lineTo(300,200);
// directcontent.closePathFillStroke();
// }
//
// /**
// * Adds text at an absolute position.
// * @param text a string of text to be added.
// * @param cb a canvas to which the text will be added.
// * @param x a position of the text along x axis.
// * @param y a position of the text along y axis.
// */
// public static void addTextAtXY( String text, PdfContentByte cb, float x, float y )
// throws IOException, DocumentException
// {
// BaseFont labelFont = BaseFont.createFont( BaseFont.TIMES_ROMAN, "Cp1252", true );
// cb.beginText();
// cb.setColorFill( TITLE_COLOR );
// cb.setFontAndSize( labelFont, 10 );
// cb.setTextMatrix( x, y );
// cb.showText( text );
// cb.endText();
// }
//
// /**
// * Adds empty line to a paragraph.
// * @param par a paragraph object to which line will be added.
// * @param howMany a number of empty lines to be added.
// */
// public static void addEmptyLine( Paragraph par, int howMany )
// throws DocumentException
// {
// for( int i=0; i < howMany; i++ )
// par.add( new Paragraph(" ") );
// }
//
// public static void main(String...args)
// throws IOException, DocumentException
// {
// new iTextIntroduction().createPdf( OUTPUT );
// }
//
//}
//