Java PDF Generation with IText

Posted: January 4, 2012 in Random Posts
Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Multi Site Keyword Extractor Tool
Analyze Keywords

First Method creates ur PDF File and Second Download your file from the relative Path in JSF

public String mypdfGeneratorAmd() {
// fileCreation();
List mylist = this.getListDfAmd();
if (mylist != null) {
Document pdf = new Document();
Date today = new Date();
ServletContext servletContext = null;
// (ServletContext)
// FacesContext.getCurrentInstance().getExternalContext().getContext();
// String logo1 = servletContext.getRealPath(“”) + “/” +
// “WebContent”
// + “/” + “reports” + “/”;

String fileName = “AssistedMaintenanceDocking.pdf”;

File f;
String fileTitle = “AssistedMaintenanceDocking ”
+ (new Date().toString());
servletContext = (ServletContext) FacesContext.getCurrentInstance()
.getExternalContext().getContext();
String logo = servletContext.getRealPath(“”) + “/” + “images” + “/”
+ “user-image.jpg”;

int rowCount = mylist.size();
PdfPTable t = new PdfPTable(5);
Paragraph title1 = new Paragraph();
try {
Image img = Image.getInstance(logo);
String mypdfFile = servletContext.getRealPath(“”) + “/”
+ “reports” + “/” + “DefectAndHistory.pdf”;
f = new File(mypdfFile);
// “/home/shussain/workspace2/DNRMS-PMSA/WebContent/reports/”+
// “AssistedMaintenanceDocking.pdf”);
if (!f.exists()) {
try {
f.createNewFile();
System.out
.println(“New file ‘AssistedMaintenanceDocking.pdf’ has been created to the current directory”);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out
.println(“File ‘AssistedMaintenanceDocking.pdf’ Already in the current directory”);
}
PdfWriter.getInstance(pdf, new FileOutputStream(f));
pdf.addTitle(fileTitle);
pdf.setMargins((float) 1.5, (float) 1.5, 30, 30);
pdf.addAuthor(session.getAttribute(“sessionUserName”)
.toString());
pdf.addCreationDate();
pdf.addCreator(session.getAttribute(“sessionUserName”)
.toString());
pdf.addSubject(“Assisted Maintenance Docking”);
pdf.open();

title1.add(new Paragraph(“Assisted Maintenance Docking” + “\n”
+ (new Date().toString()), FontFactory.getFont(
FontFactory.HELVETICA, 20, Font.BOLD,
new Color(0, 0, 0))));
title1.setAlignment(“CENTER”);
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);

pdf.add(chapter1);
addEmptyLine(title1, 2);
Phrase phraseOfImage = new Phrase();
phraseOfImage.add(new Chunk(img, 18, -18));
pdf.add(phraseOfImage);

pdf.add(Chunk.NEWLINE);
pdf.add(Chunk.NEWLINE);

t.setWidthPercentage(90);
Phrase phrase1 = null;

float[] columnWidths = { 200, 200, 200, 200, 200 };
t.setWidths(columnWidths);

PdfPCell c1 = new PdfPCell(new Phrase(“Ship/Unit”,
FontFactory.getFont(FontFactory.HELVETICA, 12,
Font.BOLD, Color.BLACK)));

c1.setBackgroundColor(Color.gray);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
t.addCell(c1);

c1 = new PdfPCell(new Phrase(“Last AMD”, FontFactory.getFont(
FontFactory.HELVETICA, 12, Font.BOLD, Color.BLACK)));
c1.setBackgroundColor(Color.gray);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
t.addCell(c1);

c1 = new PdfPCell(new Phrase(“AMD Plan”, FontFactory.getFont(
FontFactory.HELVETICA, 12, Font.BOLD, Color.BLACK)));
c1.setBackgroundColor(Color.gray);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
t.addCell(c1);

c1 = new PdfPCell(new Phrase(“Duration”, FontFactory.getFont(
FontFactory.HELVETICA, 12, Font.BOLD, Color.BLACK)));
c1.setBackgroundColor(Color.gray);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
t.addCell(c1);

c1 = new PdfPCell(new Phrase(“Package”, FontFactory.getFont(
FontFactory.HELVETICA, 12, Font.BOLD, Color.BLACK)));
c1.setBackgroundColor(Color.gray);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
t.addCell(c1);

t.setHeaderRows(1);

for (int i = 0; i < rowCount; i++) {

dfamd = (DfAmd) mylist.get(i);
t.addCell(dfamd.getDfShipUnits().getShip());

t.addCell(dfamd.getLastAmd());
t.addCell(dfamd.getAmdPlan());
t.addCell(dfamd.getDuration());
t.addCell(dfamd.getPackage_());

}

pdf.add(t);

// section1.add(t);
pdf.setPageSize(PageSize.A4);

pdf.add(pdf.getPageSize());

} catch (DocumentException e) {
System.err.println(e.getMessage());
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
pdf.close();
try {
downloadPDF();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
msging = new FacesMessage(FacesMessage.SEVERITY_WARN, "Error",
"Your Report Is Empty Please Search Some Data");
// msging ="Your Report Is Empty Please Search Some Data";

}
FacesContext.getCurrentInstance().addMessage(null, msging);
return "success";
}

// put new line after specific Element
private static void addEmptyLine(Paragraph paragraph, int number) {
for (int i = 0; i 0) {
output.write(buffer, 0, length);
}

// Finalize task.
output.flush();
} finally {
// Gently close streams.
close(output);
close(input);
}

facesContext.responseComplete();
}

private static void close(Closeable resource) {
if (resource != null) {
try {
resource.close();
} catch (IOException e) {

e.printStackTrace();
}
}
}

Advertisements
Comments
  1. Donovan Craffey says:

    That was sort of inspiring! Totally surprising. Now I understand what I’m going to perform tomorrow 🙂

    Like

  2. blackjack games free says:

    Some really choice articles on this internet site , saved to favorites .

    Like

  3. student loan database says:

    My brother recommended I may like this blog. He was once totally right. This put up actually made my day. You cann’t imagine just how so much time I had spent for this info! Thank you!

    Like

  4. blackjack shoe says:

    I like this weblog its a master peace ! Glad I noticed this on google. “Irrationally held truths may be more harmful than reasoned errors.” by Thomas Huxley.

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s