Example usage for com.itextpdf.text.pdf PdfPTable setWidthPercentage

List of usage examples for com.itextpdf.text.pdf PdfPTable setWidthPercentage

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfPTable setWidthPercentage.

Prototype

public void setWidthPercentage(final float widthPercentage) 

Source Link

Document

Sets the width percentage that the table will occupy in the page.

Usage

From source file:org.restate.project.controller.PaymentReceiptController.java

License:Open Source License

@RequestMapping(method = RequestMethod.GET, value = "receipt.print")
public void downloadDocument(HttpServletResponse response,
        @RequestParam(value = "id", required = false) Integer id) throws Exception {

    if (id == null) {
        return;//  w  w w  .  j  av a  2  s  .  co  m
    }

    // creation of the document with a certain size and certain margins
    // may want to use PageSize.LETTER instead
    // Document document = new Document(PageSize.A4, 50, 50, 50, 50);

    Document doc = new Document();
    PdfWriter docWriter = null;

    DecimalFormat df = new DecimalFormat("0.00");
    File tmpFile = File.createTempFile("paymentReceipt", ".pdf");

    try {

        //special font sizes
        Font bfBold12 = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0));
        Font bf12 = new Font(Font.FontFamily.TIMES_ROMAN, 12);

        //file path

        docWriter = PdfWriter.getInstance(doc, new FileOutputStream(tmpFile));

        //document header attributes
        doc.addAuthor("betterThanZero");
        doc.addCreationDate();
        doc.addProducer();
        doc.addCreator("MySampleCode.com");
        doc.addTitle("Report with Column Headings");
        doc.setPageSize(PageSize.LETTER);

        //open document
        doc.open();

        //create a paragraph
        Paragraph paragraph = new Paragraph("iText  is a library that allows you to create and "
                + "manipulate PDF documents. It enables developers looking to enhance web and other "
                + "applications with dynamic PDF document generation and/or manipulation.");

        //specify column widths
        float[] columnWidths = { 1.5f, 2f, 5f, 2f };
        //create PDF table with the given widths
        PdfPTable table = new PdfPTable(columnWidths);
        // set table width a percentage of the page width
        table.setWidthPercentage(90f);

        //insert column headings
        insertCell(table, "Order No", Element.ALIGN_RIGHT, 1, bfBold12);
        insertCell(table, "Account No", Element.ALIGN_LEFT, 1, bfBold12);
        insertCell(table, "Account Name", Element.ALIGN_LEFT, 1, bfBold12);
        insertCell(table, "Order Total", Element.ALIGN_RIGHT, 1, bfBold12);
        table.setHeaderRows(1);

        //insert an empty row
        insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12);
        //create section heading by cell merging
        insertCell(table, "New York Orders ...", Element.ALIGN_LEFT, 4, bfBold12);
        double orderTotal, total = 0;

        //just some random data to fill
        for (int x = 1; x < 5; x++) {

            insertCell(table, "10010" + x, Element.ALIGN_RIGHT, 1, bf12);
            insertCell(table, "ABC00" + x, Element.ALIGN_LEFT, 1, bf12);
            insertCell(table, "This is Customer Number ABC00" + x, Element.ALIGN_LEFT, 1, bf12);

            orderTotal = Double.valueOf(df.format(Math.random() * 1000));
            total = total + orderTotal;
            insertCell(table, df.format(orderTotal), Element.ALIGN_RIGHT, 1, bf12);

        }
        //merge the cells to create a footer for that section
        insertCell(table, "New York Total...", Element.ALIGN_RIGHT, 3, bfBold12);
        insertCell(table, df.format(total), Element.ALIGN_RIGHT, 1, bfBold12);

        //repeat the same as above to display another location
        insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12);
        insertCell(table, "California Orders ...", Element.ALIGN_LEFT, 4, bfBold12);
        orderTotal = 0;

        for (int x = 1; x < 7; x++) {

            insertCell(table, "20020" + x, Element.ALIGN_RIGHT, 1, bf12);
            insertCell(table, "XYZ00" + x, Element.ALIGN_LEFT, 1, bf12);
            insertCell(table, "This is Customer Number XYZ00" + x, Element.ALIGN_LEFT, 1, bf12);

            orderTotal = Double.valueOf(df.format(Math.random() * 1000));
            total = total + orderTotal;
            insertCell(table, df.format(orderTotal), Element.ALIGN_RIGHT, 1, bf12);

        }
        insertCell(table, "California Total...", Element.ALIGN_RIGHT, 3, bfBold12);
        insertCell(table, df.format(total), Element.ALIGN_RIGHT, 1, bfBold12);

        //add the PDF table to the paragraph
        paragraph.add(table);
        // add the paragraph to the document
        doc.add(paragraph);

    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            //close the document
            doc.close();

        }
        if (docWriter != null) {
            //close the writer
            docWriter.close();
        }

        response.setHeader("Content-disposition", "attachment; filename=" + "sampleDoc" + ".pdf");
        response.setContentType("application/pdf");
        OutputStream outputStream = response.getOutputStream();
        FileInputStream fileInputStream = new FileInputStream(tmpFile);

        IOUtils.copy(fileInputStream, outputStream);
        fileInputStream.close();
        outputStream.flush();

        tmpFile.delete();
    }

}

