Results 1 to 7 of 7
  1. #1
    kalambekar is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default creation of pdf using itext java

    I am trying to include a page border in pdf using itext in java.

    But I am unable to do so.

    Please can any one help me out

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    kalambekar is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default 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 01:36 PM. Reason: code tags added

  4. #4
    kalambekar is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default 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

  5. #5
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default 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

    Java Code:
    writer.setPageEvent(new CustomBorder());
    Then, you have to add the CustomBorder class, which makes use of a PdfPageEventHelper:

    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();
      } 
    }
    Obviously you can tweak the border, but try that, should help you out.
    Last edited by sehudson; 09-16-2011 at 05:43 PM.

  6. #6
    kalambekar is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: creation of pdf using itext java

    Thanks a lot sir,

    It's working fine.

  7. #7
    kalambekar is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: creation of pdf using itext java

    "The thread is solved"

Similar Threads

  1. Replies: 0
    Last Post: 01-05-2011, 10:57 AM
  2. Itext with java : PDF nested table issues.. Please help
    By akbjavauser in forum Advanced Java
    Replies: 1
    Last Post: 07-14-2010, 06:43 PM
  3. iText Read Chuncks of PDF into java
    By crimeunit in forum Advanced Java
    Replies: 1
    Last Post: 05-19-2010, 08:58 AM
  4. Replies: 0
    Last Post: 06-29-2009, 03:40 PM
  5. Java GUI creation help?
    By sabrown311313 in forum New To Java
    Replies: 4
    Last Post: 09-13-2008, 07:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •