Results 1 to 7 of 7
Thread: creation of pdf using itext java
- 09-16-2011, 06:28 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
- 09-16-2011, 07:05 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: creation of pdf using itext java
Can you show us, how did you try it out?
- 09-16-2011, 09:27 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
Re: creation of pdf using itext java
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package src; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; import java.io.FileOutputStream; import java.io.IOException; import java.util.Enumeration; import java.util.Hashtable; public class generatePDF { static String key, value; private static String FILE = "C:/Documents and Settings/rohan/pdf/FirstPdf32.pdf"; private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD); private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED); private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 17, Font.BOLD); private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD); private static Document document = new Document(PageSize.A4, 50, 50, 50, 50); public static void main(String[] args) { try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILE)); HeaderFooter event = new HeaderFooter(); writer.setBoxSize("art", new Rectangle(36, 54, 559, 788)); writer.setPageEvent((PdfPageEvent) event); document.open(); addMetaData(document); addImage(); addEmptyLine(document, 1); addHashTable(document); addEmptyLine(document, 5); document.close(); } catch (Exception e) { e.printStackTrace(); } } public static void addMetaData(Document document) { document.addTitle("PDF"); document.addSubject("border_practice"); document.addAuthor("Rohan"); document.addCreator("Rohan"); } public static void addHashTable(Document document) throws DocumentException { Hashtable Contents = new Hashtable(); Contents.put("Latitude", "17.34"); Contents.put("Longitude", "78.88"); Contents.put("Location", "12"); Contents.put("Name", "Rohan"); Contents.put("State", "Gujarat"); Contents.put("City", "Ahmedabad"); Contents.put("Location", "Naroda"); Contents.put("Street_name", "Naroda Main Road"); System.out.println("This is the size of the hash table " + Contents.size()); generateReport(Contents); } private static void addEmptyLine(Document document, int number) throws DocumentException { for (int i = 0; i < number; i++) { document.add(new Paragraph(" ")); } } static String toProperCase(String s) { return s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase(); } static class HeaderFooter extends PdfPageEventHelper { public void onEndPage(PdfWriter writer, Document document) { Rectangle rect = writer.getBoxSize("art"); ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(String.format("\u00A9 All rights reserved.")), (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0); } } private static void generateReport(Hashtable contents) throws DocumentException { Enumeration keys; keys = contents.keys(); Chunk chunk2 = new Chunk("LOS from selected BTS Report"); chunk2.setBackground(BaseColor.LIGHT_GRAY); chunk2.setFont(catFont); Paragraph title = new Paragraph(chunk2); title.setAlignment(Paragraph.ALIGN_CENTER); title.setFont(catFont); document.add(title); PdfPTable table = new PdfPTable(2); table.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell c1; addEmptyLine(document, 1); while (keys.hasMoreElements()) { key = (String) keys.nextElement(); value = (String) contents.get(key); String parts[] = key.split("_"); String camelCaseString = ""; for (String part : parts) { camelCaseString = camelCaseString + toProperCase(part) + " "; } key = camelCaseString; c1 = new PdfPCell(new Phrase(key)); c1.setHorizontalAlignment(Element.ALIGN_LEFT); c1.setBorder(0); table.addCell(c1); c1 = new PdfPCell(new Phrase(value)); c1.setHorizontalAlignment(Element.ALIGN_LEFT); c1.setBorder(0); table.addCell(c1); } table.setHorizontalAlignment(Element.ALIGN_CENTER); document.add(table); } private static void addImage() throws DocumentException, BadElementException, IOException { Image image = Image.getInstance("C:/Documents and Settings/rohan/pdf/logo.jpg"); image.setAlignment(Element.ALIGN_RIGHT); image.scaleAbsoluteHeight(20); image.scaleAbsoluteWidth(10); image.scalePercent(50); image.setAbsolutePosition(520, 800); document.add(image); } }Last edited by Eranga; 09-16-2011 at 12:36 PM. Reason: code tags added
- 09-16-2011, 09:31 AM #4
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
Re: creation of pdf using itext java
I am able to generate the pdf.
But I dont know how to include the page border as there is no method given in itext of page border
- 09-16-2011, 04:38 PM #5
Re: creation of pdf using itext java
I have run into this issue as well, and something I found online and implemented succesfully might be helpful.
You need to set a page event for your pdf writer and pass it a custom border class.
Add this right below your declaration of the PDFWriter
Then, you have to add the CustomBorder class, which makes use of a PdfPageEventHelper:Java Code:writer.setPageEvent(new CustomBorder());
Obviously you can tweak the border, but try that, should help you out.Java Code:import com.itextpdf.text.Document; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPageEventHelper; import com.itextpdf.text.pdf.PdfWriter; public class CustomBorder extends PdfPageEventHelper { public void onEndPage(PdfWriter writer, Document document) { PdfContentByte cb = writer.getDirectContent(); Rectangle pageSize = writer.getPageSize(); cb.rectangle(pageSize.getLeft() + 2, pageSize.getBottom() + 2, pageSize.getWidth() - 7, pageSize.getHeight() - 7); cb.stroke(); } }Last edited by sehudson; 09-16-2011 at 04:43 PM.
- 09-19-2011, 06:23 AM #6
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
Re: creation of pdf using itext java
Thanks a lot sir,
It's working fine.
- 09-20-2011, 04:56 AM #7
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
How to register a font in iText FontFactory in self signed Java applet?
By undertruck in forum New To JavaReplies: 0Last Post: 01-05-2011, 09:57 AM -
Itext with java : PDF nested table issues.. Please help
By akbjavauser in forum Advanced JavaReplies: 1Last Post: 07-14-2010, 05:43 PM -
iText Read Chuncks of PDF into java
By crimeunit in forum Advanced JavaReplies: 1Last Post: 05-19-2010, 07:58 AM -
How to merge two XFA formatted PDFs using iText and java ?
By dhanu8055 in forum Advanced JavaReplies: 0Last Post: 06-29-2009, 02:40 PM -
Java GUI creation help?
By sabrown311313 in forum New To JavaReplies: 4Last Post: 09-13-2008, 06:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks