Example usage for org.apache.pdfbox.pdmodel.font PDType1Font TIMES_ROMAN

List of usage examples for org.apache.pdfbox.pdmodel.font PDType1Font TIMES_ROMAN

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.font PDType1Font TIMES_ROMAN.

Prototype

PDType1Font TIMES_ROMAN

To view the source code for org.apache.pdfbox.pdmodel.font PDType1Font TIMES_ROMAN.

Click Source Link

Usage

From source file:au.org.alfred.icu.pdf.services.factories.ICUDischargeSummaryFactory.java

public static String createContent(PDDocument pdf, String icuVisitNumber, String visitNumber, String patientId,
        String userName, String userId) {
    InputStream is = null;// w w  w . ja v  a 2 s.  c o m
    int yCursor = 137;
    int temp = yCursor;

    DischargeSummaryData data = new DischargeSummaryData();
    PDPage pg_content;
    PDPageContentStream cs;
    String pdfName = DischargeSummaryDataBuilder.buildData(icuVisitNumber, visitNumber, patientId, userName,
            userId, data);
    try {

        pg_content = new PDPage(new PDRectangle(pageWidth, pageHeight));
        cs = new PDPageContentStream(pdf, pg_content);
        yCursor = createNewPage(pdf, pg_content, cs, yCursor, data);
        Paragraph para = new Paragraph(leftMargin + 5, pageHeight - yCursor - 15, data.getFinalDiagnosis());
        para.withColor(Color.BLACK);
        para.withFont(PDType1Font.TIMES_ROMAN, 12);
        para.withWidth(500);
        temp += para.getParaHeight();

        cs.beginText();
        cs.setFont(PDType1Font.TIMES_BOLD, 14);
        cs.moveTextPositionByAmount(leftMargin, pageHeight - yCursor);
        cs.setNonStrokingColor(darkGreen);
        cs.drawString("Final Diagnosis");
        cs.endText();

        yCursor += para.getFontHeight() * para.getLines().size();

        if (yCursor > (pageHeight - footerHeight)) {
            pg_content = newPageBuilder(pdf, pg_content, cs, data, yCursor);
            cs = new PDPageContentStream(pdf, pg_content);
            yCursor = createNewPage(pdf, pg_content, cs, yCursor, data);
        }

        write(cs, para);

        yCursor += 25;
        para = new Paragraph(leftMargin + 5, pageHeight - yCursor - 15, data.getPresentingHx());
        para.withColor(Color.BLACK);
        para.withFont(PDType1Font.TIMES_ROMAN, 12);
        para.withWidth(500);
        temp = yCursor;
        temp += para.getParaHeight();
        if (temp > (pageHeight - footerHeight)) {
            pg_content = newPageBuilder(pdf, pg_content, cs, data, yCursor);
            cs = new PDPageContentStream(pdf, pg_content);
            yCursor = createNewPage(pdf, pg_content, cs, yCursor, data);
            para = new Paragraph(leftMargin + 5, pageHeight - yCursor - 15, data.getPresentingHx());
            para.withColor(Color.BLACK);
            para.withFont(PDType1Font.TIMES_ROMAN, 12);
            para.withWidth(500);

        }

        cs.beginText();
        cs.setFont(PDType1Font.TIMES_BOLD, 14);
        cs.moveTextPositionByAmount(leftMargin, pageHeight - yCursor);
        cs.setNonStrokingColor(darkGreen);
        cs.drawString("Presenting History");
        cs.endText();
        yCursor += para.getFontHeight() * para.getLines().size();
        write(cs, para);

        yCursor += 25;
        para = new Paragraph(leftMargin + 5, pageHeight - yCursor - 15, data.getPastHx());
        para.withColor(Color.BLACK);
        para.withFont(PDType1Font.TIMES_ROMAN, 12);
        para.withWidth(500);
        temp = yCursor;
        temp += para.getParaHeight();
        if (temp > (pageHeight - footerHeight)) {
            pg_content = newPageBuilder(pdf, pg_content, cs, data, yCursor);
            cs = new PDPageContentStream(pdf, pg_content);
            yCursor = createNewPage(pdf, pg_content, cs, yCursor, data);
            para = new Paragraph(leftMargin + 5, pageHeight - yCursor - 15, data.getPastHx());
            para.withColor(Color.BLACK);
            para.withFont(PDType1Font.TIMES_ROMAN, 12);
            para.withWidth(500);

        }

        cs.beginText();
        cs.setFont(PDType1Font.TIMES_BOLD, 14);
        cs.moveTextPositionByAmount(leftMargin, pageHeight - yCursor);
        cs.setNonStrokingColor(darkGreen);
        cs.drawString("Past History");
        cs.endText();
        yCursor += para.getFontHeight() * para.getLines().size();
        write(cs, para);

        yCursor += 25;
        para = new Paragraph(leftMargin + 5, pageHeight - yCursor - 15, data.getDisSumm());
        para.withColor(Color.BLACK);
        para.withFont(PDType1Font.TIMES_ROMAN, 12);
        para.withWidth(500);
        temp = yCursor;
        temp += para.getParaHeight();
        if (temp > (pageHeight - footerHeight)) {
            List<String> pages = new ArrayList<String>();
            int hght = para.getParaHeight();
            System.out.println("height " + hght);
            if (hght > pageHeight - footerHeight) {
                while (hght > pageHeight - footerHeight) {
                    String div = data.getDisSumm().substring(0, data.getDisSumm().length() / 2);
                    Paragraph p = new Paragraph(leftMargin + 5, pageHeight - 147 - 15, div);
                    hght = p.getParaHeight();
                    pages.add(div);
                }
            }
            System.out.println("Pages: " + pages.size());
            if (pages.size() > 1) {
                Iterator itr = pages.iterator();
                while (itr.hasNext()) {
                    String inner = (String) itr.next();
                    pg_content = newPageBuilder(pdf, pg_content, cs, data, yCursor);
                    cs = new PDPageContentStream(pdf, pg_content);
                    yCursor = createNewPage(pdf, pg_content, cs, yCursor, data);
                    para = new Paragraph(leftMargin + 5, pageHeight - yCursor - 15, inner);
                    para.withColor(Color.BLACK);
                    para.withFont(PDType1Font.TIMES_ROMAN, 12);
                    para.withWidth(500);
                    cs.beginText();
                    cs.setFont(PDType1Font.TIMES_BOLD, 14);
                    cs.moveTextPositionByAmount(leftMargin, pageHeight - yCursor);
                    cs.setNonStrokingColor(darkGreen);
                    cs.drawString("ICU Stay Summary");
                    cs.endText();
                    yCursor += para.getFontHeight() * para.getLines().size();
                    write(cs, para);
                }
            } else {
                pg_content = newPageBuilder(pdf, pg_content, cs, data, yCursor);
                cs = new PDPageContentStream(pdf, pg_content);
                yCursor = createNewPage(pdf, pg_content, cs, yCursor, data);
                para = new Paragraph(leftMargin + 5, pageHeight - yCursor - 15, data.getDisSumm());
                para.withColor(Color.BLACK);
                para.withFont(PDType1Font.TIMES_ROMAN, 12);
                para.withWidth(500);
            }

        }

        cs.beginText();
        cs.setFont(PDType1Font.TIMES_BOLD, 14);
        cs.moveTextPositionByAmount(leftMargin, pageHeight - yCursor);
        cs.setNonStrokingColor(darkGreen);
        cs.drawString("ICU Stay Summary");
        cs.endText();
        yCursor += para.getFontHeight() * para.getLines().size();
        write(cs, para);

        yCursor += 25;
        para = new Paragraph(leftMargin + 5, pageHeight - yCursor - 15, data.getPendingInvestigations());
        para.withColor(Color.BLACK);
        para.withFont(PDType1Font.TIMES_ROMAN, 12);
        para.withWidth(500);
        temp = yCursor;
        temp += para.getParaHeight();
        if (temp > (pageHeight - footerHeight)) {
            pg_content = newPageBuilder(pdf, pg_content, cs, data, yCursor);
            cs = new PDPageContentStream(pdf, pg_content);
            yCursor = createNewPage(pdf, pg_content, cs, yCursor, data);
            para = new Paragraph(leftMargin + 5, pageHeight - yCursor - 15, data.getPendingInvestigations());
            para.withColor(Color.BLACK);
            para.withFont(PDType1Font.TIMES_ROMAN, 12);
            para.withWidth(500);

        }

        cs.beginText();
        cs.setFont(PDType1Font.TIMES_BOLD, 14);
        cs.moveTextPositionByAmount(leftMargin, pageHeight - yCursor);
        cs.setNonStrokingColor(darkGreen);
        cs.drawString("Current Pending Investigations");
        cs.endText();
        yCursor += para.getFontHeight() * para.getLines().size();
        write(cs, para);

        yCursor += 25;

        if (yCursor > (pageHeight - footerHeight)) {
            pg_content = newPageBuilder(pdf, pg_content, cs, data, yCursor);
            cs = new PDPageContentStream(pdf, pg_content);
            yCursor = createNewPage(pdf, pg_content, cs, yCursor, data);
        }

        if (data.getMicro() != null) {
            int k = 0;
            String[][] table = new String[data.getMicro().size() + 1][6];
            table[k][0] = "Date";
            table[k][1] = "Site";
            table[k][2] = "Organism";
            table[k][3] = "Sensitivity";
            table[k][4] = "";
            table[k][5] = "";
            k++;
            for (Iterator<TblMicroResults> i = data.getMicro().iterator(); i.hasNext();) {
                TblMicroResults m = i.next();
                table[k][0] = m.getMTime() == null ? "N/A"
                        : new SimpleDateFormat("dd/MM/yy").format(m.getMTime());
                table[k][1] = m.getMSite() == null ? "N/A" : m.getMSite();
                table[k][2] = m.getMOrg() == null ? "N/A" : m.getMOrg();
                table[k][3] = m.getMSens() == null ? "N/A" : m.getMSens();
                table[k][4] = "";
                table[k][5] = "";
                k++;
            }

            //System.out.println(yCursor);
            temp = getTableHeight(pg_content, cs, pageHeight - yCursor, leftMargin, table,
                    PDType1Font.TIMES_BOLD, PDType1Font.TIMES_ROMAN, 10);
            if ((yCursor + temp + 30) > (pageHeight - footerHeight)) {
                pg_content = newPageBuilder(pdf, pg_content, cs, data, yCursor);
                cs = new PDPageContentStream(pdf, pg_content);
                yCursor = createNewPage(pdf, pg_content, cs, yCursor, data);
            }
            yCursor += 25;
            cs.beginText();
            cs.setFont(PDType1Font.TIMES_BOLD, 14);
            cs.moveTextPositionByAmount(leftMargin, pageHeight - yCursor);
            cs.setNonStrokingColor(darkGreen);
            cs.drawString("Microbiology Results");
            cs.endText();

            yCursor += 10;

            yCursor += drawTable(pg_content, cs, pageHeight - yCursor, leftMargin, table,
                    PDType1Font.TIMES_BOLD, PDType1Font.TIMES_ROMAN, 10);

        }

        if (data.getOther() != null) {
            int k = 0;
            String[][] table = new String[data.getOther().size() + 1][6];
            table[k][0] = "Date";
            table[k][1] = "Investigation";
            table[k][2] = "Results";
            table[k][3] = "";
            table[k][4] = "";
            table[k][5] = "";
            k++;
            for (Iterator<TblOtherResults> i = data.getOther().iterator(); i.hasNext();) {
                TblOtherResults m = i.next();
                table[k][0] = m.getOTime() == null ? "N/A"
                        : new SimpleDateFormat("dd/MM/yy").format(m.getOTime());
                table[k][1] = m.getOInv() == null ? "N/A" : m.getOInv();
                table[k][2] = m.getOResult() == null ? "N/A" : m.getOResult();
                table[k][3] = "";
                table[k][4] = "";
                table[k][5] = "";
                k++;
            }

            yCursor += 10;
            temp = getTableHeight(pg_content, cs, pageHeight - yCursor, leftMargin, table,
                    PDType1Font.TIMES_BOLD, PDType1Font.TIMES_ROMAN, 10);
            if ((yCursor + temp + 30) > (pageHeight - footerHeight)) {
                pg_content = newPageBuilder(pdf, pg_content, cs, data, yCursor);
                cs = new PDPageContentStream(pdf, pg_content);
                yCursor = createNewPage(pdf, pg_content, cs, yCursor, data);
            }
            yCursor += 25;

            cs.beginText();
            cs.setFont(PDType1Font.TIMES_BOLD, 14);
            cs.moveTextPositionByAmount(leftMargin, pageHeight - yCursor);
            cs.setNonStrokingColor(darkGreen);
            cs.drawString("Other Significant Results");
            cs.endText();
            yCursor += 10;
            yCursor += drawTable(pg_content, cs, pageHeight - yCursor, leftMargin, table,
                    PDType1Font.TIMES_BOLD, PDType1Font.TIMES_ROMAN, 10);

        }

        if (data.getAntibiotics() != null) {
            int k = 0;
            String[][] table = new String[data.getAntibiotics().size() + 1][6];
            table[k][0] = "Start Date";
            table[k][1] = "Suggested End Date";
            table[k][2] = "Antibiotic";
            table[k][3] = "Indication";
            table[k][4] = "";
            table[k][5] = "";

            k++;
            for (Iterator<TblAntibiotic> i = data.getAntibiotics().iterator(); i.hasNext();) {
                TblAntibiotic m = i.next();
                table[k][0] = m.getAStart() == null ? "N/A"
                        : new SimpleDateFormat("dd/MM/yy").format(m.getAStart());
                table[k][1] = m.getAEnd() == null ? "N/A" : m.getAEnd();
                table[k][2] = m.getAAntibiotic() == null ? "N/A" : m.getAAntibiotic();
                table[k][3] = m.getAIndication() == null ? "N/A" : m.getAIndication();
                table[k][4] = "";
                table[k][5] = "";
                k++;
            }

            temp = getTableHeight(pg_content, cs, pageHeight - yCursor, leftMargin, table,
                    PDType1Font.TIMES_BOLD, PDType1Font.TIMES_ROMAN, 10);
            if ((yCursor + temp + 30) > (pageHeight - footerHeight)) {
                pg_content = newPageBuilder(pdf, pg_content, cs, data, yCursor);
                cs = new PDPageContentStream(pdf, pg_content);
                yCursor = createNewPage(pdf, pg_content, cs, yCursor, data);
            }
            yCursor += 25;

            cs.beginText();
            cs.setFont(PDType1Font.TIMES_BOLD, 14);
            cs.moveTextPositionByAmount(leftMargin, pageHeight - yCursor);
            cs.setNonStrokingColor(darkGreen);
            cs.drawString("Current Antibiotic Plans");
            cs.endText();
            yCursor += 10;
            yCursor += drawTable(pg_content, cs, pageHeight - yCursor, leftMargin, table,
                    PDType1Font.TIMES_BOLD, PDType1Font.TIMES_ROMAN, 10);

        }

        if (data.getNeuro() != null) {
            int k = 0;
            String[][] table = new String[data.getNeuro().size() + 1][7];
            table[k][0] = "GCS-E";
            table[k][1] = "GCS-V";
            table[k][2] = "GCS-M";
            table[k][3] = "Upper Motor";
            table[k][4] = "Upper Sensation";
            table[k][5] = "Lower Motor";
            table[k][6] = "Lower Sensation";

            k++;
            for (TblNeurology m : data.getNeuro()) {
                table[k][0] = m.getNGcsE() != null ? m.getNGcsE() : "N/A";
                table[k][1] = m.getNGcsV() != null ? m.getNGcsV() : "N/A";
                table[k][2] = m.getNGcsM() != null ? m.getNGcsM() : "N/A";
                table[k][3] = m.getNUpperMotor() != null ? m.getNUpperMotor() : "N/A";
                table[k][4] = m.getNUpperSens() != null ? m.getNUpperSens() : "N/A";
                table[k][5] = m.getNLowerMotor() != null ? m.getNLowerMotor() : "N/A";
                table[k][6] = m.getNLowerSense() != null ? m.getNLowerSense() : "N/A";
                k++;
            }

            //System.out.println(table[1][1]);

            temp = getTableHeight(pg_content, cs, pageHeight - yCursor, leftMargin, table,
                    PDType1Font.TIMES_BOLD, PDType1Font.TIMES_ROMAN, 10);
            if ((yCursor + temp + 30) > (pageHeight - footerHeight)) {
                pg_content = newPageBuilder(pdf, pg_content, cs, data, yCursor);
                cs = new PDPageContentStream(pdf, pg_content);
                yCursor = createNewPage(pdf, pg_content, cs, yCursor, data);
            }
            yCursor += 25;

            cs.beginText();
            cs.setFont(PDType1Font.TIMES_BOLD, 14);
            cs.moveTextPositionByAmount(leftMargin, pageHeight - yCursor);
            cs.setNonStrokingColor(darkGreen);
            cs.drawString("Current Neurological Exam");
            cs.endText();
            yCursor += 10;
            yCursor += drawTable(pg_content, cs, pageHeight - yCursor, leftMargin, table,
                    PDType1Font.TIMES_BOLD, PDType1Font.TIMES_ROMAN, 10);

        }

        if (data.getProcs() != null) {
            int k = 0;
            String[][] table = new String[data.getProcs().size() + 1][6];
            table[k][0] = "Start Date";
            table[k][1] = "System";
            table[k][2] = "Intervention";
            table[k][3] = "Type";
            table[k][4] = "Site";
            table[k][5] = "Side";
            k++;
            for (Iterator<VwSiss> i = data.getProcs().iterator(); i.hasNext();) {
                VwSiss m = i.next();
                if (m.getSissEndDate() == null) {
                    table[k][0] = new SimpleDateFormat("dd/MM/yy").format(m.getSissStartDate());
                    table[k][1] = m.getSissSystemDesc() == null ? "-" : m.getSissSystemDesc();
                    table[k][2] = m.getSissInterventionDesc() == null ? "-" : m.getSissInterventionDesc();
                    table[k][3] = m.getSissTypeDesc() == null ? "-" : m.getSissTypeDesc();
                    table[k][4] = m.getSissSiteDesc() == null ? "-" : m.getSissSiteDesc();
                    table[k][5] = m.getSissSideDesc() == null ? "-" : m.getSissSideDesc();
                    //System.out.println(table[k][2]);
                    k++;
                }
            }

            temp = getTableHeight(pg_content, cs, pageHeight - yCursor, leftMargin, table,
                    PDType1Font.TIMES_BOLD, PDType1Font.TIMES_ROMAN, 10);
            if ((yCursor + temp + 30) > (pageHeight - footerHeight)) {
                pg_content = newPageBuilder(pdf, pg_content, cs, data, yCursor);
                cs = new PDPageContentStream(pdf, pg_content);
                yCursor = createNewPage(pdf, pg_content, cs, yCursor, data);
            }

            yCursor += 25;
            cs.beginText();
            cs.setFont(PDType1Font.TIMES_BOLD, 14);
            cs.moveTextPositionByAmount(leftMargin, pageHeight - yCursor);
            cs.setNonStrokingColor(darkGreen);
            cs.drawString("Current In-Situ Devices");
            cs.endText();

            yCursor += 10;

            yCursor += drawTable(pg_content, cs, pageHeight - yCursor, leftMargin, table,
                    PDType1Font.TIMES_BOLD, PDType1Font.TIMES_ROMAN, 10);

        }

        yCursor += 25;
        para = new Paragraph(leftMargin + 5, pageHeight - yCursor - 15, data.getActiveProblems());
        para.withColor(Color.BLACK);
        para.withFont(PDType1Font.TIMES_ROMAN, 12);
        para.withWidth(500);
        temp = yCursor;
        temp += para.getParaHeight();
        if (temp > (pageHeight - footerHeight)) {
            pg_content = newPageBuilder(pdf, pg_content, cs, data, yCursor);
            cs = new PDPageContentStream(pdf, pg_content);
            yCursor = createNewPage(pdf, pg_content, cs, yCursor, data);
            para = new Paragraph(leftMargin + 5, pageHeight - yCursor - 15, data.getActiveProblems());
            para.withColor(Color.BLACK);
            para.withFont(PDType1Font.TIMES_ROMAN, 12);
            para.withWidth(500);

        }

        cs.beginText();
        cs.setFont(PDType1Font.TIMES_BOLD, 14);
        cs.moveTextPositionByAmount(leftMargin, pageHeight - yCursor);
        cs.setNonStrokingColor(darkGreen);
        cs.drawString("Ongoing Issues & Plan");
        cs.endText();
        yCursor += para.getFontHeight() * para.getLines().size();
        write(cs, para);

        // Add Nursing Plan
        pg_content = newPageBuilder(pdf, pg_content, cs, data, yCursor);
        cs = new PDPageContentStream(pdf, pg_content);
        yCursor = createNewPage(pdf, pg_content, cs, yCursor, data);
        cs.beginText();
        cs.setFont(PDType1Font.TIMES_BOLD, 14);
        cs.moveTextPositionByAmount(leftMargin, pageHeight - yCursor);
        cs.setNonStrokingColor(darkGreen);
        cs.drawString("Nursing Plan");
        cs.endText();

        Iterator itr = data.getNursingPlans().keySet().iterator();
        while (itr.hasNext()) {
            String heading = (String) itr.next();
            //System.out.println(heading);
            List<TblCarePlan> plans = (List<TblCarePlan>) data.getNursingPlans().get(heading);
            if (plans.size() > 0) {
                TblCarePlan plan = plans.get(0);
                yCursor += 25;
                para = new Paragraph(leftMargin + 5, pageHeight - yCursor - 15, plan.getCarePlanNote());
                para.withColor(Color.BLACK);
                para.withFont(PDType1Font.TIMES_ROMAN, 12);
                para.withWidth(500);
                temp = yCursor;
                temp += para.getParaHeight();
                if (temp > (pageHeight - footerHeight)) {
                    pg_content = newPageBuilder(pdf, pg_content, cs, data, yCursor);
                    cs = new PDPageContentStream(pdf, pg_content);
                    yCursor = createNewPage(pdf, pg_content, cs, yCursor, data);
                    para = new Paragraph(leftMargin + 5, pageHeight - yCursor - 15, plan.getCarePlanNote());
                    para.withColor(Color.BLACK);
                    para.withFont(PDType1Font.TIMES_ROMAN, 12);
                    para.withWidth(500);

                }

                cs.beginText();
                cs.setFont(PDType1Font.TIMES_BOLD, 14);
                cs.moveTextPositionByAmount(leftMargin, pageHeight - yCursor);
                cs.setNonStrokingColor(darkGreen);
                cs.drawString(heading + " [" + DateFormat.getInstance().format(plan.getUpdateDate()) + "]");
                cs.endText();
                yCursor += para.getFontHeight() * para.getLines().size();
                write(cs, para);
            }

        }
        cs.close();
        pdf.addPage(pg_content);
    } catch (IOException ex) {
        java.util.logging.Logger.getLogger(ICUDischargeSummaryFactory.class.getName()).log(Level.SEVERE, null,
                ex);
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(ICUDischargeSummaryFactory.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
    }

    return pdfName;

}

From source file:au.org.alfred.icu.pdf.services.factories.ICUDischargeSummaryFactory.java

public static void createPageTemplate(PDPageContentStream cs, int leftMargin, int pageHeight, int pageWidth,
        DischargeSummaryData data) throws IOException {

    //cs.addRect(0, 0, 595, 127);
    cs.setNonStrokingColor(171, 221, 171);
    cs.fillRect(leftMargin, pageHeight - 127, pageWidth - leftMargin, 127);
    cs.setNonStrokingColor(255, 255, 255);
    cs.fillRect(345 + leftMargin, pageHeight - 120, 210, 110);

    cs.setNonStrokingColor(Color.BLACK);
    cs.beginText();//w  w  w.jav  a2  s  . c  o m
    cs.setFont(PDType1Font.TIMES_BOLD, 14);
    cs.moveTextPositionByAmount(430 + leftMargin + 10, pageHeight - 22);
    cs.drawString("MRN: " + data.getPatientId());
    cs.endText();
    cs.beginText();
    cs.setFont(PDType1Font.TIMES_ROMAN, 8);
    cs.moveTextPositionByAmount(350 + leftMargin, pageHeight - 25);
    cs.drawString("Visit: " + data.getVisitNumber());
    cs.endText();
    cs.beginText();
    cs.setFont(PDType1Font.TIMES_BOLD, 12);
    cs.moveTextPositionByAmount(350 + leftMargin, pageHeight - 50);
    cs.drawString(data.getPatientFamilyName());
    cs.endText();
    cs.beginText();
    cs.moveTextPositionByAmount(350 + leftMargin, pageHeight - 63);
    cs.drawString(data.getPatientGivenName() + " " + (data.getPatientMiddleName()));
    cs.endText();
    cs.beginText();
    cs.setFont(PDType1Font.TIMES_ROMAN, 8);
    cs.moveTextPositionByAmount(350 + leftMargin, pageHeight - 75);
    cs.drawString(new String().concat("DOB: ").concat(data.getBirthDate()).concat("      ")
            .concat(data.getAge()).concat("yr"));
    cs.endText();
    cs.beginText();
    cs.moveTextPositionByAmount(350 + leftMargin, pageHeight - 85);
    cs.drawString(data.getSexDesc().toUpperCase());
    cs.endText();
    cs.beginText();
    cs.moveTextPositionByAmount(350 + leftMargin, pageHeight - 97);
    cs.drawString(data.getPatientAddress());
    cs.endText();
    cs.beginText();
    cs.moveTextPositionByAmount(350 + leftMargin, pageHeight - 107);
    cs.drawString(data.getUnitCode());
    cs.endText();
    cs.beginText();
    cs.moveTextPositionByAmount(350 + leftMargin, pageHeight - 117);
    cs.drawString(new String().concat("ADM: ").concat(data.getAdmDate()).concat("          F/C: ")
            .concat(data.getPayClass().toUpperCase()));
    cs.endText();
    cs.beginText();
    cs.setFont(PDType1Font.TIMES_BOLD, 10);
    cs.setNonStrokingColor(0, 153, 51);
    cs.moveTextPositionByAmount(5 + leftMargin, pageHeight - 25);
    cs.drawString("THE ALFRED");
    cs.endText();
    cs.beginText();
    cs.setFont(PDType1Font.TIMES_BOLD, 16);
    cs.moveTextPositionByAmount(5 + leftMargin, pageHeight - 87);
    cs.drawString("ICU DISCHARGE SUMMARY");
    cs.endText();
    if (data.getDisSumm() == null || data.getDisSumm().isEmpty() || data.getDisSumm().length() < 2) {
        cs.beginText();
        cs.setNonStrokingColor(Color.BLACK);
        cs.setFont(PDType1Font.TIMES_BOLD, 18);
        cs.moveTextPositionByAmount(5 + leftMargin, pageHeight - 107);
        cs.drawString("DISCHARGE SUMMARY INCOMPLETE");
        cs.endText();

    }
    cs.setNonStrokingColor(171, 221, 171);
    cs.fillRect(leftMargin, 0, 595, 42);
    cs.beginText();
    cs.setNonStrokingColor(0, 153, 51);
    cs.setFont(PDType1Font.TIMES_ROMAN, 10);
    cs.moveTextPositionByAmount(5 + leftMargin, pageHeight - 835);
    cs.drawString(new String().concat("This summary was generated ").concat(data.getUpdateDate()));
    cs.endText();
}

From source file:au.org.alfred.icu.pdf.services.factories.ICUDischargeSummaryFactory.java

private static int createNewPage(PDDocument pdf, PDPage pg_content, PDPageContentStream cs, int yCursor,
        DischargeSummaryData data) throws IOException {
    yCursor = 147;/* w  ww. j a  va2s . c om*/
    int temp = 0;
    createPageTemplate(cs, leftMargin, pageHeight, pageWidth, data);
    if (pdf.getNumberOfPages() == 0) {

        drawBox(cs, leftMargin, pageHeight - (yCursor + 51), 530, 72, 1, darkGreen, Color.WHITE);

        cs.beginText();
        cs.setFont(PDType1Font.TIMES_BOLD, 10);
        cs.setNonStrokingColor(darkGreen);
        cs.moveTextPositionByAmount(5 + leftMargin, pageHeight - yCursor - 1);
        cs.drawString("ICU Admission Date");
        cs.endText();
        //cs.addRect(leftMargin+5, pageHeight-(yCursor+14), 100, 14);
        drawBoxAndText(cs, leftMargin + 5, pageHeight - (yCursor + 17), 100, 14, 1, darkGreen, Color.WHITE,
                Color.BLACK, data.getWardAdmDate(), PDType1Font.TIMES_ROMAN, 10);

        cs.beginText();
        cs.setFont(PDType1Font.TIMES_BOLD, 10);
        cs.setNonStrokingColor(darkGreen);
        cs.moveTextPositionByAmount(110 + leftMargin, pageHeight - yCursor - 1);
        cs.drawString("ICU Discharge Date");
        cs.endText();
        //cs.addRect(leftMargin+5, pageHeight-(yCursor+14), 100, 14);
        drawBoxAndText(cs, leftMargin + 110, pageHeight - (yCursor + 17), 100, 14, 1, darkGreen, Color.WHITE,
                Color.BLACK, data.getWardExitDate(), PDType1Font.TIMES_ROMAN, 10);

        cs.beginText();
        cs.setFont(PDType1Font.TIMES_BOLD, 10);
        cs.setNonStrokingColor(darkGreen);
        cs.moveTextPositionByAmount(leftMargin + 215, pageHeight - yCursor - 1);
        cs.drawString("ICU length of stay");
        cs.endText();
        //cs.addRect(leftMargin+5, pageHeight-(yCursor+14), 100, 14);
        drawBoxAndText(cs, leftMargin + 215, pageHeight - (yCursor + 17), 100, 14, 1, darkGreen, Color.WHITE,
                Color.BLACK, data.getLOSHours().concat(" hours (").concat(data.getLOSDays()).concat(" days)"),
                PDType1Font.TIMES_ROMAN, 10);

        cs.beginText();
        cs.setFont(PDType1Font.TIMES_BOLD, 10);
        cs.setNonStrokingColor(darkGreen);
        cs.moveTextPositionByAmount(320 + leftMargin, pageHeight - yCursor - 1);
        cs.drawString("Delayed Discharge");
        cs.endText();
        //cs.addRect(leftMargin+5, pageHeight-(yCursor+14), 100, 14);
        drawBoxAndText(cs, leftMargin + 320, pageHeight - (yCursor + 17), 80, 14, 1, darkGreen, Color.WHITE,
                Color.BLACK, data.getDelayed(), PDType1Font.TIMES_ROMAN, 10);

        cs.beginText();
        cs.setFont(PDType1Font.TIMES_BOLD, 10);
        cs.setNonStrokingColor(darkGreen);
        cs.moveTextPositionByAmount(405 + leftMargin, pageHeight - yCursor - 1);
        cs.drawString("Early Discharge");
        cs.endText();
        //cs.addRect(leftMargin+5, pageHeight-(yCursor+14), 100, 14);
        drawBoxAndText(cs, leftMargin + 405, pageHeight - (yCursor + 17), 80, 14, 1, darkGreen, Color.WHITE,
                Color.BLACK, data.getEarlyDischarge(), PDType1Font.TIMES_ROMAN, 10);
        cs.beginText();
        cs.setFont(PDType1Font.TIMES_BOLD, 10);
        cs.setNonStrokingColor(darkGreen);
        cs.moveTextPositionByAmount(490 + leftMargin, pageHeight - yCursor - 1);
        cs.drawString("Adm #");
        cs.endText();
        //cs.addRect(leftMargin+5, pageHeight-(yCursor+14), 100, 14);
        drawBoxAndText(cs, leftMargin + 490, pageHeight - (yCursor + 17), 30, 14, 1, darkGreen, Color.WHITE,
                Color.BLACK, data.getAdmNumber(), PDType1Font.TIMES_ROMAN, 10);

        yCursor += 34;
        cs.beginText();
        cs.setFont(PDType1Font.TIMES_BOLD, 10);
        cs.setNonStrokingColor(darkGreen);
        cs.moveTextPositionByAmount(5 + leftMargin, pageHeight - yCursor - 1 + 7);
        cs.drawString("APACHE III-J Diagnosis");
        cs.endText();
        drawBoxAndText(cs, leftMargin + 120, pageHeight - yCursor - 1, 400, 15, 1, darkGreen, Color.WHITE,
                Color.BLACK, data.getApacheIII(), PDType1Font.TIMES_ROMAN, 10);

        if (data.getCurrentLocation() != null) {
            yCursor += 15;
            cs.beginText();
            cs.setFont(PDType1Font.TIMES_BOLD, 10);
            cs.setNonStrokingColor(darkGreen);
            cs.moveTextPositionByAmount(5 + leftMargin, pageHeight - yCursor - 1 + 7);
            cs.drawString("Destination");
            cs.endText();

            drawBoxAndText(cs, leftMargin + 120, pageHeight - yCursor - 1, 400, 15, 1, darkGreen, Color.WHITE,
                    Color.BLACK, data.getCurrentLocation().toUpperCase(), PDType1Font.TIMES_ROMAN, 10);
        }

        yCursor += 15;
        if (data.getResus() != null) {
            int k = 0;
            String[][] table = new String[data.getResus().size() + 1][6];
            table[k][0] = "For CPR";
            table[k][1] = "For Intubation";
            table[k][2] = "For Defib";
            table[k][3] = "For ICU Readmit";
            table[k][4] = "Modified MET Criteria?";
            table[k][5] = "";
            k++;
            for (TblResusPlan m : data.getResus()) {
                table[k][0] = m.getRCpr() ? "Yes" : "No";
                table[k][1] = m.getRIntub() ? "Yes" : "No";
                table[k][2] = m.getRDefib() ? "Yes" : "No";
                table[k][3] = m.getRReadmit() ? "Yes" : "No";
                table[k][4] = "".equals(m.getRMetReason()) ? "No" : m.getRMetReason();
                table[k][5] = "";
                k++;
            }

            //System.out.println(yCursor);
            temp = getTableHeight(pg_content, cs, pageHeight - yCursor, leftMargin, table,
                    PDType1Font.TIMES_BOLD, PDType1Font.TIMES_ROMAN, 10);
            if ((yCursor + temp + 30) > (pageHeight - footerHeight)) {
                pg_content = newPageBuilder(pdf, pg_content, cs, data, yCursor);
                cs = new PDPageContentStream(pdf, pg_content);
                yCursor = createNewPage(pdf, pg_content, cs, yCursor, data);
            }

            yCursor += 10;

            yCursor += drawTable(pg_content, cs, pageHeight - yCursor, leftMargin, table,
                    PDType1Font.TIMES_BOLD, PDType1Font.TIMES_ROMAN, 10);

            yCursor += 20;
        }

        if (data.getNic() != null) {
            int k = 0;
            String[][] table = new String[data.getNic().size() + 1][6];
            table[k][0] = "Assessed Nicotine Dependent";
            table[k][1] = "Management Offered";
            table[k][2] = "Assessment Date";
            table[k][3] = "Pharmacist";
            table[k][4] = "";
            table[k][5] = "";
            k++;
            for (TblNicotine m : data.getNic()) {
                table[k][0] = m.getNicDependent() == null ? "N/A" : (m.getNicDependent() == 1 ? "Yes" : "No");
                table[k][1] = m.getNicMgmOffered() == null ? "N/A" : (m.getNicMgmOffered() == 1 ? "Yes" : "No");
                table[k][2] = m.getNicDateAssess() == null ? "N/A"
                        : new SimpleDateFormat("dd/MM/yy").format(m.getNicDateAssess());
                table[k][3] = m.getNicPharmacist() == null ? "N/A" : m.getNicPharmacist();
                table[k][4] = "";
                table[k][5] = "";
                k++;
            }

            //System.out.println(yCursor);
            temp = getTableHeight(pg_content, cs, pageHeight - yCursor, leftMargin, table,
                    PDType1Font.TIMES_BOLD, PDType1Font.TIMES_ROMAN, 10);
            if ((yCursor + temp + 30) > (pageHeight - footerHeight)) {
                pg_content = newPageBuilder(pdf, pg_content, cs, data, yCursor);
                cs = new PDPageContentStream(pdf, pg_content);
                yCursor = createNewPage(pdf, pg_content, cs, yCursor, data);
            }

            yCursor += 10;

            yCursor += drawTable(pg_content, cs, pageHeight - yCursor, leftMargin, table,
                    PDType1Font.TIMES_BOLD, PDType1Font.TIMES_ROMAN, 10);

            yCursor += 20;
        }

    }

    return yCursor;

}

From source file:Clavis.Windows.WShedule.java

/**
 * @see//  w  w  w.j a  va  2s . com
 * http://fahdshariff.blogspot.pt/2010/10/creating-tables-with-pdfbox.html
 */
private static void drawTable(PDDocument doc, String[][] content, String titulo, String subtitulo,
        String subsubtitulo) throws IOException {

    float y = 680f;
    float margin = 60f;
    final int rows = content.length;
    final int cols = content[0].length;
    final float rowHeight = 20f;
    int maximolinhas = (int) (y / (rowHeight + 2));

    float tableHeight;
    int paginas = 0;
    int linhas;

    if (rows < maximolinhas) {
        linhas = rows;
        paginas++;
    } else {
        linhas = maximolinhas;
        int auxiliar = 0;
        while (auxiliar < rows) {
            paginas++;
            auxiliar += maximolinhas;
        }
    }
    final float cellMargin = 5f;
    float tamanhotexto = 12f;
    float tamanhotexto2 = 8f;
    float dimensao = 0f;
    float dimensao2 = 0f;
    PDFont font = PDType1Font.TIMES_ROMAN;
    for (String[] content1 : content) {
        dimensao = font.getStringWidth(content1[0]) / 1000 * tamanhotexto2;
        if (dimensao > 222.0f) {
            String nome = content1[0];
            String[] texto = nome.split(" ");
            nome = nome.replace(texto[0], "");
            nome = nome.replace(texto[texto.length - 1], "");
            int i = 1;
            while (dimensao > 222.0f) {
                if (texto[i].length() > 2) {
                    nome = nome.replace(texto[i], texto[i].charAt(0) + ".");
                } else {
                    nome = nome.replace(texto[i], "");
                }
                dimensao = font.getStringWidth(texto[0] + nome + texto[texto.length - 1]) / 1000
                        * tamanhotexto2;
                i++;
            }
            content1[0] = texto[0] + nome + texto[texto.length - 1];
        }
        dimensao2 = font.getStringWidth(content1[3]) / 1000 * tamanhotexto2 + 10;
        if (dimensao2 > 180.0f) {
            String nome = content1[3];
            String[] texto = nome.split(" ");
            nome = nome.replace(texto[0], "");
            nome = nome.replace(texto[texto.length - 1], "");
            int i = 1;
            while (dimensao2 > 180.0f) {
                if (texto[i].length() > 2) {
                    nome = nome.replace(texto[i], texto[i].charAt(0) + ".");
                } else {
                    nome = nome.replace(texto[i], "");
                }
                dimensao2 = font.getStringWidth(texto[0] + nome + texto[texto.length - 1]) / 1000
                        * tamanhotexto2;
                i++;
            }
            content1[3] = texto[0] + nome + texto[texto.length - 1];
        }
    }
    int passagem = 0;
    int passagemdepagina = 0;
    float tableWidth;
    if (dimensao < 100) {
        dimensao = 100;
    }
    if (dimensao2 < 100) {
        dimensao2 = 100;
    }
    float firstcolWidth = dimensao + 10 * cellMargin;
    float lastcolWidth = dimensao2 + 10 * cellMargin;
    float colWidth;
    PDPageContentStream contentStream = null;
    while (passagem < paginas) {
        PDPage page = new PDPage();
        doc.addPage(page);
        try {
            contentStream = new PDPageContentStream(doc, page);
        } catch (IOException ex) {
            Logger.getLogger(WShedule.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (contentStream != null) {
            contentStream.setFont(font, tamanhotexto);
            tableWidth = page.getMediaBox().getWidth() - (2 * margin);
            colWidth = (tableWidth - firstcolWidth - lastcolWidth) / (float) (cols - 2);
            if (passagem == 0) {
                contentStream.beginText();
                float posicao = margin + (tableWidth / 2)
                        - ((font.getStringWidth(titulo) / 1000 * tamanhotexto) / 2);
                contentStream.newLineAtOffset(posicao, page.getMediaBox().getHeight() - 40);
                contentStream.showText(titulo);
                contentStream.endText();
                contentStream.beginText();
                posicao = margin + (tableWidth / 2)
                        - ((font.getStringWidth(subtitulo) / 1000 * tamanhotexto) / 2);
                contentStream.newLineAtOffset(posicao, page.getMediaBox().getHeight() - 60);
                contentStream.showText(subtitulo);
                contentStream.endText();
                contentStream.beginText();
                posicao = margin + (tableWidth / 2)
                        - ((font.getStringWidth(subsubtitulo) / 1000 * tamanhotexto) / 2);
                contentStream.newLineAtOffset(posicao, page.getMediaBox().getHeight() - 80);
                contentStream.showText(subsubtitulo);
                contentStream.endText();
            }
            if (passagem == 1) {
                y += 40;
            }
            float nexty = y;
            contentStream.setFont(font, tamanhotexto2);

            for (int i = 0; i <= linhas; i++) {
                if (i < linhas) {
                    if ((i % 2) == 0) {
                        contentStream.setNonStrokingColor(200, 200, 200);
                        contentStream.addRect(margin, nexty - rowHeight, tableWidth, rowHeight);
                        contentStream.fill();
                    }
                }
                contentStream.setNonStrokingColor(0, 0, 0);
                contentStream.moveTo(margin, nexty);
                contentStream.lineTo(margin + tableWidth, nexty);
                contentStream.stroke();
                nexty -= rowHeight;
            }

            //draw the columns
            float nextx = margin;
            for (int i = 0; i <= cols; i++) {
                contentStream.moveTo(nextx, y);
                if (linhas < maximolinhas) {
                    tableHeight = rowHeight * linhas;
                } else {
                    tableHeight = rowHeight * maximolinhas;
                }
                contentStream.lineTo(nextx, y - tableHeight);
                contentStream.stroke();
                switch (i) {
                case 0:
                    nextx += firstcolWidth;
                    break;
                case 3:
                    nextx += lastcolWidth;
                    break;
                default:
                    nextx += colWidth;
                    break;
                }
            }

            float textx = margin;
            float texty = y - 15;
            float ttexto;
            boolean primeira = true;
            for (int i = 0; i < linhas; i++) {
                for (int j = 0; j < content[i + passagemdepagina].length; j++) {
                    String text = content[i + passagemdepagina][j];
                    ttexto = font.getStringWidth(text) / 1000 * tamanhotexto2;
                    if (j == 0) {
                        ttexto = textx + ((firstcolWidth / 2) - (ttexto / 2));
                    } else if (j < 3) {
                        ttexto = textx + ((colWidth / 2) - (ttexto / 2));
                    } else {
                        ttexto = textx + ((lastcolWidth / 2) - (ttexto / 2));
                    }
                    contentStream.beginText();
                    contentStream.newLineAtOffset(ttexto, texty);
                    contentStream.showText(text);
                    contentStream.endText();
                    if (j == 0) {
                        textx += firstcolWidth;
                    } else {
                        textx += colWidth;
                    }
                }
                texty -= rowHeight;
                textx = margin;
            }
            contentStream.beginText();
            contentStream.newLineAtOffset((tableWidth / 2) + margin, 40);
            contentStream.showText("" + (passagem + 1));
            contentStream.endText();
            contentStream.close();
        }
        passagem++;
        maximolinhas = (int) (y / (rowHeight + 1));
        linhas = rows - (maximolinhas * passagem);
        if (linhas > maximolinhas) {
            linhas = maximolinhas;
        }
        passagemdepagina = maximolinhas * passagem;
    }

}

From source file:com.baseprogramming.pdwriter.html.HtmlStyle.java

License:Apache License

private void createFontMap() {
    fontMap = new HashMap<>();
    fontMap.put("TIMES NEW ROMAN", PDType1Font.TIMES_ROMAN);
    fontMap.put("TIMES NEW ROMAN_BOLD", PDType1Font.TIMES_BOLD);
    fontMap.put("TIMES NEW ROMAN_ITALIC", PDType1Font.TIMES_ITALIC);
    fontMap.put("TIMES NEW ROMAN_OBLIQUE", PDType1Font.TIMES_ITALIC);
    fontMap.put("TIMES NEW ROMAN_BOLD_ITALIC", PDType1Font.TIMES_BOLD_ITALIC);

    fontMap.put("COURIER", PDType1Font.COURIER);
    fontMap.put("COURIER_BOLD", PDType1Font.COURIER_BOLD);
    fontMap.put("COURIER_ITALIC", PDType1Font.COURIER_OBLIQUE);
    fontMap.put("COURIER_OBLIQUE", PDType1Font.COURIER_OBLIQUE);
    fontMap.put("COURIER_BOLD_ITALIC", PDType1Font.COURIER_BOLD_OBLIQUE);

    fontMap.put("HELVATICA", PDType1Font.TIMES_ROMAN);
    fontMap.put("HELVATICA_ITALIC", PDType1Font.HELVETICA_BOLD);
    fontMap.put("HELVATICA_OBLIQUE", PDType1Font.HELVETICA_BOLD);
    fontMap.put("HELVATICA_BOLD_ITALIC", PDType1Font.HELVETICA_OBLIQUE);
    fontMap.put("HELVATICA_BOLD_ITALIC", PDType1Font.HELVETICA_BOLD_OBLIQUE);
}

From source file:com.baseprogramming.pdwriter.html.HtmlStyle.java

License:Apache License

private PDType1Font getPDType1Font(String htmlFamily, String style, String weight) {
    String key = htmlFamily;/*www. j a v  a 2 s . c  o  m*/

    if (!(weight == null || "normal".equals(weight))) {
        key += "_" + weight;
    }

    if (!(style == null || "normal".equals(style))) {
        key += "_" + style;
    }

    return fontMap.getOrDefault(key.toUpperCase(), PDType1Font.TIMES_ROMAN);

}

From source file:com.fileOperations.EmailBodyToPDF.java

/**
 * Places the text of the email into the PDF
 *
 * @param eml EmailBodyPDF/* w ww .  java  2  s  .  c  o m*/
 */
private static void createEmailBody(EmailBodyPDF eml) {
    PDDocument doc = null;
    PDPageContentStream contentStream = null;

    //Fonts used
    PDFont bodyTitleFont = PDType1Font.TIMES_BOLD;
    PDFont bodyFont = PDType1Font.TIMES_ROMAN;

    //Font Sizes
    float emailHeaderFontSize = 7;
    float leadingEmailHeader = 1.5f * emailHeaderFontSize;
    float bodyFontSize = 12;
    float leadingBody = 1.5f * bodyFontSize;

    try {
        //Create Document, Page, Margins.
        doc = new PDDocument();
        PDPage page = new PDPage();
        doc.addPage(page);
        contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false);
        PDRectangle mediabox = page.getMediaBox();
        float margin = 72;
        float width = mediabox.getWidth() - 2 * margin;
        float startX = mediabox.getLowerLeftX() + margin;
        float startY = mediabox.getUpperRightY() - margin;
        float textYlocation = margin;

        //Set Line Breaks
        List<String> sentDateContent = PDFBoxTools.setLineBreaks(eml.getSentDate(), width, emailHeaderFontSize,
                bodyFont);
        List<String> recievedDateContent = PDFBoxTools.setLineBreaks(eml.getReceiveDate(), width,
                emailHeaderFontSize, bodyFont);
        List<String> toContent = PDFBoxTools.setLineBreaks(eml.getTo(), width, emailHeaderFontSize, bodyFont);
        List<String> fromContent = PDFBoxTools.setLineBreaks(eml.getFrom(), width, emailHeaderFontSize,
                bodyFont);
        List<String> ccContent = PDFBoxTools.setLineBreaks(eml.getCc(), width, emailHeaderFontSize, bodyFont);
        List<String> bccContent = PDFBoxTools.setLineBreaks(eml.getBcc(), width, emailHeaderFontSize, bodyFont);
        List<String> attachmentContent = PDFBoxTools.setLineBreaks(eml.getAttachments(), width,
                emailHeaderFontSize, bodyFont);
        List<String> subjectContent = PDFBoxTools.setLineBreaks(eml.getSubject(), width, bodyFontSize,
                bodyFont);
        List<String> bodyContent = PDFBoxTools.setLineBreaks(eml.getBody(), width, bodyFontSize, bodyFont);

        //Set Email Header
        contentStream.beginText();
        contentStream.setFont(bodyFont, emailHeaderFontSize);
        contentStream.setNonStrokingColor(Color.BLACK);
        contentStream.newLineAtOffset(startX, startY);

        //Set Date Sent
        if (!"".equals(eml.getSentDate())) {
            contentStream.setFont(bodyTitleFont, emailHeaderFontSize);
            contentStream.showText("Date Sent: ");
            contentStream.setFont(bodyFont, emailHeaderFontSize);
            contentStream.newLineAtOffset(0, -leadingEmailHeader);
            textYlocation += leadingEmailHeader;
            for (String line : sentDateContent) {
                if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingEmailHeader)) {
                    contentStream.endText();
                    contentStream.close();
                    textYlocation = 0;

                    page = new PDPage();
                    doc.addPage(page);
                    contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false);

                    contentStream.beginText();
                    contentStream.setFont(bodyFont, emailHeaderFontSize);
                    contentStream.setNonStrokingColor(Color.BLACK);
                    contentStream.newLineAtOffset(startX, startY);
                }

                contentStream.showText(line);
                contentStream.newLineAtOffset(0, -leadingEmailHeader);
                textYlocation += leadingEmailHeader;
            }
        }

        //Set Date Received
        if (!"".equals(eml.getReceiveDate().trim())) {
            contentStream.setFont(bodyTitleFont, emailHeaderFontSize);
            contentStream.showText("Date Received: ");
            contentStream.setFont(bodyFont, emailHeaderFontSize);
            contentStream.newLineAtOffset(0, -leadingEmailHeader);
            textYlocation += leadingEmailHeader;
            for (String line : recievedDateContent) {
                if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingEmailHeader)) {
                    contentStream.endText();
                    contentStream.close();
                    textYlocation = 0;

                    page = new PDPage();
                    doc.addPage(page);
                    contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false);

                    contentStream.beginText();
                    contentStream.setFont(bodyFont, emailHeaderFontSize);
                    contentStream.setNonStrokingColor(Color.BLACK);
                    contentStream.newLineAtOffset(startX, startY);
                }

                contentStream.showText(line);
                contentStream.newLineAtOffset(0, -leadingEmailHeader);
                textYlocation += leadingBody;
            }
        }
        contentStream.newLineAtOffset(0, -leadingBody);

        //Set From
        if (!"".equals(eml.getFrom().trim())) {
            contentStream.setFont(bodyTitleFont, emailHeaderFontSize);
            contentStream.showText("From: ");
            contentStream.setFont(bodyFont, emailHeaderFontSize);
            contentStream.newLineAtOffset(0, -leadingEmailHeader);
            textYlocation += leadingEmailHeader;
            for (String line : fromContent) {
                if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingEmailHeader)) {
                    contentStream.endText();
                    contentStream.close();
                    textYlocation = 0;

                    page = new PDPage();
                    doc.addPage(page);
                    contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false);

                    contentStream.beginText();
                    contentStream.setFont(bodyFont, emailHeaderFontSize);
                    contentStream.setNonStrokingColor(Color.BLACK);
                    contentStream.newLineAtOffset(startX, startY);
                }
                contentStream.showText(line);
                contentStream.newLineAtOffset(0, -leadingEmailHeader);
                textYlocation += leadingEmailHeader;
            }
        }

        //Set To
        if (!"".equals(eml.getTo().trim())) {
            contentStream.setFont(bodyTitleFont, emailHeaderFontSize);
            contentStream.showText("To: ");
            contentStream.setFont(bodyFont, emailHeaderFontSize);
            contentStream.newLineAtOffset(0, -leadingEmailHeader);
            textYlocation += leadingEmailHeader;
            for (String line : toContent) {
                if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingEmailHeader)) {
                    contentStream.endText();
                    contentStream.close();
                    textYlocation = 0;

                    page = new PDPage();
                    doc.addPage(page);
                    contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false);

                    contentStream.beginText();
                    contentStream.setFont(bodyFont, emailHeaderFontSize);
                    contentStream.setNonStrokingColor(Color.BLACK);
                    contentStream.newLineAtOffset(startX, startY);
                }
                contentStream.showText(line);
                contentStream.newLineAtOffset(0, -leadingEmailHeader);
                textYlocation += leadingEmailHeader;
            }
        }

        //Set CC
        if (!"".equals(eml.getCc().trim())) {
            contentStream.setFont(bodyTitleFont, emailHeaderFontSize);
            contentStream.showText("CC: ");
            contentStream.setFont(bodyFont, emailHeaderFontSize);
            contentStream.newLineAtOffset(0, -leadingEmailHeader);
            textYlocation += leadingEmailHeader;
            for (String line : ccContent) {
                if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingEmailHeader)) {
                    contentStream.endText();
                    contentStream.close();
                    textYlocation = 0;

                    page = new PDPage();
                    doc.addPage(page);
                    contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false);

                    contentStream.beginText();
                    contentStream.setFont(bodyFont, emailHeaderFontSize);
                    contentStream.setNonStrokingColor(Color.BLACK);
                    contentStream.newLineAtOffset(startX, startY);
                }
                contentStream.showText(line);
                contentStream.newLineAtOffset(0, -leadingEmailHeader);
                textYlocation += leadingEmailHeader;
            }
        }

        //Set BCC
        if (!"".equals(eml.getBcc().trim())) {
            contentStream.setFont(bodyTitleFont, emailHeaderFontSize);
            contentStream.showText("BCC: ");
            contentStream.setFont(bodyFont, emailHeaderFontSize);
            contentStream.newLineAtOffset(0, -leadingEmailHeader);
            textYlocation += leadingEmailHeader;
            for (String line : bccContent) {
                if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingEmailHeader)) {
                    contentStream.endText();
                    contentStream.close();
                    textYlocation = 0;

                    page = new PDPage();
                    doc.addPage(page);
                    contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false);

                    contentStream.beginText();
                    contentStream.setFont(bodyFont, emailHeaderFontSize);
                    contentStream.setNonStrokingColor(Color.BLACK);
                    contentStream.newLineAtOffset(startX, startY);
                }
                contentStream.showText(line);
                contentStream.newLineAtOffset(0, -leadingEmailHeader);
                textYlocation += leadingEmailHeader;
            }
        }

        //Set AttachmentList
        if (!"".equals(eml.getAttachments().trim())) {
            contentStream.setFont(bodyTitleFont, emailHeaderFontSize);
            contentStream.showText("Attachments: ");
            contentStream.setFont(bodyFont, emailHeaderFontSize);
            contentStream.newLineAtOffset(0, -leadingEmailHeader);
            textYlocation += leadingEmailHeader;
            for (String line : attachmentContent) {
                if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingEmailHeader)) {
                    contentStream.endText();
                    contentStream.close();
                    textYlocation = 0;

                    page = new PDPage();
                    doc.addPage(page);
                    contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false);

                    contentStream.beginText();
                    contentStream.setFont(bodyFont, emailHeaderFontSize);
                    contentStream.setNonStrokingColor(Color.BLACK);
                    contentStream.newLineAtOffset(startX, startY);
                }
                contentStream.showText(line);
                contentStream.newLineAtOffset(0, -leadingEmailHeader);
                textYlocation += leadingEmailHeader;
            }
        }

        //Set Subject
        if (!"".equals(eml.getSubject().trim())) {
            contentStream.newLineAtOffset(0, -leadingBody);
            contentStream.newLineAtOffset(0, -leadingBody);
            contentStream.setFont(bodyTitleFont, bodyFontSize);
            contentStream.showText("Subject: ");
            contentStream.newLineAtOffset(0, -leadingBody);
            textYlocation += leadingBody;
            contentStream.setFont(bodyFont, bodyFontSize);
            for (String line : subjectContent) {
                if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingEmailHeader)) {
                    contentStream.endText();
                    contentStream.close();
                    textYlocation = 0;

                    page = new PDPage();
                    doc.addPage(page);
                    contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false);

                    contentStream.beginText();
                    contentStream.setFont(bodyFont, emailHeaderFontSize);
                    contentStream.setNonStrokingColor(Color.BLACK);
                    contentStream.newLineAtOffset(startX, startY);
                }
                contentStream.showText(line);
                contentStream.newLineAtOffset(0, -leadingBody);
                textYlocation += leadingBody;
            }
        }
        if (!"".equals(eml.getBody().trim())) {
            // Set Email Body
            contentStream.newLineAtOffset(0, -leadingBody);
            contentStream.setFont(bodyTitleFont, bodyFontSize);
            contentStream.showText("Message: ");
            contentStream.setFont(bodyFont, bodyFontSize);
            contentStream.newLineAtOffset(0, -leadingBody);
            for (String line : bodyContent) {
                if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingBody)) {
                    contentStream.endText();
                    contentStream.close();
                    textYlocation = 0;

                    page = new PDPage();
                    doc.addPage(page);
                    contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false);

                    contentStream.beginText();
                    contentStream.setFont(bodyFont, bodyFontSize);
                    contentStream.setNonStrokingColor(Color.BLACK);
                    contentStream.newLineAtOffset(startX, startY);
                }

                textYlocation += leadingBody;

                contentStream.showText(line);
                contentStream.newLineAtOffset(0, -leadingBody);
            }
            contentStream.endText();

        }
        contentStream.close();
        doc.save(eml.getFilePath() + eml.getFileName());
    } catch (IOException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        if (doc != null) {
            try {
                doc.close();
            } catch (IOException ex) {
                ExceptionHandler.Handle(ex);
            }
        }
    }
}

From source file:com.fileOperations.StampPDF.java

/**
 * This stamps docketed files.//w w w  .  ja  va 2  s . co m
 *
 * @param file String (full file path)
 * @param docketTime Timestamp
 * @param dept
 */
public static void stampDocument(String file, Timestamp docketTime, String dept) {
    // the document
    PDDocument doc = null;
    try {
        PDFont stampFont = PDType1Font.TIMES_ROMAN;
        float stampFontSize = 14;
        String title = PDFBoxTools.HeaderTimeStamp(docketTime) + " " + dept;
        float titleWidth = stampFont.getStringWidth(title) / 1000 * stampFontSize;
        float titleHeight = stampFont.getFontDescriptor().getFontBoundingBox().getHeight() / 1000
                * stampFontSize;
        int marginTop = 20;

        doc = PDDocument.load(new File(file));

        if (!doc.isEncrypted()) {
            for (int i = 0; i < doc.getPages().getCount(); i++) {
                PDPageContentStream contentStream = null;

                PDPage page = (PDPage) doc.getPages().get(i);

                contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true);
                page.getResources().getFontNames();

                contentStream.beginText();
                contentStream.setFont(stampFont, stampFontSize);
                contentStream.setNonStrokingColor(Color.RED);
                contentStream.newLineAtOffset((page.getMediaBox().getWidth() - titleWidth) / 2,
                        page.getMediaBox().getHeight() - marginTop - titleHeight);
                contentStream.showText(title);
                contentStream.endText();

                contentStream.close();
            }
            doc.save(file);
        }
    } catch (IOException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        if (doc != null) {
            try {
                doc.close();
            } catch (IOException ex) {
                ExceptionHandler.Handle(ex);
            }
        }
    }
}

From source file:com.fileOperations.TXTtoPDF.java

/**
 * Takes the text from the string and insert it into the PDF file
 *
 * @param pdfFile String (path + filename)
 * @param text String (text from document)
 *///from   www .  ja  v a 2 s.c o m
private static void makePDF(String pdfFile, String text) {
    PDDocument doc = null;
    PDPageContentStream contentStream = null;

    //Fonts used
    PDFont bodyFont = PDType1Font.TIMES_ROMAN;

    //Font Sizes
    float bodyFontSize = 12;
    float leadingBody = 1.5f * bodyFontSize;

    try {
        //Create Document, Page, Margins.
        doc = new PDDocument();
        PDPage page = new PDPage();
        doc.addPage(page);
        contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false);
        PDRectangle mediabox = page.getMediaBox();
        float margin = 72;
        float width = mediabox.getWidth() - 2 * margin;
        float startX = mediabox.getLowerLeftX() + margin;
        float startY = mediabox.getUpperRightY() - margin;
        float textYlocation = margin;

        //Set Line Breaks
        text = text.replaceAll("[\\p{C}\\p{Z}]", System.getProperty("line.separator")); //strip ZERO WIDTH SPACE
        List<String> textContent = PDFBoxTools.setLineBreaks(text, width, bodyFontSize, bodyFont);

        contentStream.beginText();
        contentStream.setFont(bodyFont, bodyFontSize);
        contentStream.setNonStrokingColor(Color.BLACK);
        contentStream.newLineAtOffset(startX, startY);

        if (!"".equals(text)) {
            for (String line : textContent) {
                if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingBody)) {
                    contentStream.endText();
                    contentStream.close();
                    textYlocation = 0;

                    page = new PDPage();
                    doc.addPage(page);
                    contentStream = new PDPageContentStream(doc, page, true, true, false);

                    contentStream.beginText();
                    contentStream.setFont(bodyFont, bodyFontSize);
                    contentStream.setNonStrokingColor(Color.BLACK);
                    contentStream.newLineAtOffset(startX, startY);
                }

                textYlocation += leadingBody;

                contentStream.showText(line);
                contentStream.newLineAtOffset(0, -leadingBody);
            }
            contentStream.endText();

        }
        contentStream.close();
        doc.save(pdfFile);
    } catch (IOException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        if (doc != null) {
            try {
                doc.close();
            } catch (IOException ex) {
                ExceptionHandler.Handle(ex);
            }
        }
    }
}

From source file:de.fau.amos4.util.ZipGenerator.java

License:Open Source License

public void generate(OutputStream out, Locale locale, float height, Employee employee, int fontSize,
        String zipPassword) throws ZipException, NoSuchMessageException, IOException, COSVisitorException,
        CloneNotSupportedException {
    final ZipOutputStream zout = new ZipOutputStream(out);

    if (zipPassword == null) {
        // Use default password if none is set.
        zipPassword = "fragebogen";
    }//w w w  .j  av  a2s.c om

    ZipParameters params = new ZipParameters();
    params.setFileNameInZip("employee.txt");
    params.setCompressionLevel(Zip4jConstants.COMP_DEFLATE);
    params.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_ULTRA);
    params.setEncryptFiles(true);
    params.setReadHiddenFiles(false);
    params.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
    params.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
    params.setPassword(zipPassword);
    params.setSourceExternalStream(true);

    zout.putNextEntry(null, params);
    zout.write((AppContext.getApplicationContext().getMessage("HEADER", null, locale) + "\n\n").getBytes());

    zout.write(
            (AppContext.getApplicationContext().getMessage("print.section.personalData", null, locale) + "\n\n")
                    .getBytes());

    Iterator it = employee.getPersonalDataFields().entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry) it.next();
        zout.write((pair.getKey() + ": " + pair.getValue() + '\n').getBytes());
        it.remove(); // avoids a ConcurrentModificationException
    }

    zout.write(("\n\n" + AppContext.getApplicationContext().getMessage("print.section.taxes", null, locale)
            + "\n\n").getBytes());

    it = employee.getTaxesFields().entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry) it.next();
        zout.write((pair.getKey() + ": " + pair.getValue() + '\n').getBytes());
        it.remove(); // avoids a ConcurrentModificationException
    }
    zout.closeEntry();

    // Create a document and add a page to it
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    float y = -1;
    int margin = 100;

    // Create a new font object selecting one of the PDF base fonts
    PDFont font = PDType1Font.TIMES_ROMAN;

    // Start a new content stream which will "hold" the to be created content
    PDPageContentStream contentStream = new PDPageContentStream(document, page);

    // Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
    contentStream.beginText();

    y = page.getMediaBox().getHeight() - margin + height;
    contentStream.moveTextPositionByAmount(margin, y);
    /*
    List<String> list = StringUtils.splitEqually(fileContent, 90);
    for (String e : list) {
        contentStream.moveTextPositionByAmount(0, -15);
        contentStream.drawString(e);
    }
    */

    contentStream.setFont(PDType1Font.TIMES_BOLD, 36);
    contentStream.drawString(AppContext.getApplicationContext().getMessage("HEADER", null, locale));
    contentStream.setFont(PDType1Font.TIMES_BOLD, 14);
    contentStream.moveTextPositionByAmount(0, -4 * height);
    contentStream.drawString(
            AppContext.getApplicationContext().getMessage("print.section.personalData", null, locale));
    contentStream.moveTextPositionByAmount(0, -2 * height);
    contentStream.setFont(font, fontSize);

    it = employee.getPersonalDataFields().entrySet().iterator();
    while (it.hasNext()) {
        StringBuffer nextLineToDraw = new StringBuffer();
        Map.Entry pair = (Map.Entry) it.next();
        nextLineToDraw.append(pair.getKey());
        nextLineToDraw.append(": ");
        nextLineToDraw.append(pair.getValue());

        contentStream.drawString(nextLineToDraw.toString());
        contentStream.moveTextPositionByAmount(0, -height);
        it.remove(); // avoids a ConcurrentModificationException
    }
    contentStream.setFont(PDType1Font.TIMES_BOLD, 14);
    contentStream.moveTextPositionByAmount(0, -2 * height);
    contentStream
            .drawString(AppContext.getApplicationContext().getMessage("print.section.taxes", null, locale));
    contentStream.moveTextPositionByAmount(0, -2 * height);
    contentStream.setFont(font, fontSize);
    it = employee.getTaxesFields().entrySet().iterator();
    while (it.hasNext()) {
        StringBuffer nextLineToDraw = new StringBuffer();
        Map.Entry pair = (Map.Entry) it.next();
        nextLineToDraw.append(pair.getKey());
        nextLineToDraw.append(": ");
        nextLineToDraw.append(pair.getValue());

        contentStream.drawString(nextLineToDraw.toString());
        contentStream.moveTextPositionByAmount(0, -height);
        it.remove(); // avoids a ConcurrentModificationException
    }
    contentStream.endText();

    // Make sure that the content stream is closed:
    contentStream.close();

    // Save the results and ensure that the document is properly closed:
    ByteArrayOutputStream pdfout = new ByteArrayOutputStream();
    document.save(pdfout);
    document.close();

    ZipParameters params2 = (ZipParameters) params.clone();
    params2.setFileNameInZip("employee.pdf");

    zout.putNextEntry(null, params2);
    zout.write(pdfout.toByteArray());
    zout.closeEntry();

    // Write the zip to client
    zout.finish();
    zout.flush();
    zout.close();
}