From source file:org.sakaiproject.attendance.export.PDFEventExporterImpl.java

License:Educational Community License

private PdfPTable signInSheetTable() {

    PdfPTable table = new PdfPTable(2);
    table.setWidthPercentage(100);
    table.setSpacingBefore(12);// w ww .j a va2  s .c  o  m

    PdfPCell nameHeader = new PdfPCell(new Paragraph("Student Name", tableHeader));
    nameHeader.setPadding(10);

    PdfPCell signatureHeader = new PdfPCell(new Paragraph("Signature", tableHeader));
    signatureHeader.setPadding(10);

    table.addCell(nameHeader);
    table.addCell(signatureHeader);

    Collections.sort(users, new SortNameUserComparator());

    for (User user : users) {

        PdfPCell userCell = new PdfPCell(new Paragraph(user.getSortName(), body));
        userCell.setPadding(10);

        PdfPCell blankCell = new PdfPCell(new Paragraph());
        blankCell.setPadding(10);

        table.addCell(userCell);
        table.addCell(blankCell);
    }

    return table;

}

From source file:org.sakaiproject.attendance.export.PDFEventExporterImpl.java

License:Educational Community License

private PdfPTable attendanceSheetTable() {

    List<AttendanceStatus> activeStatuses = attendanceLogic.getActiveStatusesForSite(event.getAttendanceSite());
    int colSpan = activeStatuses.size() - 1;

    if (colSpan <= 0) {
        colSpan = 1;/*from   w ww  .  j a  v  a 2s.  c o  m*/
    }

    PdfPTable table = new PdfPTable(colSpan * 2);
    table.setWidthPercentage(100);
    table.setSpacingBefore(12);

    PdfPCell nameHeader = new PdfPCell(new Paragraph("Student Name", tableHeader));
    nameHeader.setPadding(10);
    nameHeader.setColspan(colSpan);
    table.addCell(nameHeader);

    int numStatusHeaders = 0;
    for (AttendanceStatus status : activeStatuses) {
        if (status.getStatus() != Status.UNKNOWN) {
            Paragraph statusHeaderParagraph = new Paragraph(getStatusString(status.getStatus(), colSpan),
                    tableHeader);
            statusHeaderParagraph.setAlignment(Element.ALIGN_CENTER);
            PdfPCell statusHeader = new PdfPCell(statusHeaderParagraph);
            statusHeader.setPadding(10);
            table.addCell(statusHeader);
            numStatusHeaders++;
        }
    }
    if (numStatusHeaders == 0) {
        Paragraph statusHeaderParagraph = new Paragraph("Status", tableHeader);
        statusHeaderParagraph.setAlignment(Element.ALIGN_CENTER);
        PdfPCell statusHeader = new PdfPCell(statusHeaderParagraph);
        statusHeader.setPadding(10);
        table.addCell(statusHeader);
    }

    Collections.sort(users, new SortNameUserComparator());

    for (User user : users) {

        PdfPCell userCell = new PdfPCell(
                new Paragraph(user.getSortName() + " (" + user.getDisplayId() + ")", body));
        userCell.setPadding(10);
        userCell.setColspan(colSpan);

        table.addCell(userCell);

        for (int i = 0; i < colSpan; i++) {
            // Add blank cell
            table.addCell(new PdfPCell(new Paragraph()));
        }

    }

    return table;
}

From source file:org.sharegov.cirm.utils.PDFExportUtil.java

License:Apache License

private static void createTable(Section subCatPart, Json allData) throws DocumentException {

    Json metaData = allData.at("metaData");
    PdfPTable table = null;
    PdfPCell c1 = null;// w  w w.  j  a va2s  .  c o  m

    int columns = metaData.at("columns").asInteger();
    table = new PdfPTable(columns);
    table.setWidthPercentage(100);
    if (columns == 9) {
        int[] widths = { 10, 20, 12, 12, 4, 6, 6, 8, 8 };
        table.setWidths(widths);
    } else {
        int[] widths = { 10, 25, 12, 12, 4, 6, 6, 8 };
        table.setWidths(widths);
    }
    // t.setBorderColor(BaseColor.GRAY);
    // t.setPadding(4);
    // t.setSpacing(4);
    // t.setBorderWidth(1);

    c1 = new PdfPCell(new Phrase(metaData.at("boid").asString(), smallBold));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
    table.addCell(c1);
    c1 = new PdfPCell(new Phrase(metaData.at("type").asString(), smallBold));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
    table.addCell(c1);
    c1 = new PdfPCell(new Phrase(metaData.at("fullAddress").asString(), smallBold));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
    table.addCell(c1);
    c1 = new PdfPCell(new Phrase(metaData.at("city").asString(), smallBold));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
    table.addCell(c1);
    c1 = new PdfPCell(new Phrase(metaData.at("zip").asString(), smallBold));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
    table.addCell(c1);
    c1 = new PdfPCell(new Phrase(metaData.at("hasStatus").asString(), smallBold));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
    table.addCell(c1);
    c1 = new PdfPCell(new Phrase(metaData.at("createdDate").asString(), smallBold));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
    table.addCell(c1);
    c1 = new PdfPCell(new Phrase(metaData.at("lastActivityUpdatedDate").asString(), smallBold));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
    table.addCell(c1);
    if (columns == 9) {
        c1 = new PdfPCell(new Phrase(metaData.at("gisColumn").asString(), smallBold));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
        table.addCell(c1);
    }
    table.setHeaderRows(1);

    List<Json> data = allData.at("data").asJsonList();
    for (Json dataValue : data) {
        if (!dataValue.at("hasCaseNumber").asString().isEmpty())
            c1 = new PdfPCell(new Phrase(dataValue.at("hasCaseNumber").asString(), smallFont));
        else
            c1 = new PdfPCell(new Phrase(GenUtils.makeCaseNumber(dataValue.at("boid").asLong()), smallFont));
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase(dataValue.at("label").asString(), smallFont));
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase(dataValue.at("fullAddress").asString(), smallFont));
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase(dataValue.at("Street_Address_City").asString(), smallFont));
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase(dataValue.at("Zip_Code").asString(), smallFont));
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase(dataValue.at("hasStatus").asString(), smallFont));
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase(dataValue.at("hasDateCreated").asString(), smallFont));
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase(dataValue.at("lastActivityUpdatedDate").asString(), smallFont));
        table.addCell(c1);
        if (columns == 9) {
            c1 = new PdfPCell(new Phrase(dataValue.at("gisColumn").asString(), smallFont));
            table.addCell(c1);
        }
    }

    subCatPart.add(table);
}

From source file:org.sharegov.cirm.utils.PDFViewReport.java

License:Apache License

private void addTopTable(Section subCatPart, Json data) throws DocumentException {
    PdfPTable table = new PdfPTable(4);
    table.setWidthPercentage(100);
    int[] widths = { 12, 55, 15, 20 };
    table.setWidths(widths);/*from   ww w .  ja v  a2  s . co  m*/

    PdfPCell cell = null;

    OWLNamedIndividual typeInd = individual(legacyPrefix + data.at("type").asString());
    addCell(cell, table, "Type:", Element.ALIGN_RIGHT, boldFont, null);
    addCell(cell, table, getEntityLabel(typeInd), Element.ALIGN_LEFT, normalFont, null);
    addCell(cell, table, "SR #:", Element.ALIGN_RIGHT, boldFont, null);
    if (data.at("properties").has("hasCaseNumber"))
        addCell(cell, table, data.at("properties").at("hasCaseNumber").asString(), Element.ALIGN_LEFT,
                normalFont, null);
    else
        addCell(cell, table, GenUtils.makeCaseNumber(data.at("boid").asLong()), Element.ALIGN_LEFT, normalFont,
                null);
    //02-20-2014 - Syed add providedby to report
    addCell(cell, table, "Area:", Element.ALIGN_RIGHT, boldFont, null);
    addCell(cell, table, getArea(typeInd), Element.ALIGN_LEFT, normalFont, null);
    addCell(cell, table, "Priority:", Element.ALIGN_RIGHT, boldFont, null);
    addCell(cell, table, data.at("properties").at("hasPriority").at("label").asString(), Element.ALIGN_LEFT,
            normalFont, null);
    addCell(cell, table, "Group:", Element.ALIGN_RIGHT, boldFont, null);
    addCell(cell, table, getGroup(typeInd), Element.ALIGN_LEFT, normalFont, null);
    addCell(cell, table, "Status:", Element.ALIGN_RIGHT, boldFont, null);
    addCell(cell, table, getEntityLabel(individual(data.at("properties"), "hasStatus")), Element.ALIGN_LEFT,
            normalFont, null);

    addCell(cell, table, "Jurisdiction:", Element.ALIGN_RIGHT, boldFont, null);
    OWLLiteral jurisdiction = dataProperty(typeInd, legacyPrefix + "hasJurisdictionDescription");
    addCell(cell, table, jurisdiction != null ? jurisdiction.getLiteral() : emptyField, Element.ALIGN_LEFT,
            normalFont, null);
    addCell(cell, table, "Status Date:", Element.ALIGN_RIGHT, boldFont, null);
    String modifiedDate = data.at("properties").has("hasDateLastModified")
            ? data.at("properties").at("hasDateLastModified").asString()
            : "";
    addCell(cell, table, formatDate(modifiedDate, true, false), Element.ALIGN_LEFT, normalFont, null);

    addCell(cell, table, "Input By:", Element.ALIGN_RIGHT, boldFont, null);
    String inputBy = data.at("properties").has("isCreatedBy")
            ? data.at("properties").at("isCreatedBy").asString()
            : emptyField;
    addCell(cell, table, inputBy, Element.ALIGN_LEFT, normalFont, null);
    addCell(cell, table, "Created Date:", Element.ALIGN_RIGHT, boldFont, null);
    String createdDate = data.at("properties").has("hasDateCreated")
            ? data.at("properties").at("hasDateCreated").asString()
            : "";
    addCell(cell, table, formatDate(createdDate, true, false), Element.ALIGN_LEFT, normalFont, null);

    addCell(cell, table, "CC Groups:", Element.ALIGN_RIGHT, boldFont, null);
    addCell(cell, table, blankField, Element.ALIGN_LEFT, normalFont, null);
    addCell(cell, table, "Created By:", Element.ALIGN_RIGHT, boldFont, null);
    addCell(cell, table, getEmployeeName(inputBy), Element.ALIGN_LEFT, normalFont, null);

    addCell(cell, table, "Location:", Element.ALIGN_RIGHT, boldFont, null);
    addCell(cell, table,
            data.at("properties").has("atAddress") ? buildAddress(data.at("properties").at("atAddress"))
                    : blankField,
            Element.ALIGN_LEFT, normalFont, null);
    addCell(cell, table, "Method Received:", Element.ALIGN_RIGHT, boldFont, null);
    addCell(cell, table, data.at("properties").at("hasIntakeMethod").at("label").asString(), Element.ALIGN_LEFT,
            normalFont, null);

    subCatPart.add(table);
    subCatPart.add(new Chunk(new LineSeparator()));
}

From source file:org.sharegov.cirm.utils.PDFViewReport.java

License:Apache License

private void addDescription(Section subCatPart, Json data) throws DocumentException {
    PdfPTable table = new PdfPTable(2);
    table.setWidthPercentage(100);
    int[] widths = { 15, 85 };
    table.setWidths(widths);//from   w  ww.  j  a  v  a 2  s.c  om

    PdfPCell cell = null;

    addCell(cell, table, "Description:", Element.ALIGN_LEFT, bigBoldFont, null);
    if (data.at("properties").has("hasDetails"))
        addCell(cell, table, data.at("properties").at("hasDetails").asString(), Element.ALIGN_LEFT, normalFont,
                null);

    subCatPart.add(table);
    subCatPart.add(new Chunk(new LineSeparator()));
}

From source file:org.sharegov.cirm.utils.PDFViewReport.java

License:Apache License

private void addQuestions(Section subCatPart, Json data)
        throws DocumentException, UnsupportedEncodingException {
    PdfPTable table = new PdfPTable(2);
    table.setWidthPercentage(100);
    int[] widths = { 65, 35 };
    table.setWidths(widths);//from ww w. j  a v  a2 s  .c o  m

    PdfPCell cell = null;

    addCell(cell, table, "SR Questions", Element.ALIGN_LEFT, bigBoldFont, null);
    addCell(cell, table, "Answers", Element.ALIGN_LEFT, bigBoldFont, null);

    subCatPart.add(table);
    //subCatPart.add(Chunk.NEWLINE);
    //subCatPart.add(new Chunk(new LineSeparator()));

    table = new PdfPTable(2);
    table.setWidthPercentage(100);
    table.setWidths(widths);

    OWLNamedIndividual typeInd = individual(legacyPrefix + data.at("type").asString());
    List<Json> allServiceAnswers = new ArrayList<Json>();
    LegacyEmulator.getAllServiceFields(typeInd, allServiceAnswers, true);

    List<Json> saList = getAllServiceAnswers(data);

    for (int k = 0; k < allServiceAnswers.size(); k++) {
        Json serviceAnswer = allServiceAnswers.get(k);
        BaseColor color = addColor(k);
        int counter = 0;
        addCell(cell, table, serviceAnswer.at("hasServiceField").at("label").asString(), Element.ALIGN_LEFT,
                normalFont, color);
        for (Json sa : saList) {
            if (sa.at("hasServiceField").at("iri").asString()
                    .equals(serviceAnswer.at("hasServiceField").at("iri").asString())) {
                OWLIndividual qtn = individual(sa.at("hasServiceField").at("iri").asString());
                OWLObjectProperty cvl = objectProperty(legacyPrefix + "hasChoiceValueList");

                if (sa.has("hasAnswerValue")) {
                    if (sa.at("hasAnswerValue").at("literal").isArray()) {
                        List<Json> literalList = sa.at("hasAnswerValue").at("literal").asJsonList();
                        for (int i = 0; i < literalList.size(); i++) {
                            String eachAns = literalList.get(i).asString();
                            addCell(cell, table, eachAns, Element.ALIGN_LEFT, normalFont, color);
                            if (i != literalList.size() - 1)
                                addCell(cell, table, blankField, Element.ALIGN_LEFT, normalFont, color);
                        }
                    } else {
                        String ans = sa.at("hasAnswerValue").at("literal").asString();
                        if (serviceAnswer.at("hasDataType").asString().equals("NUMBER"))
                            ans = Long.toString(new BigDecimal(ans).longValue());
                        if (serviceAnswer.at("hasDataType").asString().equals("DATE"))
                            ans = formatDate(ans, false, false);
                        addCell(cell, table, ans, Element.ALIGN_LEFT, normalFont, color);

                    }
                } else if (sa.has("hasAnswerObject")) {
                    if (sa.at("hasAnswerObject").isArray()) {
                        List<Json> answerObjectList = sa.at("hasAnswerObject").asJsonList();
                        for (int i = 0; i < answerObjectList.size(); i++) {
                            Json eachAnswerObject = answerObjectList.get(i);
                            //OWL.objectProperties(qtn, cvl.getIRI().toString());
                            if (!OWL.collectObjectProperties(qtn, cvl).isEmpty()
                                    && !containsWhiteSpace(eachAnswerObject.at("iri").asString())) {
                                addCell(cell, table,
                                        getEntityLabel(individual(eachAnswerObject.at("iri").asString())),
                                        Element.ALIGN_LEFT, normalFont, color);
                                if (i != answerObjectList.size() - 1)
                                    addCell(cell, table, blankField, Element.ALIGN_LEFT, normalFont, color);
                            }
                        }
                    } else {
                        String ans = sa.at("hasAnswerObject").at("iri").asString();
                        if (!OWL.collectObjectProperties(qtn, cvl).isEmpty() && !containsWhiteSpace(ans)) {
                            addCell(cell, table, getEntityLabel(individual(ans)), Element.ALIGN_LEFT,
                                    normalFont, color);
                        } else
                            addCell(cell, table, ans, Element.ALIGN_LEFT, normalFont, color);
                    }
                }
                ++counter;
            } else
                continue;
        }
        if (counter == 0)
            addCell(cell, table, emptyField, Element.ALIGN_LEFT, normalFont, color);
    }
    subCatPart.add(table);
    subCatPart.add(new Chunk(new LineSeparator()));
}

From source file:org.sharegov.cirm.utils.PDFViewReport.java

License:Apache License

private void addActors(Section subCatPart, Json data) throws DocumentException {
    if (data.at("properties").has("hasServiceCaseActor")) {
        int iActorHeaderColumns = 5;
        int iActorColumns = 6;
        PdfPTable table = new PdfPTable(iActorHeaderColumns);
        table.setWidthPercentage(100);
        int[] headerWidths = { 15, 15, 25, 18, 22 };
        int[] widths = { 15, 15, 25, 18, 5, 17 };
        table.setWidths(headerWidths);//  w  w  w .ja  v a 2  s. c o m
        PdfPCell cell = null;

        addCell(cell, table, "Customer", Element.ALIGN_LEFT, bigBoldFont, null);
        addCell(cell, table, "Name", Element.ALIGN_LEFT, bigBoldFont, null);
        addCell(cell, table, "Address", Element.ALIGN_LEFT, bigBoldFont, null);
        addCell(cell, table, "e-Mail", Element.ALIGN_LEFT, bigBoldFont, null);
        addCell(cell, table, "Contact No", Element.ALIGN_LEFT, bigBoldFont, null);

        subCatPart.add(table);
        //subCatPart.add(Chunk.NEWLINE);
        //subCatPart.add(new Chunk(new LineSeparator()));

        table = new PdfPTable(iActorColumns);
        table.setWidthPercentage(100);
        table.setWidths(widths);

        if (!data.at("properties").at("hasServiceCaseActor").isArray()) {
            data.at("properties").set("hasServiceCaseActor",
                    Json.array().add(data.at("properties").at("hasServiceCaseActor")));
        }

        for (int k = 0; k < data.at("properties").at("hasServiceCaseActor").asJsonList().size(); k++) {
            Json actor = data.at("properties").at("hasServiceCaseActor").at(k);
            BaseColor color = addColor(k);
            OWLNamedIndividual act = individual(actor, "hasServiceActor");
            String actorType = getEntityLabel(act);

            StringBuilder actorName = new StringBuilder();
            if (actor.has("Name"))
                actorName.append(actor.at("Name").asString());
            if (actor.has("LastName")) {
                if (actorName.toString().isEmpty())
                    actorName.append(actor.at("LastName").asString());
                else
                    actorName.append(" ").append(actor.at("LastName").asString());
            }
            if (actorName.toString().isEmpty())
                actorName.append(blankField);

            StringBuilder actorAddr = new StringBuilder();
            if (actor.has("atAddress")) {
                Json addr = actor.at("atAddress");
                if (addr.has("fullAddress"))
                    actorAddr.append(addr.at("fullAddress").asString());
                if (addr.has("Street_Unit_Number"))
                    actorAddr.append(", #").append(addr.at("Street_Unit_Number").asString());
                if (addr.has("Street_Address_City")) {
                    OWLNamedIndividual cityInd = individual(addr, "Street_Address_City");
                    OWLLiteral city = dataProperty(cityInd, "Name");
                    if (city == null)
                        city = dataProperty(cityInd, "Alias");
                    if (city != null)
                        actorAddr.append(", ").append(city.getLiteral());
                }
                if (addr.has("fullAddress") && addr.has("Street_Address_State")) {
                    OWLNamedIndividual stateInd = individual(addr, "Street_Address_State");
                    OWLLiteral state = dataProperty(stateInd, "USPS_Abbreviation");
                    if (state != null)
                        actorAddr.append(", ").append(state.getLiteral());
                }
                if (addr.has("Zip_Code"))
                    actorAddr.append(" - ").append(addr.at("Zip_Code").asString());
            } else
                actorAddr.append(blankField);

            String eMail = actor.has("hasEmailAddress") ? actor.at("hasEmailAddress").isObject()
                    ? actor.at("hasEmailAddress").at("iri").asString().split(":")[1]
                    : actor.at("hasEmailAddress").asString().split(":")[1] : blankField;

            addCell(cell, table, actorType, Element.ALIGN_LEFT, normalFont, color);
            addCell(cell, table, actorName.toString(), Element.ALIGN_LEFT, normalFont, color);
            addCell(cell, table, actorAddr.toString(), Element.ALIGN_LEFT, normalFont, color);
            addCell(cell, table, eMail, Element.ALIGN_LEFT, normalFont, color);

            boolean isFirstContactNo = true;
            String actorHmPh = actor.has("HomePhoneNumber") ? actor.at("HomePhoneNumber").asString() : null;
            String actorCellNo = actor.has("CellPhoneNumber") ? actor.at("CellPhoneNumber").asString() : null;
            String actorBizNo = actor.has("BusinessPhoneNumber") ? actor.at("BusinessPhoneNumber").asString()
                    : null;
            String actorFaxNo = actor.has("FaxNumber") ? actor.at("FaxNumber").asString() : null;
            String actorOtherNo = actor.has("OtherPhoneNumber") ? actor.at("OtherPhoneNumber").asString()
                    : null;

            if (actorHmPh != null)
                isFirstContactNo = addActorsContactNumbers(table, cell, actorHmPh, "Home ", iActorColumns,
                        color, isFirstContactNo);
            if (actorCellNo != null)
                isFirstContactNo = addActorsContactNumbers(table, cell, actorCellNo, "Cell ", iActorColumns,
                        color, isFirstContactNo);
            if (actorBizNo != null)
                isFirstContactNo = addActorsContactNumbers(table, cell, actorBizNo, "Biz ", iActorColumns,
                        color, isFirstContactNo);
            if (actorFaxNo != null)
                isFirstContactNo = addActorsContactNumbers(table, cell, actorFaxNo, "Fax ", iActorColumns,
                        color, isFirstContactNo);
            if (actorOtherNo != null)
                isFirstContactNo = addActorsContactNumbers(table, cell, actorOtherNo, "Other ", iActorColumns,
                        color, isFirstContactNo);
            if (actorHmPh == null && actorCellNo == null && actorBizNo == null && actorFaxNo == null
                    && actorOtherNo == null) {
                addCell(cell, table, blankField, Element.ALIGN_LEFT, normalFont, color);
                addCell(cell, table, blankField, Element.ALIGN_LEFT, normalFont, color);
            }
        }
        subCatPart.add(table);
        subCatPart.add(new Chunk(new LineSeparator()));
    }
}

From source file:org.sharegov.cirm.utils.PDFViewReport.java

License:Apache License

private void addActivities(Section subCatPart, Json data) throws DocumentException {
    if (data.at("properties").has("hasServiceActivity")) {
        if (!data.at("properties").at("hasServiceActivity").isArray()) {
            data.at("properties").set("hasServiceActivity",
                    Json.array().add(data.at("properties").at("hasServiceActivity")));
        }//w ww .j  a  v a2s  .  c  o  m
        Json filteredActivities = Json.array();
        //Do not add any StatusChangeActivity Activities to the report
        for (Json act : data.at("properties").at("hasServiceActivity").asJsonList()) {
            OWLNamedIndividual actInd = individual(act, "hasActivity");
            if (!actInd.getIRI().getFragment().equals("StatusChangeActivity"))
                filteredActivities.add(act);
        }

        if (filteredActivities.asJsonList().size() > 0) {
            //PdfPTable table = new PdfPTable(7);
            PdfPTable table = new PdfPTable(6);
            table.setWidthPercentage(100);
            //int[] widths = {17, 13, 12, 12, 13, 13, 20};
            int[] widths = { 25, 20, 13, 12, 13, 17 };
            table.setWidths(widths);
            PdfPCell cell = null;

            addCell(cell, table, "Activity", Element.ALIGN_LEFT, bigBoldFont, null);
            addCell(cell, table, "Assigned To", Element.ALIGN_LEFT, bigBoldFont, null);
            addCell(cell, table, "Created Date", Element.ALIGN_LEFT, bigBoldFont, null);
            addCell(cell, table, "Due Date", Element.ALIGN_LEFT, bigBoldFont, null);
            addCell(cell, table, "Completed Date", Element.ALIGN_LEFT, bigBoldFont, null);
            addCell(cell, table, "Outcome", Element.ALIGN_LEFT, bigBoldFont, null);
            //addCell(cell, table, "Details", Element.ALIGN_LEFT, bigBoldFont);

            subCatPart.add(table);
            //subCatPart.add(Chunk.NEWLINE);
            //subCatPart.add(new Chunk(new LineSeparator()));

            //table = new PdfPTable(7);
            //table = new PdfPTable(6);
            //table.setWidthPercentage(100);
            //table.setWidths(widths);

            //Store all outcome iris(key), labels(value).
            //Because in case of duplicate outcomes all outcome values (except the first) will be a string and not object
            Map<String, String> outcomeMap = new HashMap<String, String>(10);

            //for (int k = 0; k < data.at("properties").at("hasServiceActivity").asJsonList().size(); k++)
            for (int k = 0; k < filteredActivities.asJsonList().size(); k++) {
                Json activity = filteredActivities.at(k);
                BaseColor color = addColor(k);
                OWLIndividual act = individual(activity, "hasActivity");
                OWLLiteral typeLabel = null;
                for (OWLAnnotation ann : OWL.annotations(act.asOWLNamedIndividual())) {
                    if (ann.getProperty().isLabel())
                        typeLabel = (OWLLiteral) ann.getValue();
                }

                String activityType = typeLabel == null ? "" : typeLabel.getLiteral();
                String assignedTo = activity.has("isAssignedTo") ? activity.at("isAssignedTo").asString()
                        : blankField;
                String createdDate = activity.has("hasDateCreated") ? activity.at("hasDateCreated").asString()
                        : blankField;
                String dueDate = activity.has("hasDueDate") ? activity.at("hasDueDate").asString() : blankField;
                String completedDate = activity.has("hasCompletedTimestamp")
                        ? activity.at("hasCompletedTimestamp").asString()
                        : blankField;
                String details = activity.has("hasDetails") ? activity.at("hasDetails").asString() : blankField;
                StringBuilder outcome = new StringBuilder("");
                if (activity.has("hasOutcome")) {
                    if (activity.at("hasOutcome").isObject()) {
                        if (!outcomeMap.containsKey(activity.at("hasOutcome").at("iri").asString())) {
                            outcomeMap.put(activity.at("hasOutcome").at("iri").asString(),
                                    activity.at("hasOutcome").at("label").asString());
                            outcome.append(activity.at("hasOutcome").at("label").asString());
                        }
                    } else if (activity.at("hasOutcome").isString()) {
                        if (outcomeMap.containsKey(activity.at("hasOutcome").asString()))
                            outcome.append(outcomeMap.get(activity.at("hasOutcome").asString()));
                        else
                            outcome.append(OWL.fullIri(activity.at("hasOutcome").asString()).getFragment());
                    }
                }

                //table = new PdfPTable(7);
                table = new PdfPTable(6);
                table.setWidthPercentage(100);
                table.setWidths(widths);

                addCell(cell, table, activityType, Element.ALIGN_LEFT, normalFont, color);
                addCell(cell, table, getEmployeeName(assignedTo), Element.ALIGN_LEFT, normalFont, color);
                addCell(cell, table, formatDate(createdDate, false, false), Element.ALIGN_LEFT, normalFont,
                        color);
                addCell(cell, table, formatDate(dueDate, false, false), Element.ALIGN_LEFT, normalFont, color);
                addCell(cell, table, formatDate(completedDate, true, false), Element.ALIGN_LEFT, normalFont,
                        color);
                addCell(cell, table, outcome.toString(), Element.ALIGN_LEFT, normalFont, color);
                subCatPart.add(table);
                if (!details.equals(blankField)) {
                    table = new PdfPTable(2);
                    table.setWidthPercentage(100);
                    int[] detailsWitdh = { 10, 90 };
                    table.setWidths(detailsWitdh);
                    addCell(cell, table, "Details: ", Element.ALIGN_LEFT, normalFont, color);
                    addCell(cell, table, details, Element.ALIGN_LEFT, normalFont, color);
                    subCatPart.add(table);
                }
            }
            //subCatPart.add(table);
            subCatPart.add(new Chunk(new LineSeparator()));
        }
    }
}

