Example usage for com.lowagie.text HeaderFooter setAlignment

List of usage examples for com.lowagie.text HeaderFooter setAlignment

Introduction

In this page you can find the example usage for com.lowagie.text HeaderFooter setAlignment.

Prototype


public void setAlignment(int alignment) 

Source Link

Document

Sets the alignment.

Usage

From source file:ambit2.db.reporters.PDFReporter.java

public void header(Document output, Q query) {

    output.addCreationDate();//from  ww  w .ja  va2s .c  om
    output.addCreator(getClass().getName());
    output.addSubject("");
    output.addAuthor("http://ambit.sourceforge.net");
    output.addHeader("License", getLicenseURI());
    output.addTitle(query.toString());
    output.addKeywords(query.toString());

    if (getLicenseURI() != null) {
        HeaderFooter footer = new HeaderFooter(new Phrase(String.format("License %s", getLicenseURI())), false);
        footer.setAlignment(Element.ALIGN_CENTER);
        output.setFooter(footer);
    }

    output.open();

    font = new Font(Font.TIMES_ROMAN, 10, Font.NORMAL);

    table = new PdfPTable(new float[] { 3f, 5f });
    table.setWidthPercentage(100);

    try {
        writeHeader(output);
    } catch (Exception x) {

    }

}

From source file:ch.gpb.elexis.cst.view.CstResultPart.java

License:Open Source License

private void makeActions(final Control viewer) {

    actionScreenshot = new Action() {
        public void run() {
            if (profile == null) {
                SWTHelper.alert("No profile", "Ohne Profil kann kein Resultat erzeugt werden");
                return;
            }//from   w ww .  j  av a 2 s  .  co  m

            GC gc = null;
            Image image = null;
            try {

                String latestPath = CoreHub.userCfg.get(CstPreference.CST_IDENTIFIER_LATESTPATH, null);
                if (latestPath == null) {
                    latestPath = System.getProperty("user.home");
                }

                FileDialog fd = new FileDialog(baseComposite.getShell(), SWT.SAVE);
                fd.setText("Save");
                fd.setFilterPath(latestPath);
                String[] filterExt = { "*.png", "*.*" };
                fd.setFilterExtensions(filterExt);
                fd.setFileName(CstService.generateFilename(patient));
                String selected = fd.open();

                if (selected == null) {
                    return;
                }

                File selFile = new File(selected);

                CoreHub.userCfg.set(CstPreference.CST_IDENTIFIER_LATESTPATH,
                        selFile.getParentFile().getAbsolutePath());

                //if (profile.getAnzeigeTyp().toLowerCase().equals("effektiv")) {
                if (profile.getAnzeigeTyp().toLowerCase().equals(CstProfile.ANZEIGETYP_EFFEKTIV)) {

                    if (profile.getAusgabeRichtung()) {
                        image = new Image(viewer.getDisplay(), 1123, viewer.getBounds().height);

                    } else {
                        image = new Image(viewer.getDisplay(), 794, viewer.getBounds().height);

                    }
                } else {
                    image = new Image(viewer.getDisplay(), 794, viewer.getBounds().height);

                }

                ImageLoader loader = new ImageLoader();

                gc = new GC(image);
                viewer.print(gc);

                gc.dispose();

                loader.data = new ImageData[] { image.getImageData() };
                loader.save(selected, SWT.IMAGE_PNG);

            } catch (Exception e) {
                log.error("Error saving png: " + e.toString());
                showMessage("Error while saving PNG", e.getMessage());
            } finally {
                if (image != null) {
                    image.dispose();
                }
                if (gc != null) {
                    gc.dispose();
                }
            }

        }
    };
    actionScreenshot.setText(Messages.Cst_Text_Save_as_png);
    actionScreenshot.setToolTipText(Messages.Cst_Text_Save_as_png);
    actionScreenshot.setImageDescriptor(
            PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJ_ELEMENT));
    actionScreenshot.setImageDescriptor(Activator.getImageDescriptor(Activator.IMG_PNG_PATH));

    // TODO: die pdf ausgabe ist eine ziemliche Baustelle - berarbeiten
    actionPdf = new Action() {
        public void run() {

            //////////////////////////
            if (profile == null) {
                SWTHelper.alert("No profile", "Ohne Profil kann kein Resultat erzeugt werden");
                return;
            }

            GC gc = null;
            Image image = null;
            try {
                String latestPath = CoreHub.userCfg.get(CstPreference.CST_IDENTIFIER_LATESTPATH, null);
                if (latestPath == null) {
                    latestPath = System.getProperty("user.home");
                }

                FileDialog fd = new FileDialog(baseComposite.getShell(), SWT.SAVE);
                fd.setText("Save");
                fd.setFilterPath(latestPath);
                String[] filterExt = { "*.pdf", "*.*" };
                fd.setFilterExtensions(filterExt);
                fd.setFileName(CstService.generateFilename(patient));
                String selected = fd.open();

                if (selected == null) {
                    return;
                }
                File selFile = new File(selected);

                CoreHub.userCfg.set(CstPreference.CST_IDENTIFIER_LATESTPATH,
                        selFile.getParentFile().getAbsolutePath());

                int printHeigth = 1123;
                int printWidth = 794;
                if (profile.getAusgabeRichtung()) {
                    printHeigth = 794;
                    printWidth = 1123;

                }

                // get the image from the viewport
                image = new Image(viewer.getDisplay(), printWidth, viewer.getBounds().height);
                ImageLoader loader = new ImageLoader();

                gc = new GC(image);
                viewer.print(gc);
                gc.dispose();

                // prepare title data
                //Date date = new Date();
                //SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy  HH:mm");

                Patient patient = Patient.load(profile.getKontaktId());
                //String sTitle = "Gemeinschaftspraxis Brunnmatt Dr. Beat Knzi ";
                String sTitle;
                sTitle = profile.getOutputHeader() == null ? "No header configured!"
                        : profile.getOutputHeader();

                if (sTitle == null || sTitle.length() == 0) {
                    sTitle = "No header configured!";
                }
                sTitle = sTitle + " Datum: " + CstService.getReadableDateAndTime();

                // get option (paging to A4/ in one piece)
                int pdfOutputOption = 0;
                boolean onePage = true;

                PdfOptionsDialog dialog = new PdfOptionsDialog(baseComposite.getShell());
                dialog.create();

                if (dialog.open() == Window.OK) {
                    pdfOutputOption = dialog.getPdfOutputOption();
                    if (pdfOutputOption == PdfOptionsDialog.OPTION_ONE_PAGE) {
                        onePage = true;
                    } else {

                        onePage = false;
                    }
                }

                float docHeight = viewer.getBounds().height;
                docHeight = docHeight / 7.5f;

                float fontSize = 12f;

                if (docHeight < 360f) {
                    docHeight = 360f;
                }

                BufferedImage bimage = ImageUtils.convertToAWT(image.getImageData());

                // create an Itextt Image from AWT BufferedImage
                com.lowagie.text.Image itextImage = null;
                java.awt.Image awtImage = null;

                try {
                    awtImage = Toolkit.getDefaultToolkit().createImage(bimage.getSource());
                    itextImage = com.lowagie.text.Image.getInstance(awtImage, null);

                } catch (Exception e) {
                    log.error("Error on image loading: " + e.toString());
                    e.printStackTrace();
                }

                // only for debugging

                //loader.data = new ImageData[] { image.getImageData() };
                //loader.save("C:\\Users\\daniel\\tmp\\debug.png", SWT.IMAGE_PNG);

                Rectangle pagesize = new Rectangle(595f, itextImage.getHeight() * 0.75f);

                // is it a4 quer?
                if (profile.getAusgabeRichtung()) {
                    pagesize = new Rectangle(842f, itextImage.getHeight() * 0.75f);

                }

                //System.out.println("pagesize: " + pagesize.toString());

                Document document;
                if (onePage) {
                    document = new Document(pagesize);

                } else {
                    document = new Document(PageSize.A4);
                    if (profile.getAusgabeRichtung()) {
                        document = new Document(PageSize.A4.rotate());
                    }

                }

                document.addCreationDate();

                try {
                    // creation of the different writers
                    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(selected));

                    // various fonts
                    BaseFont bf_helv = BaseFont.createFont(BaseFont.HELVETICA, "Cp1252", true);
                    BaseFont bf_times = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", true);
                    BaseFont bf_courier = BaseFont.createFont(BaseFont.COURIER, "Cp1252", true);

                    com.lowagie.text.Font fontHelv12 = new com.lowagie.text.Font(bf_helv, fontSize);

                    com.lowagie.text.Font fontTimes = new com.lowagie.text.Font(bf_times, fontSize);

                    fontTimes.setSize(fontSize);
                    fontTimes.setStyle(com.lowagie.text.Font.ITALIC);

                    Chunk chunkHeader = new Chunk(sTitle, FontFactory.getFont(FontFactory.HELVETICA, fontSize,
                            com.lowagie.text.Font.NORMAL, new java.awt.Color(255, 0, 0)));

                    com.lowagie.text.Phrase phraseHeader = new com.lowagie.text.Phrase(chunkHeader);

                    // headers and footers must be added before the document
                    // is opened

                    Chunk chunkFooter = new Chunk("Seite: ", FontFactory.getFont(FontFactory.HELVETICA,
                            fontSize, com.lowagie.text.Font.BOLD, new java.awt.Color(0, 0, 0)));

                    Phrase phraseFooter = new Phrase(chunkFooter);
                    phraseFooter.setFont(fontHelv12);

                    HeaderFooter footer = new HeaderFooter(phraseFooter, true);
                    footer.setBorder(Rectangle.NO_BORDER);
                    footer.setAlignment(Element.ALIGN_CENTER);

                    document.setFooter(footer);

                    Phrase headerPhrase = new Phrase(sTitle);
                    headerPhrase.setFont(fontTimes);

                    HeaderFooter header = new HeaderFooter(phraseHeader, false);
                    header.setBorder(Rectangle.BOTTOM);
                    header.setBorderWidth(0.5f);
                    header.setAlignment(Element.ALIGN_LEFT);
                    document.setHeader(header);

                    document.open();

                    //System.out.println("itext image w: " + itextImage.getWidth() + " h:" + itextImage.getHeight());

                    if (onePage) {

                        int scale = 66;
                        itextImage.scalePercent(scale);

                        document.add(itextImage);

                    } else {

                        BufferedImage[] imageChunks = ImageUtils.splitImageByHeigth(bimage, printHeigth);

                        for (int i = 0; i < imageChunks.length; i++) {

                            com.lowagie.text.Image itextImage2 = com.lowagie.text.Image.getInstance(
                                    Toolkit.getDefaultToolkit().createImage(imageChunks[i].getSource()), null);

                            // width becomes typically 523 (595 - 72) for a4Hoch or 770 (842 - 72) for A4Quer
                            float imgWidth = document.getPageSize().getWidth() - document.leftMargin()
                                    - document.rightMargin();
                            float imgHeigth = itextImage.getHeight() * 0.75f;

                            itextImage2.setAbsolutePosition(30, 20);
                            int scale = 66;
                            itextImage2.scalePercent(scale);

                            document.add(itextImage2);
                            document.newPage();

                        }

                    }

                    // we're done!
                    document.close();

                    ///////////////////////////////

                } catch (Exception ex) {
                    log.error(ex.getMessage());
                    showMessage("Error while generating PDF", ex.getMessage());
                }

            } finally {
                if (image != null) {
                    image.dispose();
                }
                if (gc != null) {
                    gc.dispose();
                }
                /*
                image.dispose();
                gc.dispose();
                */
            }

        }
    };

    actionPdf.setText(Messages.Cst_Text_Save_as_pdf);
    actionPdf.setToolTipText(Messages.Cst_Text_Save_as_pdf);
    /*
     * actionPdf.setImageDescriptor(PlatformUI.getWorkbench() .getSharedImages()
     * .getImageDescriptor(ISharedImages.IMG_OBJ_FILE));
     */
    actionPdf.setImageDescriptor(Activator.getImageDescriptor(Activator.IMG_PDF_PATH));

}

