Example usage for com.lowagie.text Document Document

List of usage examples for com.lowagie.text Document Document

Introduction

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

Prototype


public Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom) 

Source Link

Document

Constructs a new Document -object.

Usage

From source file:com.foc.vaadin.FPrint.java

License:Apache License

public FPrint(Stack<Component> stack) {
    Document document = null;/*from   www.j a v a2 s  . co m*/
    try {
        document = new Document(PageSize.A4, 50, 50, 50, 50);

        PdfWriter.getInstance(document, os);
        document.open();
        ////////////////////////////////////////
        String value = null;
        String caption = null;
        while (!stack.isEmpty()) {
            Component comp = stack.pop();
            if (comp instanceof FVTextField) {
                caption = ((FVTextField) comp).getCaption();
                value = (String) ((FVTextField) comp).getValue();
            }

            document.add(new Paragraph("\n" + caption + ": " + value));

        }
        ////////////////////////////////////////
        Paragraph pp = new Paragraph();
        // Chapter ch = new Chapter();
        // ch.addSection(pp);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (document != null) {
            document.close();
        }
    }
}

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

License:Open Source License

/**
 * Prints the abductor's poster in PDF file.
 *
 * @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/*w w w .  j a va2  s .co m*/
 */
public ActionForward printPoster(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    // 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);

    // Retrieve the abductor
    try {
        int id = Integer.parseInt(request.getParameter("id"));

        Abductor abductor = abductorService.getAbductorById(id);

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

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

        // Open the document for writing
        document.open();
        // Add the banner
        Paragraph wantedParagraph = new Paragraph("W A N T E D",
                FontFactory.getFont(FontFactory.HELVETICA_BOLD, 36, Font.BOLD, new Color(255, 0, 0)));
        wantedParagraph.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(wantedParagraph);
        // Add name
        Paragraph redParagraph;
        if (!abductor.getNickname().isEmpty()) {
            redParagraph = new Paragraph(
                    abductor.getFirstName() + " \"" + abductor.getNickname() + "\" " + abductor.getLastName(),
                    FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(255, 0, 0)));
        } else {
            redParagraph = new Paragraph(abductor.getFirstName() + " " + abductor.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 birth date
        Paragraph blackParagraph;
        if (abductor.getBirthMonth() > 0) {
            blackParagraph = new Paragraph(
                    getResources(request).getMessage("label.date.birth") + ": "
                            + getResources(request).getMessage("month." + abductor.getBirthMonth()) + " "
                            + abductor.getBirthDay() + ", " + abductor.getBirthYear(),
                    FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0)));
            blackParagraph.setAlignment(Paragraph.ALIGN_CENTER);
            document.add(blackParagraph);
        }
        // Add birth place
        blackParagraph = new Paragraph(
                getResources(request).getMessage("label.address.city") + ": " + abductor.getCity(),
                FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0)));
        blackParagraph.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(blackParagraph);
        // Add sex
        blackParagraph = new Paragraph(
                getResources(request).getMessage("label.sex") + ": "
                        + getResources(request).getMessage("sex." + abductor.getSex()),
                FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0)));
        blackParagraph.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(blackParagraph);
        // Add height
        blackParagraph = new Paragraph(
                getResources(request).getMessage("label.height") + ": " + abductor.getFeet() + "' "
                        + abductor.getInches() + "\"",
                FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0)));
        blackParagraph.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(blackParagraph);
        // Add weight
        blackParagraph = new Paragraph(
                getResources(request).getMessage("label.weight") + ": " + abductor.getWeight() + " "
                        + getResources(request).getMessage("label.weight.lbs"),
                FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0)));
        blackParagraph.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(blackParagraph);
        // Add hair color
        blackParagraph = new Paragraph(
                getResources(request).getMessage("label.color.hair") + ": "
                        + getResources(request).getMessage("color.hair." + abductor.getHairColor()),
                FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0)));
        blackParagraph.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(blackParagraph);
        // Add eye color
        blackParagraph = new Paragraph(
                getResources(request).getMessage("label.color.eye") + ": "
                        + getResources(request).getMessage("color.eye." + abductor.getEyeColor()),
                FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0)));
        blackParagraph.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(blackParagraph);
        // Add race
        blackParagraph = new Paragraph(
                getResources(request).getMessage("label.race") + ": "
                        + getResources(request).getMessage("race." + abductor.getRace()),
                FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0)));
        blackParagraph.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(blackParagraph);
        // Add circumstance
        blackParagraph = new Paragraph(
                getResources(request).getMessage("label.remarks") + ": " + abductor.getRemarks(),
                FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0)));
        blackParagraph.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(blackParagraph);
        // Add line
        blackParagraph = new Paragraph("---------------------------------------");
        blackParagraph.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(blackParagraph);
        // Add contact
        blackParagraph = new Paragraph(getResources(request).getMessage("global.contact"),
                FontFactory.getFont(FontFactory.HELVETICA, 14, Font.NORMAL, new Color(0, 0, 0)));
        blackParagraph.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(blackParagraph);
        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=Poster.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);
    }
}

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