From source file:org.sharegov.cirm.utils.PDFViewReport.java

License:Apache License

private void addWASDHeader(Chapter chapter) throws DocumentException {
    try {//from   w  w w  .java  2 s.  c o m
        String img = StartUp.config.at("workingDir").asString() + "/src/html/images/md-logo.png";
        Image logo = Image.getInstance(img);
        logo.scalePercent(67);

        PdfPTable table = new PdfPTable(2);
        table.setWidthPercentage(100);
        int[] widths = { 50, 50 };
        table.setWidths(widths);
        //table.addCell(logo); //This exactly fits the image to the size of the cell.
        PdfPCell cell = new PdfPCell(logo);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        String str1 = "Miami-Dade Water and Sewer Department";
        String str2 = "P.O.Box 33016 . 3071 SW 38 Ave";
        String str3 = "Miami, Florida 33146";
        String str4 = "T: 786-552-8974";
        String str5 = "www.miamidade.gov";
        PdfPTable wasdTable = new PdfPTable(1);
        addCell(wasdTable, str1, Element.ALIGN_RIGHT, boldFont, null);
        addCell(wasdTable, str2, Element.ALIGN_RIGHT, normalFont, null);
        addCell(wasdTable, str3, Element.ALIGN_RIGHT, normalFont, null);
        addCell(wasdTable, str4, Element.ALIGN_RIGHT, normalFont, null);
        addCell(wasdTable, str5, Element.ALIGN_RIGHT, boldFont, null);
        cell = new PdfPCell(wasdTable);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        addSection(chapter).add(table);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage());
    }
}