From source file:ch.gpb.elexis.kgexporter.pdf.PdfHandler.java

License:Open Source License

private static HeaderFooter getHeader(String headerText) {
    Phrase headerPhrase = new Phrase(headerText);
    headerPhrase.setFont(fontTimes);/*  w w  w  .  ja v a 2 s . com*/

    HeaderFooter header = new HeaderFooter(headerPhrase, false);
    header.setBorder(Rectangle.BOTTOM);
    header.setBorderWidth(0.5f);
    header.setAlignment(Element.ALIGN_LEFT);

    return header;
}

From source file:com.concursive.connect.web.modules.wiki.utils.WikiPDFUtils.java

License:Open Source License

public static boolean exportToFile(WikiPDFContext context, Connection db) throws Exception {

    LOG.debug("exportToFile-> begin");

    // Context Objects
    Wiki wiki = context.getWiki();/*from   w  w w  . j  a  v  a2s  .  co  m*/
    Project project = context.getProject();
    File file = context.getFile();
    WikiExportBean exportBean = context.getExportBean();

    // Determine the content to parse
    String content = wiki.getContent();
    if (content == null) {
        return false;
    }

    // Create a pdf
    Document document = new Document(PageSize.LETTER);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));

    // Meta data
    document.addTitle(project.getTitle());
    document.addSubject(wiki.getSubject());
    document.addCreator("Concursive ConcourseConnect");
    document.addAuthor("Wiki Contributor");
    //writer.setPageEvent(new PageNumbersWatermark());

    if (!exportBean.getIncludeTitle()) {
        boolean hasTitle = StringUtils.hasText(wiki.getSubject());
        HeaderFooter pageFooter = new HeaderFooter(
                new Phrase(project.getTitle() + (hasTitle ? ": " + wiki.getSubject() : "") + " - page "),
                new Phrase(""));
        pageFooter.setAlignment(Element.ALIGN_CENTER);
        document.setFooter(pageFooter);
    }

    document.open();

    if (exportBean.getIncludeTitle()) {
        //HeaderFooter pageHeader = new HeaderFooter(new Phrase(project.getTitle()), false);
        //document.setHeader(pageHeader);
        boolean hasTitle = (StringUtils.hasText(wiki.getSubject()));
        HeaderFooter pageFooter = new HeaderFooter(
                new Phrase(project.getTitle() + (hasTitle ? ": " + wiki.getSubject() : "") + " - page "),
                new Phrase(""));
        pageFooter.setAlignment(Element.ALIGN_CENTER);
        document.setFooter(pageFooter);

        // Draw a title page
        Rectangle rectangle = new Rectangle(600, 30);
        rectangle.setBackgroundColor(new Color(100, 100, 100));
        LOG.debug("document.add(rectangle)");
        document.add(rectangle);

        document.add(new Paragraph(project.getTitle(), titleFont));
        if (!"".equals(wiki.getSubject())) {
            document.add(new Paragraph(wiki.getSubject(), titleFont));
        }
        document.add(Chunk.NEWLINE);
        document.add(new Paragraph("Last Modified: " + wiki.getModified(), titleSmallFont));
        document.newPage();
    }

    ArrayList<Integer> wikiListDone = new ArrayList<Integer>();

    appendWiki(context, context.getWiki(), document, db, wikiListDone);
    // Close everything
    document.close();
    writer.close();
    LOG.debug("exportToFile-> finished");
    return true;
}

From source file:com.googlecode.openmpis.action.CaseAction.java

License:Open Source License

/**
 * Writes the cases to a PDF file.//from ww w  .j  a  v  a2  s . c o m
 *
 * @param mapping       the ActionMapping used to select this instance
 * @param form          the optional ActionForm bean for this request
 * @param request       the HTTP Request we are processing
 * @param response      the HTTP Response we are processing
 * @return              the forwarding instance
 * @throws java.lang.Exception
 */