License:Open Source License

/**
 * Writes the cases to a PDF file./*from w  w  w  .  j  a  v  a  2  s .c om*/
 *
 * @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.PersonAction.java

License:Open Source License

/**
 * Prints the person's poster in PDF file.
 *
 * @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/* w  w  w  . j  a va  2s .c om*/
 */
public ActionForward printPoster(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    // 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);

    // Retrieve the person
    try {
        int id = Integer.parseInt(request.getParameter("id"));
        Person person = (Person) personService.getPersonById(id);

        // 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;

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

        // Open the document for writing
        document.open();
        // 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);
        // Add circumstance
        redParagraph = new Paragraph(getResources(request).getMessage("label.circumstance"),
                FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(255, 0, 0)));
        redParagraph.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(redParagraph);
        blackParagraph = new Paragraph(person.getCircumstance(),
                FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL));
        blackParagraph.setAlignment(Paragraph.ALIGN_JUSTIFIED);
        document.add(blackParagraph);
        // Add line
        blackParagraph = new Paragraph(
                "------------------------------------------------------------------------------");
        blackParagraph.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(blackParagraph);
        // Add contact
        blackParagraph = new Paragraph(getResources(request).getMessage("global.contact"),
                FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL));
        blackParagraph.setAlignment(Paragraph.ALIGN_JUSTIFIED);
        document.add(blackParagraph);
        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=Poster.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);
    }
}

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

License:Open Source License

/**
 * Prints the reports./* w w  w .ja va2s  .  com*/
 * 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./* ww  w  . j ava  2  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 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.gtdfree.test.PDFTestFont.java

License:Open Source License

/**
 * Fonts and encoding.//from  ww w.  j  a  va2 s.  c o m
 * @param args no arguments needed
 */
public static void main(String[] args) {

    System.out.println("Encodings");

    String[] names = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

    System.out.println(Arrays.toString(names));

    /*System.out.println("---");
    try {
    System.getProperties().store(System.out, "");
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    System.out.println("---");*/
    //System.out.println(System.getenv());
    //System.out.println("---");

    //String font= System.getProperty("java.home")+"/lib/fonts/LucidaBrightRegular.ttf";
    //String font= "fonts/DejaVuSans.ttf";

    //byte[] ttf= ApplicationHelper.loadResource(font);

    try {
        // step 1
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream("encodingfont.pdf"));
        // step 3
        document.open();
        // step 4
        String all[] = { "Symbol", "ZapfDingbats" };
        Font hex = new Font(Font.HELVETICA, 5);
        for (int z = 0; z < all.length; ++z) {
            String file = all[z];
            document.add(new Paragraph(
                    "Unicode equivalence for the font \"" + file + "\" with the encoding \"" + file + "\"\n"));
            /*char tb[];
            if (z == 0)
               tb = SYMBOL_TABLE;
            else
               tb = DINGBATS_TABLE;*/
            BaseFont bf;
            if (z == 2) {
                bf = BaseFont.createFont(file, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true, null, null);
                ;
            } else {
                bf = BaseFont.createFont(file, file, true);
            }
            Font f = new Font(bf, 12);
            PdfPTable table = new PdfPTable(16);
            table.setWidthPercentage(100);
            table.getDefaultCell().setBorderWidth(1);
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
            for (char k = 0; k < Character.MAX_VALUE; ++k) {
                char c = k;
                if (bf.charExists(c)) {
                    Phrase ph = new Phrase(12, new String(new char[] { c }), f);
                    ph.add(new Phrase(12, "\n" + Integer.toString(c) + "\n" + cst(c), hex));
                    table.addCell(ph);
                } /*
                  else {
                     Phrase ph = new Phrase("\u00a0");
                     ph.add(new Phrase(12, "\n\n" + cst(c), hex));
                     table.addCell(ph);
                  }*/
            }
            document.add(table);
            document.newPage();
        }
        // step 5
        document.close();

        FontFactory.registerDirectories();

        Set<?> s = FontFactory.getRegisteredFonts();

        System.out.println("Fonts: " + s);

        s = FontFactory.getRegisteredFamilies();

        System.out.println("Families: " + s);

        ArrayList<Font> f = new ArrayList<Font>(s.size());

        for (Object name : s) {

            try {
                f.add(FontFactory.getFont(name.toString(), "UTF-8", true, 12, Font.NORMAL, Color.BLACK, true));
            } catch (Exception e) {
                f.add(FontFactory.getFont(name.toString(), "UTF-8", false, 12, Font.NORMAL, Color.BLACK, true));
            }

        }

        Collections.sort(f, new Comparator<Font>() {
            @Override
            public int compare(Font o1, Font o2) {
                return o1.getFamilyname().compareTo(o2.getFamilyname());
            }
        });

        for (Font ff : f) {

            if (ff.getBaseFont() == null) {
                continue;
            }
            System.out.println(ff.getFamilyname() + " " + ff.getBaseFont().isEmbedded());

        }

    } catch (Exception de) {
        de.printStackTrace();
    }
}

From source file:com.gtdfree.test.TableBorders.java

License:Open Source License

/**
 * Demonstrates different borderstyles./*from w w w  .ja  v  a2s .co  m*/
 * 
 * @param args
 *            the number of rows for each table fragment.
 */
public static void main(String[] args) {

    System.out.println("Table Borders");
    // step1
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
        // step2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TableBorders.pdf"));
        // step3
        document.open();
        // step4

        // page 1
        Font tableFont = FontFactory.getFont("Helvetica", 8, Font.BOLD, Color.BLACK);
        float padding = 0f;
        Rectangle border = new Rectangle(0f, 0f);
        border.setBorderWidthLeft(6f);
        border.setBorderWidthBottom(5f);
        border.setBorderWidthRight(4f);
        border.setBorderWidthTop(2f);
        border.setBorderColorLeft(Color.RED);
        border.setBorderColorBottom(Color.ORANGE);
        border.setBorderColorRight(Color.YELLOW);
        border.setBorderColorTop(Color.GREEN);
        makeTestPage(tableFont, border, writer, document, padding, true, true);
        Font font = FontFactory.getFont("Helvetica", 10);
        Paragraph p;
        p = new Paragraph("\nVarious border widths and colors\nuseAscender=true, useDescender=true", font);
        document.add(p);

        document.newPage();

        // page 2
        padding = 2f;
        border = new Rectangle(0f, 0f);
        border.setBorderWidthLeft(1f);
        border.setBorderWidthBottom(2f);
        border.setBorderWidthRight(1f);
        border.setBorderWidthTop(2f);
        border.setBorderColor(Color.BLACK);
        makeTestPage(tableFont, border, writer, document, padding, true, true);
        p = new Paragraph(
                "More typical use - padding of 2\nuseBorderPadding=true, useAscender=true, useDescender=true",
                font);
        document.add(p);

        document.newPage();

        // page 3
        padding = 0f;
        border = new Rectangle(0f, 0f);
        border.setBorderWidthLeft(1f);
        border.setBorderWidthBottom(2f);
        border.setBorderWidthRight(1f);
        border.setBorderWidthTop(2f);
        border.setBorderColor(Color.BLACK);
        makeTestPage(tableFont, border, writer, document, padding, false, true);
        p = new Paragraph("\nuseBorderPadding=true, useAscender=false, useDescender=true", font);
        document.add(p);

        document.newPage();

        // page 4
        padding = 0f;
        border = new Rectangle(0f, 0f);
        border.setBorderWidthLeft(1f);
        border.setBorderWidthBottom(2f);
        border.setBorderWidthRight(1f);
        border.setBorderWidthTop(2f);
        border.setBorderColor(Color.BLACK);
        makeTestPage(tableFont, border, writer, document, padding, false, false);
        p = new Paragraph("\nuseBorderPadding=true, useAscender=false, useDescender=false", font);
        document.add(p);

        document.newPage();

        // page 5
        padding = 0f;
        border = new Rectangle(0f, 0f);
        border.setBorderWidthLeft(1f);
        border.setBorderWidthBottom(2f);
        border.setBorderWidthRight(1f);
        border.setBorderWidthTop(2f);
        border.setBorderColor(Color.BLACK);
        makeTestPage(tableFont, border, writer, document, padding, true, false);
        p = new Paragraph("\nuseBorderPadding=true, useAscender=true, useDescender=false", font);
        document.add(p);
    } catch (Exception de) {
        de.printStackTrace();
    }
    // step5
    document.close();
}

