Posts Tagged ‘array’

Conversion PDF to Byte and Vice verse

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

Simple conversion easily enjoy the word of JAVA……………….

public static byte[] convertPDFToByteArray(String sourcePath) {
byte[] bytes=null;
InputStream inputStream;

File file = new File(sourcePath);

try {
inputStream = new FileInputStream(file);
bytes = new byte[(int)file.length()];
int read = inputStream.read(bytes);
} catch (IOException ex) {
Logger.getLogger(

DesktopApplication3View.class.getName()).log(Level.SEVERE, null, ex);
}return bytes;

}

public void convertByteArrayToPDF(String sourcePath,byte[] bytes) {
OutputStream out;
try {
out = new FileOutputStream(sourcePath);
try {
out.write(bytes);
out.close();
} catch (IOException ex) {
Logger.getLogger(DesktopApplication3View.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(DesktopApplication3View.class.getName()).log(Level.SEVERE, null, ex);
}

}

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();
}
}
}