public ActionForward printCases(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    User currentUser = null;

    // Check if there exists a session
    if (request.getSession().getAttribute("currentuser") != null) {
        currentUser = (User) request.getSession().getAttribute("currentuser");
    }

    // Set the paper size and margins
    Document document = new Document(PageSize.LETTER.rotate(), 50, 50, 50, 50);

    // Create the PDF writer
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter.getInstance(document, baos);

    // Add some meta information to the document
    document.addTitle("Case Statistics");
    document.addAuthor("OpenMPIS");
    document.addSubject("Statistics for All Cases");
    document.addKeywords("OpenMPIS, missing, found, unidentified");
    document.addProducer();
    document.addCreationDate();
    document.addCreator("OpenMPIS version " + Constants.VERSION);

    // Set the header
    String date = simpleDateFormat.format(System.currentTimeMillis());
    document.setHeader(new HeaderFooter(new Phrase("Statistics for cases as of " + date), false));

    // Set the footer
    HeaderFooter footer = new HeaderFooter(new Phrase("Page : "), true);
    footer.setAlignment(Element.ALIGN_CENTER);
    document.setFooter(footer);

    // Open the document for writing
    document.open();
    Table table = new Table(2);
    table.setBorderWidth(1);
    table.setBorderColor(new Color(0, 0, 0));
    table.setPadding(2);
    table.setSpacing(0);
    Paragraph paragraph = new Paragraph("Cases",
            FontFactory.getFont(FontFactory.HELVETICA, 24, Font.BOLD, new Color(0, 0, 0)));
    paragraph.setAlignment(Paragraph.ALIGN_CENTER);
    Cell cell = new Cell(paragraph);
    cell.setHeader(true);
    cell.setColspan(2);
    table.addCell(cell);
    table.endHeaders();
    table.addCell("Total On-going Cases");
    table.addCell("" + personService.countOngoing());
    table.addCell("\t\t\t\t\tMissing Persons");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countMissing());
    table.addCell("\t\t\t\t\tFamily Abductions");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countFamilyAbduction());
    table.addCell("\t\t\t\t\tNon-Family Abductions");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countNonFamilyAbduction());
    table.addCell("\t\t\t\t\tRunaway Persons");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countRunaway());
    table.addCell("\t\t\t\t\tUnknown");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countUnknown());
    table.addCell("\t\t\t\t\tFound Persons");
    table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countFound());
    table.addCell("\t\t\t\t\tAbandoned Persons");
    table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countAbandoned());
    table.addCell("\t\t\t\t\tThrowaway Persons");
    table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countThrowaway());
    table.addCell("\t\t\t\t\tUnidentified");
    table.addCell(
            "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countUnidentified());
    table.addCell("Total Solved Cases");
    table.addCell("" + personService.countSolved());
    table.addCell("\t\t\t\t\tMissing Persons");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countSolvedMissing());
    table.addCell("\t\t\t\t\tFamily Abductions");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countSolvedFamilyAbduction());
    table.addCell("\t\t\t\t\tNon-Family Abductions");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countSolvedNonFamilyAbduction());
    table.addCell("\t\t\t\t\tRunaway Persons");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countSolvedRunaway());
    table.addCell("\t\t\t\t\tUnknown");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countSolvedUnknown());
    table.addCell("\t\t\t\t\tFound Persons");
    table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countSolvedFound());
    table.addCell("\t\t\t\t\tAbandoned Persons");
    table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countSolvedAbandoned());
    table.addCell("\t\t\t\t\tThrowaway Persons");
    table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countSolvedThrowaway());
    table.addCell("\t\t\t\t\tUnidentified");
    table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
            + personService.countSolvedUnidentified());
    table.addCell("Total Unsolved Cases");
    table.addCell("" + personService.countUnsolved());
    table.addCell("\t\t\t\t\tMissing Persons");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countUnsolvedMissing());
    table.addCell("\t\t\t\t\tFamily Abductions");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countUnsolvedFamilyAbduction());
    table.addCell("\t\t\t\t\tNon-Family Abductions");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countUnsolvedNonFamilyAbduction());
    table.addCell("\t\t\t\t\tRunaway Persons");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countUnsolvedRunaway());
    table.addCell("\t\t\t\t\tUnknown");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countUnsolvedUnknown());
    table.addCell("\t\t\t\t\tFound Persons");
    table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countUnsolvedFound());
    table.addCell("\t\t\t\t\tAbandoned Persons");
    table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countUnsolvedAbandoned());
    table.addCell("\t\t\t\t\tThrowaway Persons");
    table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countUnsolvedThrowaway());
    table.addCell("\t\t\t\t\tUnidentified");
    table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
            + personService.countUnsolvedUnidentified());
    table.addCell("Total Cases");
    table.addCell("" + personService.countAllPersons());
    table.addCell("\t\t\t\t\tTotal Missing Persons");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countAllMissing());
    table.addCell("\t\t\t\t\tTotal Found Persons");
    table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countAllFound());
    table.addCell("\t\t\t\t\tTotal Unidentified Persons");
    table.addCell(
            "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countUnidentified());
    table.addCell("Total Relatives");
    table.addCell("" + relativeService.countAllRelatives());
    table.addCell("Total Abductors");
    table.addCell("" + abductorService.countAllAbductors());
    document.add(table);
    if (currentUser != null) {
        // List ongoing cases
        document.setHeader(new HeaderFooter(new Phrase("List of ongoing cases as of " + date), false));
        document.newPage();
        float[] widths = { 0.05f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.05f, 0.15f, 0.1f, 0.1f, 0.05f };
        PdfPTable pdfptable = new PdfPTable(widths);
        pdfptable.setWidthPercentage(100);
        pdfptable.addCell(new Phrase("ID", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Last Name", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("First Name", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Nickname", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Middle Name", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Case Type", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Status", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Photo", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Relative", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Abductor", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Investigator", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        List<Person> personList = personService.listOngoing();
        if (personList != null) {
            for (Person person : personList) {
                // Process the photo
                String tokens[] = person.getPhoto().split("\\/");
                String defaultPhotoBasename = "";
                for (int i = 0; i < tokens.length - 1; i++) {
                    defaultPhotoBasename += tokens[i] + File.separator;
                }
                defaultPhotoBasename += tokens[tokens.length - 1];
                String absoluteDefaultPhotoFilename = getServlet().getServletContext().getRealPath("/")
                        + defaultPhotoBasename;
                Image image = Image.getInstance(absoluteDefaultPhotoFilename);
                image.scaleAbsolute(50, 75);
                image.setAlignment(Image.ALIGN_CENTER);

                pdfptable.addCell(
                        new Phrase("" + person.getId(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(person.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(person.getFirstName(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(person.getNickname(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(person.getMiddleName(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(new Phrase(getResources(request).getMessage("type." + person.getType()),
                        FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(getResources(request).getMessage("status.case." + person.getStatus()),
                                FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(image);
                String relativeName = "";
                if (person.getRelativeId() != null) {
                    Relative relative = relativeService.getRelativeById(person.getRelativeId());
                    relativeName = relative.getFirstName() + " " + relative.getLastName();
                }
                pdfptable.addCell(new Phrase(relativeName, FontFactory.getFont(FontFactory.HELVETICA, 8)));
                String abductorName = "";
                if (person.getAbductorId() != null) {
                    Abductor abductor = abductorService.getAbductorById(person.getAbductorId());
                    abductorName = abductor.getFirstName() + " " + abductor.getLastName();
                }
                pdfptable.addCell(new Phrase(abductorName, FontFactory.getFont(FontFactory.HELVETICA, 8)));
                String investigatorUsername = "";
                if (person.getInvestigatorId() != null) {
                    User investigator = userService.getUserById(person.getInvestigatorId());
                    investigatorUsername = investigator.getUsername();
                }
                pdfptable.addCell(
                        new Phrase(investigatorUsername, FontFactory.getFont(FontFactory.HELVETICA, 8)));
            }
        }
        document.add(pdfptable);

        // List solved cases
        document.setHeader(new HeaderFooter(new Phrase("List of solved cases as of " + date), false));
        document.newPage();
        pdfptable = new PdfPTable(widths);
        pdfptable.setWidthPercentage(100);
        pdfptable.addCell(new Phrase("ID", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Last Name", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("First Name", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Nickname", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Middle Name", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Case Type", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Status", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Photo", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Relative", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Abductor", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Investigator", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        personList = personService.listSolved();
        if (personList != null) {
            for (Person person : personList) {
                // Process the photo
                String tokens[] = person.getPhoto().split("\\/");
                String defaultPhotoBasename = "";
                for (int i = 0; i < tokens.length - 1; i++) {
                    defaultPhotoBasename += tokens[i] + File.separator;
                }
                defaultPhotoBasename += tokens[tokens.length - 1];
                String absoluteDefaultPhotoFilename = getServlet().getServletContext().getRealPath("/")
                        + defaultPhotoBasename;
                Image image = Image.getInstance(absoluteDefaultPhotoFilename);
                image.scaleAbsolute(50, 75);
                image.setAlignment(Image.ALIGN_CENTER);

                pdfptable.addCell(
                        new Phrase("" + person.getId(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(person.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(person.getFirstName(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(person.getNickname(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(person.getMiddleName(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(new Phrase(getResources(request).getMessage("type." + person.getType()),
                        FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(getResources(request).getMessage("status.case." + person.getStatus()),
                                FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(image);
                String relativeName = "";
                if (person.getRelativeId() != null) {
                    Relative relative = relativeService.getRelativeById(person.getRelativeId());
                    relativeName = relative.getFirstName() + " " + relative.getLastName();
                }
                pdfptable.addCell(new Phrase(relativeName, FontFactory.getFont(FontFactory.HELVETICA, 8)));
                String abductorName = "";
                if (person.getAbductorId() != null) {
                    Abductor abductor = abductorService.getAbductorById(person.getAbductorId());
                    abductorName = abductor.getFirstName() + " " + abductor.getLastName();
                }
                pdfptable.addCell(new Phrase(abductorName, FontFactory.getFont(FontFactory.HELVETICA, 8)));
                String investigatorUsername = "";
                if (person.getInvestigatorId() != null) {
                    User investigator = userService.getUserById(person.getInvestigatorId());
                    investigatorUsername = investigator.getUsername();
                }
                pdfptable.addCell(
                        new Phrase(investigatorUsername, FontFactory.getFont(FontFactory.HELVETICA, 8)));
            }
        }
        document.add(pdfptable);

        // List unsolved cases
        document.setHeader(new HeaderFooter(new Phrase("List of unsolved cases as of " + date), false));
        document.newPage();
        pdfptable = new PdfPTable(widths);
        pdfptable.setWidthPercentage(100);
        pdfptable.addCell(new Phrase("ID", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Last Name", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("First Name", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Nickname", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Middle Name", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Case Type", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Status", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Photo", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Relative", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Abductor", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        pdfptable.addCell(new Phrase("Investigator", FontFactory.getFont(FontFactory.HELVETICA, 12)));
        personList = personService.listUnsolved();
        if (personList != null) {
            for (Person person : personList) {
                // Process the photo
                String tokens[] = person.getPhoto().split("\\/");
                String defaultPhotoBasename = "";
                for (int i = 0; i < tokens.length - 1; i++) {
                    defaultPhotoBasename += tokens[i] + File.separator;
                }
                defaultPhotoBasename += tokens[tokens.length - 1];
                String absoluteDefaultPhotoFilename = getServlet().getServletContext().getRealPath("/")
                        + defaultPhotoBasename;
                Image image = Image.getInstance(absoluteDefaultPhotoFilename);
                image.scaleAbsolute(50, 75);
                image.setAlignment(Image.ALIGN_CENTER);

                pdfptable.addCell(
                        new Phrase("" + person.getId(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(person.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(person.getFirstName(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(person.getNickname(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(person.getMiddleName(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(new Phrase(getResources(request).getMessage("type." + person.getType()),
                        FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(getResources(request).getMessage("status.case." + person.getStatus()),
                                FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(image);
                String relativeName = "";
                if (person.getRelativeId() != null) {
                    Relative relative = relativeService.getRelativeById(person.getRelativeId());
                    relativeName = relative.getFirstName() + " " + relative.getLastName();
                }
                pdfptable.addCell(new Phrase(relativeName, FontFactory.getFont(FontFactory.HELVETICA, 8)));
                String abductorName = "";
                if (person.getAbductorId() != null) {
                    Abductor abductor = abductorService.getAbductorById(person.getAbductorId());
                    abductorName = abductor.getFirstName() + " " + abductor.getLastName();
                }
                pdfptable.addCell(new Phrase(abductorName, FontFactory.getFont(FontFactory.HELVETICA, 8)));
                String investigatorUsername = "";
                if (person.getInvestigatorId() != null) {
                    User investigator = userService.getUserById(person.getInvestigatorId());
                    investigatorUsername = investigator.getUsername();
                }
                pdfptable.addCell(
                        new Phrase(investigatorUsername, FontFactory.getFont(FontFactory.HELVETICA, 8)));
            }
        }
        document.add(pdfptable);
    }
    document.close();

    // Set the response to return the poster (PDF file)
    response.setContentType("application/pdf");
    response.setContentLength(baos.size());
    response.setHeader("Content-disposition", "attachment; filename=Case_Statistics.pdf");

    // Close the output stream
    baos.writeTo(response.getOutputStream());
    response.getOutputStream().flush();

    return null;
}

From source file:com.googlecode.openmpis.action.ReportAction.java

License:Open Source License

/**
 * Prints the reports.//w  w  w  . j av  a 2 s. c om
 * This is the print report action called from the Struts framework.
 *
 * @param mapping       the ActionMapping used to select this instance
 * @param form          the optional ActionForm bean for this request
 * @param request       the HTTP Request we are processing
 * @param response      the HTTP Response we are processing
 * @return              the forwarding instance
 * @throws java.lang.Exception
 */
public ActionForward printReport(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    User currentUser = null;

    // Check if there exists a session
    if (request.getSession().getAttribute("currentuser") == null) {
        return mapping.findForward(Constants.EXPIRED);
    } else {
        currentUser = (User) request.getSession().getAttribute("currentuser");
    }

    // Check if current user is authorized
    if ((currentUser.getGroupId() == 1) || (currentUser.getGroupId() == 2)) {
        // Retrieve person ID
        try {
            int personId = Integer.parseInt(request.getParameter("personid"));
            Person person = personService.getPersonById(personId);

            // Process the photo
            String tokens[] = person.getPhoto().split("\\/");
            String defaultPhotoBasename = "";
            for (int i = 0; i < tokens.length - 1; i++) {
                defaultPhotoBasename += tokens[i] + File.separator;
            }
            defaultPhotoBasename += tokens[tokens.length - 1];
            String absoluteDefaultPhotoFilename = getServlet().getServletContext().getRealPath("/")
                    + defaultPhotoBasename;

            // Set the paper size and margins
            Document document = new Document(PageSize.LETTER, 50, 50, 50, 50);

            // Create the PDF writer
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfWriter.getInstance(document, baos);

            // Add some meta information to the document
            document.addTitle("Progress Report");
            document.addAuthor("OpenMPIS");
            document.addSubject("Progress Report");
            document.addKeywords("OpenMPIS, missing, found, unidentified");
            document.addProducer();
            document.addCreationDate();
            document.addCreator("OpenMPIS version " + Constants.VERSION);

            // Set the header
            String date = simpleDateFormat.format(System.currentTimeMillis());
            document.setHeader(new HeaderFooter(new Phrase("Report for " + person.getFirstName() + " \""
                    + person.getNickname() + "\" " + person.getLastName() + " as of " + date), false));

            // Set the footer
            HeaderFooter footer = new HeaderFooter(new Phrase("Page : "), true);
            footer.setAlignment(Element.ALIGN_CENTER);
            document.setFooter(footer);

            // Open the document for writing
            document.open();
            // Print the information on person
            // Add the banner
            if (person.getType() > 4) {
                Paragraph foundParagraph = new Paragraph("F O U N D",
                        FontFactory.getFont(FontFactory.HELVETICA_BOLD, 36, Font.BOLD, new Color(255, 0, 0)));
                foundParagraph.setAlignment(Paragraph.ALIGN_CENTER);
                document.add(foundParagraph);
            } else {
                Paragraph missingParagraph = new Paragraph("M I S S I N G",
                        FontFactory.getFont(FontFactory.HELVETICA_BOLD, 36, Font.BOLD, new Color(255, 0, 0)));
                missingParagraph.setAlignment(Paragraph.ALIGN_CENTER);
                document.add(missingParagraph);
            }
            // Add date missing or found
            Paragraph blackParagraph = new Paragraph(
                    getResources(request).getMessage("month." + person.getMonthMissingOrFound()) + " "
                            + person.getDayMissingOrFound() + ", " + person.getYearMissingOrFound(),
                    FontFactory.getFont(FontFactory.HELVETICA, 10, Font.BOLD, new Color(0, 0, 0)));
            blackParagraph.setAlignment(Paragraph.ALIGN_CENTER);
            document.add(blackParagraph);
            // Add missing from location
            if (person.getType() < 5) {
                blackParagraph = new Paragraph(
                        person.getMissingFromCity() + ", " + person.getMissingFromProvince(),
                        FontFactory.getFont(FontFactory.HELVETICA, 10, Font.BOLD, new Color(0, 0, 0)));
                blackParagraph.setAlignment(Paragraph.ALIGN_CENTER);
                document.add(blackParagraph);
            }
            // Add name
            Paragraph redParagraph;
            if (!person.getNickname().isEmpty()) {
                redParagraph = new Paragraph(
                        person.getFirstName() + " \"" + person.getNickname() + "\" " + person.getLastName(),
                        FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(255, 0, 0)));
            } else {
                redParagraph = new Paragraph(person.getFirstName() + " " + person.getLastName(),
                        FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(255, 0, 0)));
            }
            redParagraph.setAlignment(Paragraph.ALIGN_CENTER);
            document.add(redParagraph);
            // Add the photo
            Image image = Image.getInstance(absoluteDefaultPhotoFilename);
            image.scaleAbsolute(200, 300);
            image.setAlignment(Image.ALIGN_CENTER);
            document.add(image);
            // Add description
            redParagraph = new Paragraph("Description",
                    FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(255, 0, 0)));
            redParagraph.setAlignment(Paragraph.ALIGN_CENTER);
            document.add(redParagraph);
            float[] widths = { 0.5f, 0.5f };
            PdfPTable pdfptable = new PdfPTable(widths);
            pdfptable.setWidthPercentage(100);
            if (person.getType() < 5) {
                pdfptable
                        .addCell(
                                new Phrase(
                                        getResources(request).getMessage("label.date.birth") + ": "
                                                + getResources(request)
                                                        .getMessage("month." + person.getBirthMonth())
                                                + " " + person.getBirthDay() + ", " + person.getBirthYear(),
                                        FontFactory.getFont(FontFactory.HELVETICA, 12)));
                pdfptable.addCell(new Phrase(
                        getResources(request).getMessage("label.address.city") + ": " + person.getCity(),
                        FontFactory.getFont(FontFactory.HELVETICA, 12)));
            }
            pdfptable.addCell(new Phrase(
                    getResources(request).getMessage("label.sex") + ": "
                            + getResources(request).getMessage("sex." + person.getSex()),
                    FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable
                    .addCell(
                            new Phrase(
                                    getResources(request).getMessage("label.color.hair") + ": "
                                            + getResources(request)
                                                    .getMessage("color.hair." + person.getHairColor()),
                                    FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable
                    .addCell(new Phrase(
                            getResources(request).getMessage("label.height") + ": " + person.getFeet() + "' "
                                    + person.getInches() + "\"",
                            FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable
                    .addCell(
                            new Phrase(
                                    getResources(request).getMessage("label.color.eye") + ": "
                                            + getResources(request)
                                                    .getMessage("color.eye." + person.getEyeColor()),
                                    FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase(
                    getResources(request).getMessage("label.weight") + ": " + person.getWeight() + " "
                            + getResources(request).getMessage("label.weight.lbs"),
                    FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase(
                    getResources(request).getMessage("label.race") + ": "
                            + getResources(request).getMessage("race." + person.getRace()),
                    FontFactory.getFont(FontFactory.HELVETICA, 12)));
            document.add(pdfptable);

            // Print information on relative
            Relative relative = relativeService.getRelativeById(person.getRelativeId());

            document.newPage();
            document.add(new Paragraph(getResources(request).getMessage("label.relative.name") + ": "
                    + relative.getFirstName() + " " + relative.getLastName()));
            document.add(new Paragraph(getResources(request).getMessage("label.relation") + ": "
                    + getResources(request).getMessage("relation." + person.getRelationToRelative())));
            document.add(new Paragraph(getResources(request).getMessage("label.address") + ": "
                    + relative.getStreet() + ", " + relative.getCity() + ", " + relative.getProvince()));
            document.add(new Paragraph(
                    getResources(request).getMessage("label.number") + ": " + relative.getNumber()));
            document.add(new Paragraph(
                    getResources(request).getMessage("label.email") + ": " + relative.getEmail()));

            // Print information on abductor
            if (person.getAbductorId() != null) {
                Abductor abductor = abductorService.getAbductorById(person.getAbductorId());

                document.newPage();
                document.add(new Paragraph(getResources(request).getMessage("label.abductor.name") + ": "
                        + abductor.getFirstName() + " " + abductor.getLastName()));
            }

            // Print sightings
            if ((currentUser.getGroupId() == 1) || (currentUser.getGroupId() == 2)) {
                document.setHeader(new HeaderFooter(new Phrase("List of sightings as of " + date), false));
                document.newPage();
                float sightingsWidths[] = { 0.1f, 0.1f, 0.1f, 0.3f, 0.1f, 0.1f, 0.1f, 0.1f };
                pdfptable = new PdfPTable(sightingsWidths);
                pdfptable.setWidthPercentage(100);
                pdfptable.addCell(new Phrase(getResources(request).getMessage("label.id"),
                        FontFactory.getFont(FontFactory.HELVETICA, 12)));
                pdfptable.addCell(new Phrase(getResources(request).getMessage("label.date.sent"),
                        FontFactory.getFont(FontFactory.HELVETICA, 12)));
                pdfptable.addCell(new Phrase(getResources(request).getMessage("label.subject"),
                        FontFactory.getFont(FontFactory.HELVETICA, 12)));
                pdfptable.addCell(new Phrase(getResources(request).getMessage("label.message"),
                        FontFactory.getFont(FontFactory.HELVETICA, 12)));
                pdfptable.addCell(new Phrase(getResources(request).getMessage("label.lastname"),
                        FontFactory.getFont(FontFactory.HELVETICA, 12)));
                pdfptable.addCell(new Phrase(getResources(request).getMessage("label.firstname"),
                        FontFactory.getFont(FontFactory.HELVETICA, 12)));
                pdfptable.addCell(new Phrase(getResources(request).getMessage("label.email"),
                        FontFactory.getFont(FontFactory.HELVETICA, 12)));
                pdfptable.addCell(new Phrase(getResources(request).getMessage("label.ipaddress"),
                        FontFactory.getFont(FontFactory.HELVETICA, 12)));
                List<Message> sightingList = messageService.listAllSightingsForPerson(personId);
                for (Message sighting : sightingList) {
                    pdfptable.addCell(
                            new Phrase("" + sighting.getId(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                    pdfptable.addCell(
                            new Phrase(sighting.getDate(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                    pdfptable.addCell(
                            new Phrase(sighting.getSubject(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                    pdfptable.addCell(
                            new Phrase(sighting.getMessage(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                    pdfptable.addCell(
                            new Phrase(sighting.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                    pdfptable.addCell(
                            new Phrase(sighting.getFirstName(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                    pdfptable.addCell(
                            new Phrase(sighting.getEmail(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                    pdfptable.addCell(
                            new Phrase(sighting.getIpAddress(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                }
                document.add(pdfptable);
            }

            // Print progress reports
            if ((currentUser.getGroupId() == 1) || (currentUser.getGroupId() == 2)) {
                document.setHeader(
                        new HeaderFooter(new Phrase("List of progress reports as of " + date), false));
                document.newPage();
                float reportsWidths[] = { 0.1f, 0.1f, 0.3f };
                pdfptable = new PdfPTable(reportsWidths);
                pdfptable.setWidthPercentage(100);
                pdfptable.addCell(new Phrase(getResources(request).getMessage("label.id"),
                        FontFactory.getFont(FontFactory.HELVETICA, 12)));
                pdfptable.addCell(new Phrase(getResources(request).getMessage("label.date.reported"),
                        FontFactory.getFont(FontFactory.HELVETICA, 12)));
                pdfptable.addCell(new Phrase(getResources(request).getMessage("label.report"),
                        FontFactory.getFont(FontFactory.HELVETICA, 12)));
                List<Report> reportList = reportService.listAllReportsForPerson(personId);
                for (Report report : reportList) {
                    pdfptable.addCell(
                            new Phrase("" + report.getId(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                    pdfptable.addCell(
                            new Phrase(report.getDate(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                    pdfptable.addCell(
                            new Phrase(report.getReport(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                }
                document.add(pdfptable);
            }
            document.close();

            // Set the response to return the poster (PDF file)
            response.setContentType("application/pdf");
            response.setContentLength(baos.size());
            response.setHeader("Content-disposition", "attachment; filename=Report.pdf");

            // Close the output stream
            baos.writeTo(response.getOutputStream());
            response.getOutputStream().flush();

            return null;
        } catch (NumberFormatException nfe) {
            return mapping.findForward(Constants.LIST_PERSON);
        } catch (NullPointerException npe) {
            return mapping.findForward(Constants.LIST_PERSON);
        }
    } else {
        return mapping.findForward(Constants.UNAUTHORIZED);
    }
}

From source file:com.googlecode.openmpis.action.UserAction.java

License:Open Source License

/**
 * Writes the users to a PDF file.//w ww  .  jav a2s  . c o  m
 *
 * @param mapping       the ActionMapping used to select this instance
 * @param form          the optional ActionForm bean for this request
 * @param request       the HTTP Request we are processing
 * @param response      the HTTP Response we are processing
 * @return              the forwarding instance
 * @throws java.lang.Exception
 */
public ActionForward printUsers(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    User currentUser = null;

    // Check if there exists a session
    if (request.getSession().getAttribute("currentuser") != null) {
        currentUser = (User) request.getSession().getAttribute("currentuser");
    }

    // Set the paper size and margins
    Document document = new Document(PageSize.LETTER.rotate(), 50, 50, 50, 50);

    // Create the PDF writer
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter.getInstance(document, baos);

    // Add some meta information to the document
    document.addTitle("Case Statistics");
    document.addAuthor("OpenMPIS");
    document.addSubject("Statistics for All Cases");
    document.addKeywords("OpenMPIS, missing, found, unidentified");
    document.addProducer();
    document.addCreationDate();
    document.addCreator("OpenMPIS version " + Constants.VERSION);

    // Set the header
    String date = simpleDateFormat.format(System.currentTimeMillis());
    document.setHeader(new HeaderFooter(new Phrase("List of users as of " + date), false));

    // Set the footer
    HeaderFooter footer = new HeaderFooter(new Phrase("Page : "), true);
    footer.setAlignment(Element.ALIGN_CENTER);
    document.setFooter(footer);

    // Open the document for writing
    document.open();
    Table table = new Table(2);
    table.setBorderWidth(1);
    table.setPadding(2);
    table.setSpacing(0);
    Paragraph paragraph = new Paragraph("Users", FontFactory.getFont(FontFactory.HELVETICA, 24, Font.BOLD));
    paragraph.setAlignment(Paragraph.ALIGN_CENTER);
    Cell cell = new Cell(paragraph);
    cell.setHeader(true);
    cell.setColspan(2);
    table.addCell(cell);
    table.endHeaders();
    table.addCell("Total Administrators");
    table.addCell("" + userService.countAdministrators());
    table.addCell("\t\t\t\t\tActive Administrators");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + userService.countActiveAdministrators());
    table.addCell("\t\t\t\t\tSuspended Administrators");
    table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + userService.countSuspendedAdministrators());
    table.addCell("Total Encoders");
    table.addCell("" + userService.countEncoders());
    table.addCell("\t\t\t\t\tActive Encoders");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + userService.countActiveEncoders());
    table.addCell("\t\t\t\t\tSuspended Encoders");
    table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + userService.countSuspendedEncoders());
    table.addCell("Total Investigators");
    table.addCell("" + userService.countInvestigators());
    table.addCell("\t\t\t\t\tActive Investigators");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + userService.countActiveInvestigators());
    table.addCell("\t\t\t\t\tSuspended Investigators");
    table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + userService.countSuspendedInvestigators());
    table.addCell("Total Users");
    table.addCell("" + userService.countAllUsers());
    table.addCell("\t\t\t\t\tTotal Active Users");
    table.addCell("\t\t\t\t\t\t\t\t\t\t" + userService.countActiveUsers());
    table.addCell("\t\t\t\t\tTotal Suspended Users");
    table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + userService.countSuspendedUsers());
    document.add(table);
    if (currentUser != null) {
        if ((currentUser.getGroupId() == 0) || (currentUser.getGroupId() == 1)) {
            // List administrators
            document.setHeader(new HeaderFooter(new Phrase("List of administrators as of " + date), false));
            document.newPage();
            float[] widths = { 0.03f, 0.07f, 0.1f, 0.1f, 0.1f, 0.1f, 0.2f, 0.05f };
            PdfPTable pdfptable = new PdfPTable(widths);
            pdfptable.setWidthPercentage(100);
            pdfptable.addCell(new Phrase("ID", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("Group", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("Last Name", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("First Name", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("Agency", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("Designation", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("E-mail Address", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("Status", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            List<User> administratorList = userService.listAdministrators();
            for (User administrator : administratorList) {
                pdfptable.addCell(
                        new Phrase("" + administrator.getId(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(getResources(request).getMessage("group." + administrator.getGroupId()),
                                FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(administrator.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(new Phrase(administrator.getFirstName(),
                        FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(administrator.getAgency(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(new Phrase(administrator.getDesignation(),
                        FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(administrator.getEmail(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(getResources(request).getMessage("status.user." + administrator.getStatus()),
                                FontFactory.getFont(FontFactory.HELVETICA, 8)));
            }
            document.add(pdfptable);

            // List encoders
            document.setHeader(new HeaderFooter(new Phrase("List of encoders as of " + date), false));
            document.newPage();
            pdfptable = new PdfPTable(widths);
            pdfptable.setWidthPercentage(100);
            pdfptable.addCell(new Phrase("ID", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("Group", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("Last Name", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("First Name", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("Agency", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("Designation", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("E-mail Address", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("Status", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            List<User> encoderList = userService.listEncoders();
            for (User encoder : encoderList) {
                pdfptable.addCell(
                        new Phrase("" + encoder.getId(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(new Phrase(getResources(request).getMessage("group." + encoder.getGroupId()),
                        FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(encoder.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(encoder.getFirstName(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(encoder.getAgency(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(encoder.getDesignation(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable
                        .addCell(new Phrase(encoder.getEmail(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(getResources(request).getMessage("status.user." + encoder.getStatus()),
                                FontFactory.getFont(FontFactory.HELVETICA, 8)));
            }
            document.add(pdfptable);

            // List investigators
            document.setHeader(new HeaderFooter(new Phrase("List of investigators as of " + date), false));
            document.newPage();
            pdfptable = new PdfPTable(widths);
            pdfptable.setWidthPercentage(100);
            pdfptable.addCell(new Phrase("ID", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("Group", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("Last Name", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("First Name", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("Agency", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("Designation", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("E-mail Address", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            pdfptable.addCell(new Phrase("Status", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            List<User> investigatorList = userService.listInvestigators();
            for (User investigator : investigatorList) {
                pdfptable.addCell(
                        new Phrase("" + investigator.getId(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(getResources(request).getMessage("group." + investigator.getGroupId()),
                                FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(investigator.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(investigator.getFirstName(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(investigator.getAgency(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(new Phrase(investigator.getDesignation(),
                        FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(investigator.getEmail(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
                pdfptable.addCell(
                        new Phrase(getResources(request).getMessage("status.user." + investigator.getStatus()),
                                FontFactory.getFont(FontFactory.HELVETICA, 8)));
            }
            document.add(pdfptable);
        }
    }
    document.close();

    // Set the response to return the poster (PDF file)
    response.setContentType("application/pdf");
    response.setContentLength(baos.size());
    response.setHeader("Content-disposition", "attachment; filename=User_Statistics.pdf");

    // Close the output stream
    baos.writeTo(response.getOutputStream());
    response.getOutputStream().flush();

    return null;
}

From source file:com.gp.cong.logisoft.lcl.report.FreightInvoiceLclPdfCreator.java

public void init(String outputFileName, String fileId)
        throws FileNotFoundException, DocumentException, Exception {
    document = new Document(PageSize.A4);
    document.setMargins(10, 10, 10, 10);
    pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(outputFileName));
    pdfWriter.setPageEvent(new FreightInvoiceLclPdfCreator(realPath, fileId));
    company = new SystemRulesDAO().getCompanyDetails();
    this.setBrand(fileId);
    HeaderFooter footer = new HeaderFooter(
            new Phrase(companyName, new Font(Font.HELVETICA, 10, Font.ITALIC, Color.BLACK)), false);
    footer.setBorder(Rectangle.NO_BORDER);
    footer.setAlignment(Element.ALIGN_CENTER);
    document.setFooter(footer);//from w w w .j av  a2  s .co  m
    document.open();
}

From source file:com.kahlon.guard.controller.DocumentManager.java

/**
 *
 * @param document// www.  j  a v  a  2  s .com
 */
public void preProcessPDF(Object document) {
    try {

        BaseFont bf_courier = BaseFont.createFont(BaseFont.COURIER, "Cp1252", false);

        Document pdf = (Document) document;
        pdf.setPageSize(PageSize.A4);
        pdf.setMargins(5f, 5f, 10f, 5f);

        // headers and footers must be added before the document is opened
        HeaderFooter footer = new HeaderFooter(new Phrase("page: ", new Font(bf_courier)), true);
        footer.setBorder(Rectangle.NO_BORDER);
        footer.setAlignment(Element.ALIGN_CENTER);
        pdf.setFooter(footer);

        //            HeaderFooter header = new HeaderFooter(
        //                        new Phrase("This is a header without a page number", new Font(bf_courier)), false);
        //            header.setAlignment(Element.ALIGN_CENTER);
        //            pdf.setHeader(header);

        pdf.open();

        String logoPath = "/resources/image/logo.png";
        ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();
        String logo = servletContext.getRealPath(logoPath);

        pdf.add(Image.getInstance(logo));

        Person person = context.getSelectedPerson().getDisplayPerson();

        String name = FacesMessageUtil.getMessage("person.name") + " : " + person.getName();
        String age = FacesMessageUtil.getMessage("person.age") + " : " + Integer.toString(person.getAge());
        String gender = FacesMessageUtil.getMessage("person.gender") + " : "
                + person.getGender().getDescription();
        String race = FacesMessageUtil.getMessage("person.ethnicity") + " : "
                + person.getEthnicity().getDescription();

        Person rootPerson = person.getRootPerson();
        PersonImage imgp = imageService.getLastestPersonImage(rootPerson.getId());
        Image imgb = Image.getInstance(imgp.getContent());
        imgb.scaleToFit(100, 120);

        PdfPTable headerTable = new PdfPTable(2);

        PdfPTable personTable = new PdfPTable(1);
        personTable.setWidthPercentage(100);
        PdfPCell cell;

        cell = new PdfPCell(new Phrase(name));
        cell.setBorder(Rectangle.NO_BORDER);
        personTable.addCell(cell);

        cell = new PdfPCell(new Phrase(age));
        cell.setBorder(Rectangle.NO_BORDER);
        personTable.addCell(cell);

        cell = new PdfPCell(new Phrase(gender));
        cell.setBorder(Rectangle.NO_BORDER);
        personTable.addCell(cell);

        cell = new PdfPCell(new Phrase(race));
        cell.setBorder(Rectangle.NO_BORDER);
        personTable.addCell(cell);

        BarcodePDF417 pdf417 = new BarcodePDF417();
        pdf417.setText(Integer.toString(person.getId()));
        Image img = pdf417.getImage();
        img.scalePercent(150, 60 * pdf417.getYHeight());
        cell = new PdfPCell(img);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setPaddingTop(15);
        personTable.addCell(cell);

        cell.addElement(personTable);
        headerTable.addCell(cell);

        cell = new PdfPCell(imgb);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setBorder(Rectangle.NO_BORDER);
        headerTable.addCell(cell);

        headerTable.setSpacingAfter(15);
        pdf.add(headerTable);

        LineSeparator lineSeparator = new LineSeparator();
        lineSeparator.setPercentage(82);
        pdf.add(lineSeparator);
        Paragraph space = new Paragraph();
        space.add("");
        space.setSpacingAfter(15);
        pdf.add(space);

    } catch (IOException e) {
        logger.log(Level.INFO, e.getMessage());
    } catch (BadElementException e) {
        logger.log(Level.INFO, e.getMessage());
    } catch (DocumentException e) {
        logger.log(Level.INFO, e.getMessage());
    } catch (Exception e) {
        logger.log(Level.INFO, e.getMessage());
    }

}

From source file:com.krawler.esp.handlers.PdfGenHandler.java

License:Open Source License

public static ByteArrayOutputStream getInvoiceForm1(String invoice) {
    ByteArrayOutputStream os = null;
    try {/*  www.  ja  v a  2s .c om*/
        os = new ByteArrayOutputStream();
        Document document = new Document(PageSize.A4, 15, 15, 15, 15);
        PdfWriter writer = PdfWriter.getInstance(document, os);
        Font font7 = FontFactory.getFont("Helvetica", 8, Font.BOLD, Color.BLACK);
        Font font10 = FontFactory.getFont("Helvetica", 14, Font.BOLD, Color.BLACK);
        HeaderFooter temp = new HeaderFooter(new Phrase(
                String.format("Note: This is a computer generated document and does not require signature"),
                font7), false);
        temp.setAlignment(Element.ALIGN_CENTER);
        document.setFooter(temp);
        document.open();
        JSONObject jbj = new JSONObject(invoice);
        String currency = "";
        com.krawler.utils.json.base.JSONArray gridinfo = jbj.getJSONArray("items");
        String ids[] = { "name", "address", "invoiceno", "paymentdate" };
        String uinfo[] = new String[ids.length];
        for (int i = 0; i < uinfo.length; i++) {
            uinfo[i] = jbj.getString(ids[i]);
        }
        PdfPTable tableinfo = new PdfPTable(1);
        PdfPTable table = new PdfPTable(2);
        Font font1 = FontFactory.getFont("Helvetica", 24, Font.BOLD, Color.BLACK);
        String cnxt = "";//StorageHandler.getProfileStorePath() + "/inceif-200.png";
        PdfPCell cell1 = null;
        try {
            Image img = Image.getInstance(cnxt);
            cell1 = new PdfPCell(img);
        } catch (Exception e) {
            cnxt = StorageHandler.GetProfileImgStorePath() + "/140-logo.png";
            Image img = Image.getInstance(cnxt);
            cell1 = new PdfPCell(img);
        }
        cell1.setBorder(0);
        cell1.setHorizontalAlignment(Element.ALIGN_RIGHT);
        tableinfo.addCell(cell1);
        tableinfo = addspace(1, tableinfo);
        tableinfo = addspace(4, tableinfo);
        cell1 = new PdfPCell(new Paragraph("INVOICE", font10));
        cell1.setBorder(0);
        cell1.setHorizontalAlignment(Element.ALIGN_RIGHT);
        tableinfo.addCell(cell1);
        tableinfo = addspace(2, tableinfo);
        table = addspace(1, table);
        //            cell1 = new PdfPCell(new Paragraph(uinfo[uinfo.length - 1],font9));
        //            cell1.setBorder(0);
        //            cell1.setHorizontalAlignment(Element.ALIGN_RIGHT);
        //            tableinfo.addCell(cell1);
        //            tableinfo = addspace(1, tableinfo);
        //            tableinfo = addspace(2, tableinfo);
        cell1 = new PdfPCell(tableinfo);
        cell1.setBorder(0);
        table.addCell(cell1);
        PdfPTable tb = new PdfPTable(1);
        document.add(new Paragraph("\u00a0"));
        tableinfo = addspace(1, tableinfo);

        Font font = FontFactory.getFont("Helvetica", 10, Font.BOLD, Color.BLACK);
        Font font2 = FontFactory.getFont("Helvetica", 10, Font.NORMAL, Color.BLACK);
        //Font font3 = FontFactory.getFont("Helvetica", 8, Font.ITALIC, Color.BLACK);
        Font font11 = FontFactory.getFont("Helvetica", 12, Font.BOLD, Color.BLACK);
        PdfPCell c1 = null;
        c1 = new PdfPCell(new Paragraph("Bill To :", font2));
        c1.setBorder(0);
        tb.addCell(c1);
        for (int i = 0; i < 2; i++) {
            PdfPCell c = null;
            if (i == 0)
                c = new PdfPCell(new Paragraph(uinfo[i], font11));
            else
                c = new PdfPCell(new Paragraph(uinfo[i]));
            //c.(20);
            c.setBorder(0);
            tb.addCell(c);
        }
        PdfPCell c = new PdfPCell(tb);
        c.setBorder(0);
        table.addCell(c);
        PdfPTable tb1 = new PdfPTable(1);
        for (int i = 2; i < 4; i++) {
            PdfPTable nested1 = new PdfPTable(3);
            c = new PdfPCell();
            c.setBorder(0);
            c.setPaddingRight(5);
            nested1.addCell(c);
            c = new PdfPCell(new Paragraph(invoiceheadings[i], font));
            c.setBorder(0);

            nested1.addCell(c);
            c = new PdfPCell(new Paragraph(uinfo[i], font2));
            c.setBorder(0);
            c.setHorizontalAlignment(Element.ALIGN_RIGHT);
            //c.setPaddingLeft(5);
            nested1.addCell(c);

            c = new PdfPCell(nested1);
            c.setBorder(0);
            tb1.addCell(c);
        }
        PdfPCell c2 = new PdfPCell(tb1);
        c2.setBorder(0);
        table.addCell(c2);
        PdfPTable tb2 = new PdfPTable(new float[] { 60, 40 });

        /*  for (int i = 6; i < 7; i++) {
        PdfPTable tb3 = new PdfPTable(2);//new float[]{20, 40});
        tb3.setWidths(new int[]{1, 3});
        c2 = new PdfPCell(new Paragraph(invoiceheadings[i], font));
        c2.setBorder(0);
        tb3.addCell(c2);
        c2 = new PdfPCell(new Paragraph(uinfo[i]));
        c2.setBorder(0);
        tb3.addCell(c2);
        c2 = new PdfPCell(tb3);
        c2.setBorder(0);
        c2.setHorizontalAlignment(Element.ALIGN_LEFT);
        tb2.addCell(c2);
        tb2 = addspace(1, tb2);
          }
        */
        PdfPTable maintable = new PdfPTable(1);
        c2 = new PdfPCell(table);
        c2.setBorder(0);
        maintable.addCell(c2);
        maintable = addspace(0, maintable);
        c2 = new PdfPCell(tb2);
        c2.setBorder(0);
        maintable.addCell(c2);
        document.add(maintable);
        //for (int i = 0; i < 2; i++) {
        document.add(new Paragraph("\u00a0"));
        //}
        String[] colwidth2 = { "Subscription date", "Description", "Rate ($/Project)", "Amount ($)" };

        PdfPTable table1 = new PdfPTable(4);
        table1.setWidths(new float[] { 2, 3, 1, 1 });
        for (int i = 0; i < colwidth2.length; i++) {
            PdfPCell cell = new PdfPCell(new Paragraph(colwidth2[i], font));
            cell.setBorder(0);
            if (i != 1) {
                cell.setBorderWidthLeft(1);
                cell.setBorderWidthRight(1);
            }
            if (i == 3) {
                cell.setBorderWidthLeft(0);
                cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            } else {
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            }
            cell.setBorderWidthTop(1);
            cell.setBackgroundColor(Color.getHSBColor(0f, 0f, 0.9f));
            cell.setBorderWidthBottom(1);
            cell.setPaddingBottom(5);
            cell.setPaddingTop(5);
            table1.addCell(cell);
        }
        //table1.setHeaderRows(1);
        double d = 0;
        String[] di = { "billdate", "description", "rate", "amount" };
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");
        for (int i = 0; i < gridinfo.length(); i++) {
            currency = "USD";//gridinfo.getJSONObject(i).getString("currency");
            PdfPCell cell = null;
            for (int j = 0; j < di.length; j++) {
                if (j == 3) {
                    cell = new PdfPCell(new Paragraph(
                            df.format(Double.parseDouble(gridinfo.getJSONObject(i).getString(di[j]))), font2));
                    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                } else {
                    cell = new PdfPCell(new Paragraph(gridinfo.getJSONObject(i).getString(di[j]), font2));
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                }
                cell.setBorder(0);
                if (j != 1) {
                    cell.setBorderWidthLeft(1);
                    cell.setBorderWidthRight(1);
                }
                if (j == 3) {
                    cell.setBorder(0);
                    cell.setBorderWidthRight(1);
                }
                cell.setPaddingTop(10);
                cell.setFixedHeight(100);
                table1.addCell(cell);
            }
            d += Double.parseDouble(gridinfo.getJSONObject(i).getString(di[3]));

        }
        PdfPCell cell = null;
        cell = new PdfPCell(new Paragraph(" ", font));
        cell.setBorder(0);
        cell.setBorderWidthTop(1);
        cell.setBorderWidthBottom(1);
        cell.setBorderWidthLeft(1);
        cell.setBorderWidthRight(0);
        table1.addCell(cell);
        cell = new PdfPCell(new Paragraph("    ", font));
        cell.setBorder(0);
        cell.setBorderWidthTop(1);
        cell.setBorderWidthBottom(1);
        cell.setBorderWidthLeft(0);
        cell.setBorderWidthRight(0);
        table1.addCell(cell);
        cell = new PdfPCell(new Paragraph("     Total", font));
        cell.setBorder(0);
        cell.setBorderWidthTop(1);
        cell.setBorderWidthBottom(1);
        cell.setBorderWidthLeft(0);
        cell.setBorderWidthRight(0);
        cell.setPaddingBottom(5);
        table1.addCell(cell);
        cell = new PdfPCell(new Paragraph(" " + df.format(d), font));
        cell.setBorder(0);
        cell.setBorderWidthTop(1);
        cell.setBorderWidthBottom(1);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setBorderWidthLeft(0);
        cell.setBorderWidthRight(1);
        cell.setPaddingBottom(5);
        table1.addCell(cell);

        EnglishDecimalFormat f1 = new EnglishDecimalFormat();
        if (currency.equals("USD")) {
            cell = new PdfPCell(new Paragraph("Amount in words : " + CurrencyConvert(currency, d), font));
        } else {
            cell = new PdfPCell(new Paragraph(CurrencyConvert(currency, d), font));
        }
        cell.setColspan(4);
        cell.setBorder(0);
        //cell.setBorderWidthTop(1);
        cell.setBorderWidthBottom(1);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setBorderWidthLeft(1);
        cell.setBorderWidthRight(1);
        cell.setPaddingBottom(5);
        table1.addCell(cell);
        document.add(table1);
        document.add(new Paragraph("\u00a0"));
        PdfPTable inst = new PdfPTable(1);
        //            for (int i = 0; i < Inst1.length; i++) {
        //                c2 = new PdfPCell(new Paragraph(Inst1[i]));
        //                c2.setBorder(0);
        //                inst.addCell(c2);
        //            }
        document.add(inst);
        document.close();
        writer.close();
        os.close();

    }
    //        catch (ConfigurationException ex) {
    //            Logger.getLogger(PdfGenHandler.class.getName()).log(Level.SEVERE, null, ex);
    //        }
    catch (JSONException ex) {
        Logger.getLogger(PdfGenHandler.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(PdfGenHandler.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(PdfGenHandler.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception exp) {
        Logger.getLogger(PdfGenHandler.class.getName()).log(Level.SEVERE, null, exp);
    }
    return os;
}