From source file:com.huateng.system.util.PdfUtil.java

License:Open Source License

public static void create(String mchtId, String selMchtId, String path, List<Object[]> list,
        LinkedHashMap<String, List<Object[]>> map, Set<String> set) throws Exception {

    BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

    ////  w  ww.  j  av a  2 s .  c  o m
    Font font17 = new Font(bfChinese, 17, Font.BOLD);
    Font font8 = new Font(bfChinese, 8, Font.NORMAL);
    Font font9 = new Font(bfChinese, 9, Font.NORMAL);
    Font font9Bold = new Font(bfChinese, 9, Font.BOLD);
    Font font8Red = new Font(bfChinese, 8, Font.NORMAL);
    font8Red.setColor(Color.RED);
    Font font8Green = new Font(bfChinese, 8, Font.NORMAL);
    font8Green.setColor(Color.GREEN);

    logger.info("Starting build document...");

    Document document = new Document(PageSize.A4, 36, 36, 36, 36);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();
    LINECANVAS border = new LINECANVAS();

    //
    float[] widths = { 0.1f, 0.35f, 0.35f, 0.1f, 0.1f };
    PdfPTable table = new PdfPTable(widths);
    table.setWidthPercentage(100);
    table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
    table.getDefaultCell().setFixedHeight(12);

    //CELL
    PdfPCell cellMchntId = new PdfPCell(new Paragraph(mchtId, font8));
    cellMchntId.setBorder(PdfPCell.BOTTOM);
    cellMchntId.setHorizontalAlignment(Element.ALIGN_CENTER);

    PdfPCell cellMchntName = new PdfPCell(new Paragraph(InformationUtil.getMchtName(mchtId), font8));
    cellMchntName.setBorder(PdfPCell.BOTTOM);
    cellMchntName.setHorizontalAlignment(Element.ALIGN_CENTER);

    String point = InformationUtil.getCurPaperPoint(selMchtId);
    PdfPCell cellPoint;
    cellPoint = new PdfPCell(new Paragraph(point, font8Red));
    cellPoint.setBorder(PdfPCell.NO_BORDER);
    cellPoint.setHorizontalAlignment(Element.ALIGN_CENTER);

    PdfPCell cellLevel;
    String level = InformationUtil.getCurPaperLevel(selMchtId);
    if (Integer.valueOf(point) >= 60) {
        cellLevel = new PdfPCell(new Paragraph(level, font8Green));
    } else {
        cellLevel = new PdfPCell(new Paragraph(level, font8Red));
    }
    cellLevel.setBorder(PdfPCell.NO_BORDER);
    cellLevel.setHorizontalAlignment(Element.ALIGN_CENTER);

    //
    Image img = Image.getInstance(
            ServletActionContext.getServletContext().getResource("/ext/resources/images/Title_logo.gif"));
    img.scalePercent(70);
    float w = img.getScaledWidth();
    float h = img.getScaledHeight();
    writer.getDirectContent().addImage(img, w, 0, 0, h, 36, PageSize.A4.getHeight() - 36 - h);

    //
    PdfPCell cell = new PdfPCell(new Paragraph("?", font17));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setColspan(5);
    cell.setFixedHeight(h);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setFixedHeight(20);
    cell.setColspan(5);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);

    table.addCell(new Paragraph("?", font8));
    table.addCell(cellMchntId);
    table.addCell(" ");
    table.addCell(new Paragraph(": ", font8));
    table.addCell(cellPoint);

    table.addCell(new Paragraph("??", font8));
    table.addCell(cellMchntName);
    table.addCell(" ");
    table.addCell(new Paragraph(": ", font8));
    table.addCell(cellLevel);

    document.add(table);
    document.add(new Paragraph("\n\n"));

    //?
    PdfPTable t = new PdfPTable(1);

    Iterator<Object[]> it0 = list.iterator();
    int i = 1;
    while (it0.hasNext()) {
        Object[] obj = it0.next();
        PdfPCell c = new PdfPCell();
        c.addElement(new Paragraph("Q" + String.valueOf(i++) + "" + obj[1].toString(), font9Bold));
        List<Object[]> opts = map.get(obj[0].toString());
        String opt = "";
        Iterator<Object[]> it1 = opts.iterator();
        while (it1.hasNext()) {
            Object[] o = it1.next();
            if (set.contains(o[0])) {
                opt += "? ";
                opt += o[1].toString();
                opt += "         ";
            } else {
                opt += " ";
                opt += o[1].toString();
                opt += "         ";
            }
        }
        c.addElement(new Paragraph(opt.trim(), font9));
        c.setBorder(PdfPCell.NO_BORDER);
        if (i - 1 != list.size()) {
            c.setCellEvent(border);
        }
        t.addCell(c);
    }

    PdfPTable oTable = new PdfPTable(1);
    oTable.setWidthPercentage(100);
    PdfPCell ce = new PdfPCell(t);
    ce.setBorderColor(Color.GRAY);
    oTable.addCell(ce);

    document.add(oTable);

    document.close();

    logger.info("Finish build document...");
}

From source file:com.huateng.system.util.PdfUtil.java

License:Open Source License

public static void create(String path, List<Object[]> list, LinkedHashMap<String, List<Object[]>> map)
        throws Exception {

    BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

    ////from  w  w w  . ja  v  a2s  . c o  m
    Font font17 = new Font(bfChinese, 17, Font.BOLD);
    Font font8 = new Font(bfChinese, 8, Font.NORMAL);
    Font font10 = new Font(bfChinese, 10, Font.NORMAL);
    Font font10Bold = new Font(bfChinese, 10, Font.BOLD);
    Font font8Red = new Font(bfChinese, 8, Font.NORMAL);
    font8Red.setColor(Color.RED);
    Font font8Green = new Font(bfChinese, 8, Font.NORMAL);
    font8Green.setColor(Color.GREEN);

    logger.info("Starting build document...");

    Document document = new Document(PageSize.A4, 36, 36, 36, 36);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();
    LINECANVAS border = new LINECANVAS();

    //
    float[] widths = { 0.1f, 0.35f, 0.55f };
    PdfPTable table = new PdfPTable(widths);
    table.setWidthPercentage(100);
    table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
    table.getDefaultCell().setFixedHeight(12);

    //CELL
    PdfPCell cellMchntId = new PdfPCell(new Paragraph("XXXXXXXXXXXXXXX", font8));
    cellMchntId.setBorder(PdfPCell.BOTTOM);
    cellMchntId.setHorizontalAlignment(Element.ALIGN_CENTER);

    PdfPCell cellMchntName = new PdfPCell(new Paragraph("?", font8));
    cellMchntName.setBorder(PdfPCell.BOTTOM);
    cellMchntName.setHorizontalAlignment(Element.ALIGN_CENTER);

    //
    Image img = Image.getInstance(
            ServletActionContext.getServletContext().getResource("/ext/resources/images/Title_logo.gif"));
    img.scalePercent(70);
    float w = img.getScaledWidth();
    float h = img.getScaledHeight();
    writer.getDirectContent().addImage(img, w, 0, 0, h, 36, PageSize.A4.getHeight() - 36 - h);

    //
    PdfPCell cell = new PdfPCell(new Paragraph("?", font17));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setColspan(3);
    cell.setFixedHeight(h);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setFixedHeight(20);
    cell.setColspan(3);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);

    table.addCell(new Paragraph("?", font8));
    table.addCell(cellMchntId);
    table.addCell(" ");

    table.addCell(new Paragraph("??", font8));
    table.addCell(cellMchntName);
    table.addCell(" ");

    document.add(table);
    document.add(new Paragraph("\n\n"));

    //?
    PdfPTable t = new PdfPTable(1);

    Iterator<Object[]> it0 = list.iterator();
    int i = 1;
    while (it0.hasNext()) {
        Object[] obj = it0.next();
        PdfPCell c = new PdfPCell();
        c.addElement(new Paragraph("Q" + String.valueOf(i++) + "" + obj[1].toString(), font10Bold));
        List<Object[]> opts = map.get(obj[0].toString());
        String opt = "";
        Iterator<Object[]> it1 = opts.iterator();
        while (it1.hasNext()) {
            Object[] o = it1.next();
            opt += " ";
            opt += o[1].toString();
            opt += "         ";
        }
        c.addElement(new Paragraph(opt.trim(), font10));
        c.setBorder(PdfPCell.NO_BORDER);
        if (i - 1 != list.size()) {
            c.setCellEvent(border);
        }
        t.addCell(c);
    }

    PdfPTable oTable = new PdfPTable(1);
    oTable.setWidthPercentage(100);
    PdfPCell ce = new PdfPCell(t);
    ce.setBorderColor(Color.GRAY);
    oTable.addCell(ce);

    document.add(oTable);

    document.close();

    logger.info("Finish build document...");
}