Example usage for org.apache.pdfbox.pdmodel PDDocument addPage

List of usage examples for org.apache.pdfbox.pdmodel PDDocument addPage

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDDocument addPage.

Prototype

public void addPage(PDPage page) 

Source Link

Document

This will add a page to the document.

Usage

From source file:at.medevit.elexis.impfplan.ui.handlers.PrintVaccinationEntriesHandler.java

License:Open Source License

private void createPDF(Patient patient, Image image) throws IOException, COSVisitorException {
    PDDocumentInformation pdi = new PDDocumentInformation();
    Mandant mandant = (Mandant) ElexisEventDispatcher.getSelected(Mandant.class);
    pdi.setAuthor(mandant.getName() + " " + mandant.getVorname());
    pdi.setCreationDate(new GregorianCalendar());
    pdi.setTitle("Impfausweis " + patient.getLabel());

    PDDocument document = new PDDocument();
    document.setDocumentInformation(pdi);

    PDPage page = new PDPage();
    page.setMediaBox(PDPage.PAGE_SIZE_A4);
    document.addPage(page);

    PDRectangle pageSize = page.findMediaBox();
    PDFont font = PDType1Font.HELVETICA_BOLD;

    PDFont subFont = PDType1Font.HELVETICA;

    PDPageContentStream contentStream = new PDPageContentStream(document, page);
    contentStream.beginText();//from w  w w.java 2  s . c  om
    contentStream.setFont(font, 14);
    contentStream.moveTextPositionByAmount(40, pageSize.getUpperRightY() - 40);
    contentStream.drawString(patient.getLabel());
    contentStream.endText();

    String dateLabel = sdf.format(Calendar.getInstance().getTime());
    String title = Person.load(mandant.getId()).get(Person.TITLE);
    String mandantLabel = title + " " + mandant.getName() + " " + mandant.getVorname();
    contentStream.beginText();
    contentStream.setFont(subFont, 10);
    contentStream.moveTextPositionByAmount(40, pageSize.getUpperRightY() - 55);
    contentStream.drawString("Ausstellung " + dateLabel + ", " + mandantLabel);
    contentStream.endText();

    BufferedImage imageAwt = convertToAWT(image.getImageData());

    PDXObjectImage pdPixelMap = new PDPixelMap(document, imageAwt);
    contentStream.drawXObject(pdPixelMap, 40, 30, pageSize.getWidth() - 80, pageSize.getHeight() - 100);
    contentStream.close();

    String outputPath = CoreHub.userCfg.get(PreferencePage.VAC_PDF_OUTPUTDIR,
            CoreHub.getWritableUserDir().getAbsolutePath());
    if (outputPath.equals(CoreHub.getWritableUserDir().getAbsolutePath())) {
        SWTHelper.showInfo("Kein Ausgabeverzeichnis definiert", "Ausgabe erfolgt in: " + outputPath
                + "\nDas Ausgabeverzeichnis kann unter Einstellungen\\Klinische Hilfsmittel\\Impfplan definiert werden.");
    }
    File outputDir = new File(outputPath);
    File pdf = new File(outputDir, "impfplan_" + patient.getPatCode() + ".pdf");
    document.save(pdf);
    document.close();
    Desktop.getDesktop().open(pdf);
}

From source file:at.oculus.teamf.technical.printing.Printer.java

License:Open Source License

/**
 * <h3>$print</h3>/*from  w w w.  j  a va2  s  . c o  m*/
 * <p/>
 * <b>Description:</b>
 * <p/>
 * This method prints the given parameters into a PDF-Document and opens the file with the standard program.
 * <p/>
 * <b>Parameter</b>
 *
 * @param title Is a string which will be printed as title in the PDF Document.
 * @param text Is a string which will be printed as message in the PDF Document.
 */
public void print(String title, String text) throws IOException, COSVisitorException {

    //creates a new PDDocument object
    PDDocument document = new PDDocument();
    //create a page to the document
    PDPage page1 = new PDPage(PDPage.PAGE_SIZE_A4);
    //rectangle is for sizes (height and width) of page
    PDRectangle rectangle = page1.getMediaBox();
    //add page to document
    document.addPage(page1);

    //fonts for the document are implemented here
    PDFont fontPlain = PDType1Font.HELVETICA;
    PDFont fontBold = PDType1Font.HELVETICA_BOLD;

    //running variable to calculate which line you are in the document right now
    int line = 0;

    try {
        //create the content stream which will write into the document
        PDPageContentStream stream = new PDPageContentStream(document, page1);

        //always use all these lines for entering new text into the document
        //move textToPosition will set the cursor to the current position
        stream.beginText();
        stream.setFont(fontBold, 14);
        stream.moveTextPositionByAmount(SPACING_LEFT, rectangle.getHeight() - SPACING_TOP);
        stream.drawString(title + ":");
        stream.endText();

        //calculates the correct place and then writes the whole text into the PDF-Document
        int start = 0;
        int end = text.length();
        while (start < end) {
            String tobePrinted;
            if ((end - start) > MAX_CHARACTERS_PER_LINE) {
                int tempEnd = start + MAX_CHARACTERS_PER_LINE;
                while (text.charAt(tempEnd) != ' ') {
                    ++tempEnd;
                }
                tobePrinted = text.substring(start, start = ++tempEnd);

            } else {
                tobePrinted = text.substring(start);
                start = start + MAX_CHARACTERS_PER_LINE;
            }
            stream.beginText();
            stream.setFont(fontPlain, 12);
            stream.moveTextPositionByAmount(SPACING_LEFT,
                    rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
            stream.drawString(tobePrinted);
            stream.endText();

        }
        //print oculus image into pdf file
        BufferedImage awtImage = ImageIO
                .read(new File("/home/oculus/IdeaProjects/Oculus/Technical/src/res/oculus.JPG"));
        PDXObjectImage ximage = new PDPixelMap(document, awtImage);
        float scale = 0.3f; // alter this value to set the image size
        stream.drawXObject(ximage, 380, 780, ximage.getWidth() * scale, ximage.getHeight() * scale);

        //close stream afterwards
        stream.close();

        //save document and close document
        document.save(
                "/home/oculus/IdeaProjects/Oculus/Technical/src/at/oculus/teamf/technical/printing/output_files/"
                        + title + ".pdf");
        document.close();
        //open document with standard application for the file
        Desktop.getDesktop().open(new File(
                "/home/oculus/IdeaProjects/Oculus/Technical/src/at/oculus/teamf/technical/printing/output_files/"
                        + title + ".pdf"));
    } catch (IOException | COSVisitorException e) {
        throw e;
    }
}

From source file:at.oculus.teamf.technical.printing.Printer.java

License:Open Source License

/**
 * <h3>$printPrescription</h3>
 * <p/>//from   w w w.j  a v  a2  s  .  c o  m
 * <b>Description:</b>
 * <p/>
 * This method prints the given parameters into a PDF-Document and opens the file with the standard program.
 * <p/>
 * <b>Parameter</b>
 *
 * @param iPrescription Is an Interface of a prescription from which all the information will be printed into
 *                      a PDF-File and then started with standard application from OS.
 */
public void printPrescription(IPrescription iPrescription, IDoctor iDoctor) throws COSVisitorException,
        IOException, CantGetPresciptionEntriesException, NoPrescriptionToPrintException {
    if (iPrescription == null) {
        throw new NoPrescriptionToPrintException();
    }

    //instantiate a new document, create a page and add the page to the document
    PDDocument document = new PDDocument();
    PDPage page1 = new PDPage(PDPage.PAGE_SIZE_A4);
    PDRectangle rectangle = page1.getMediaBox();
    document.addPage(page1);

    //create fonts for the document
    PDFont fontPlain = PDType1Font.HELVETICA;
    PDFont fontBold = PDType1Font.HELVETICA_BOLD;

    //running variable to calculate current line
    int line = 0;

    try {
        //new Stream to print into the file
        PDPageContentStream stream = new PDPageContentStream(document, page1);

        //print header (headlining)
        stream.beginText();
        stream.setFont(fontBold, 14);
        stream.moveTextPositionByAmount(SPACING_LEFT, rectangle.getHeight() - SPACING_TOP);
        stream.drawString("Prescription:");
        stream.endText();

        //get patient to print data from
        IPatient iPatient = iPrescription.getPatient();

        //write data from patient
        stream.beginText();
        stream.setFont(fontPlain, 12);
        stream.moveTextPositionByAmount(SPACING_LEFT,
                rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
        stream.drawString(iPatient.getFirstName() + " " + iPatient.getLastName());
        stream.endText();

        stream.beginText();
        stream.setFont(fontPlain, 12);
        stream.moveTextPositionByAmount(SPACING_LEFT,
                rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
        /* -- Team D: Add check on null -- */
        stream.drawString(iPatient.getBirthDay() == null ? "" : iPatient.getBirthDay().toString());
        /* -- -- -- */
        stream.endText();

        if (iPatient.getStreet() != null) {
            stream.beginText();
            stream.setFont(fontPlain, 12);
            stream.moveTextPositionByAmount(SPACING_LEFT,
                    rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
            stream.drawString(iPatient.getStreet());
            stream.endText();
        }

        if ((iPatient.getPostalCode() != null) && (iPatient.getCity() != null)) {
            stream.beginText();
            stream.setFont(fontPlain, 12);
            stream.moveTextPositionByAmount(SPACING_LEFT,
                    rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
            stream.drawString(iPatient.getPostalCode() + ", " + iPatient.getCity());
            stream.endText();
        }

        //next row
        ++line;

        //write data from doctor
        stream.beginText();
        stream.setFont(fontBold, 14);
        stream.moveTextPositionByAmount(SPACING_LEFT,
                rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
        stream.drawString("Prescription issued from doctor:");
        stream.endText();

        stream.beginText();
        stream.setFont(fontPlain, 12);
        stream.moveTextPositionByAmount(SPACING_LEFT,
                rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
        stream.drawString(iDoctor.getTitle() + " " + iDoctor.getFirstName() + " " + iDoctor.getLastName());
        stream.endText();

        //next row
        ++line;

        //print all the entries in the prescription
        if (iPrescription.getPrescriptionEntries().size() > 0) {
            stream.beginText();
            stream.setFont(fontBold, 14);
            stream.moveTextPositionByAmount(SPACING_LEFT,
                    rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
            stream.drawString("Prescription Entries:");
            stream.endText();
            for (IPrescriptionEntry entry : iPrescription.getPrescriptionEntries()) {
                stream.beginText();
                stream.setFont(fontPlain, 12);
                stream.moveTextPositionByAmount(SPACING_LEFT,
                        rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
                stream.drawString("ID: " + entry.getId() + ", " + entry.getMedicine());
                stream.endText();
            }
        }

        //print oculus image into pdf file
        //Not working
        /*BufferedImage awtImage = ImageIO.read(new File("oculus.JPG"));
        PDXObjectImage ximage = new PDPixelMap(document, awtImage);
        float scale = 0.3f; // alter this value to set the image size
        stream.drawXObject(ximage, 380, 780, ximage.getWidth() * scale, ximage.getHeight() * scale);*/

        //signature field

        ++line;
        stream.beginText();
        stream.setFont(fontPlain, 12);
        stream.moveTextPositionByAmount(SPACING_LEFT,
                rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
        stream.drawString("Doctor's Signature:");
        stream.endText();

        //print a line for signature field
        stream.setLineWidth(0.5f);
        stream.addLine(SPACING_LEFT, rectangle.getHeight() - LINE_HEIGHT * (line += 2) - SPACING_HEADER,
                SPACING_LEFT + 100, rectangle.getHeight() - LINE_HEIGHT * (line) - SPACING_HEADER);
        stream.closeAndStroke();

        //close the stream
        stream.close();

        //save the document and close it
        document.save("prescription.pdf"); //Todo: position from property file
        document.close();
        //open file with standard OS application
        Desktop.getDesktop().open(new File("prescription.pdf"));

        //Print file directly from standard printer (NOT SUPPORTED ON OCULUS-LINUX -- should be tested first!!!)
        //Desktop.getDesktop().print(new File("/home/oculus/IdeaProjects/Oculus/Technical/src/at/oculus/teamf/technical/printing/output_files/prescription.pdf"));
    } catch (COSVisitorException | CantGetPresciptionEntriesException | IOException e) {
        throw e;
    }
}

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

public static void createDischargeSummaryPDF(PDDocument pdf) {

    PDPage front = new PDPage();
    pdf.addPage(front);

}

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  va 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 PDPage newPageBuilder(PDDocument pdf, PDPage pg_content, PDPageContentStream cs,
        DischargeSummaryData data, int yCursor) throws IOException {
    pdf.addPage(pg_content);
    cs.close();//from w  w w. j a v  a  2 s .c  o m
    PDPage pg = new PDPage(new PDRectangle(pageWidth, pageHeight));
    return pg;
}

From source file:blankpdf.BlankPDF.java

public void TestePDF() {
    String fileName = "Sparta.pdf"; // name of our file

    PDDocument doc = new PDDocument();
    PDPage page = new PDPage();

    doc.addPage(page);
    PDImageXObject imagem;/*w ww  .  j a  v  a  2  s  .  c  o m*/
    PDPageContentStream content;
    try {
        content = new PDPageContentStream(doc, page);

        //OBSERVAO IMPORTANTE --
        //Para funcionar sem tratamento de string criar o projeto em pasta sem caracteres
        //especiais nem ESPAO EM BRANCO
        imagem = PDImageXObject.createFromFile(getClass().getResource("sparta.png").getPath(), doc);
        ///Users/marcelosiedler/Google%20Drive/bage/2016_02/BlankPDF/build/classes/blankpdf/silviosantos.jpg
        //imagem = PDImageXObject.createFromFile("/Users/marcelosiedler/Google Drive/bage/2016_02/BlankPDF/build/classes/blankpdf/sparta.png", doc);

        content.beginText();
        content.setFont(PDType1Font.TIMES_BOLD, 26);
        content.newLineAtOffset(10, 750);
        content.showText("Gincana IFSUL2");
        content.endText();

        content.beginText();
        content.setFont(PDType1Font.TIMES_BOLD, 16);
        content.newLineAtOffset(80, 700);
        content.showText("Turma : ");
        content.endText();

        content.drawImage(imagem, 75, 500);

        content.close();
        doc.save(fileName);
        doc.close();

        System.out.println("Arquivo criado em : " + System.getProperty("user.dir"));

    } catch (Exception e) {

        System.out.println(e.getMessage());

    }

}

From source file:boxtable.table.Table.java

License:Apache License

/**
 * Starts a new page with the same size as the last one
 * /*w  ww.  j a  va2 s .  c o m*/
 * @param document
 *            The document the table is rendered to
 * @param stream
 *            The PDPageContentStream used to render the table up to now (will be closed after calling this method)
 * @return A new PDPageContentStream for rendering to the new page
 * @throws IOException
 *             If writing to the streams fails
 */
private PDPageContentStream newPage(final PDDocument document, final PDPageContentStream stream)
        throws IOException {
    final PDRectangle pageSize = document.getPage(document.getNumberOfPages() - 1).getMediaBox();
    handleEvent(EventType.END_PAGE, document, stream, 0, pageSize.getHeight(), pageSize.getWidth(),
            pageSize.getHeight());
    stream.close();
    final PDPage page = new PDPage(pageSize);
    document.addPage(page);
    PDPageContentStream newStream = new PDPageContentStream(document, page, AppendMode.APPEND, true);
    handleEvent(EventType.BEGIN_PAGE, document, newStream, 0, pageSize.getHeight(), pageSize.getWidth(),
            pageSize.getHeight());
    return newStream;
}

From source file:Bulletin.test.java

public static void test2() {
    try {//from  ww w .  j  a v a2s. c  o m
        PDDocument document = new PDDocument();
        PDPage page = new PDPage(PDRectangle.A4);
        PDPageContentStream cos = null;
        try {
            cos = new PDPageContentStream(document, page);
        } catch (IOException ex) {
            Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
        }

        float X = 501.4f;
        float Y = 556.0f;
        //            String text = "HALLO";
        //            String text = "HALLO#HOE#GAAT#HET";
        //            String text = "HALLO#HOE#GAAT";
        String text = "Non atteints";
        int rh = 10;

        cos.moveTo(X, Y - 50);
        cos.lineTo(X, Y + 50);
        cos.stroke();
        cos.moveTo(X - 50, Y);
        cos.lineTo(X + 50, Y);
        cos.stroke();

        cos.beginText();
        cos.setFont(PDType1Font.HELVETICA, 6);
        float dtw = 0.5f * getTextWidth(PDType1Font.HELVETICA, text, 6);
        float dth = 0.0f * getFontHeight(PDType1Font.HELVETICA, 6);
        int nbLines = text.split("#").length;

        Y += 0.5f * (nbLines - 1) * rh;

        for (String line : text.split("#")) {
            Matrix M = Matrix.getTranslateInstance(X, Y);
            M.concatenate(Matrix.getRotateInstance(Math.toRadians(0), 0, 0));
            M.concatenate(Matrix.getTranslateInstance(-dtw, -dth));
            cos.setTextMatrix(M);
            cos.showText(line);
            Y -= rh;
        }
        cos.close();
        document.addPage(page);
        document.save("/tmp/test.pdf");
        document.close();

    } catch (IOException ex) {
        Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:casmi.Applet.java

License:Open Source License

@Override
public void drawWithGraphics(Graphics g) {
    setGLParam(g.getGL());//from   w ww.  j  ava  2s .co m

    drawObjects(g);

    updateObjects();

    // Calculate real fps.
    {
        frame++;
        long now = System.currentTimeMillis();
        long elapse = now - baseTime;
        if (1000 < elapse) {
            workingFPS = frame * 1000.0 / elapse;
            baseTime = now;
            frame = 0;
        }
    }

    // capture image
    if (saveImageFlag) {
        saveImageFlag = false;

        try {
            switch (imageType) {
            case JPG:
            case PNG:
            case BMP:
            case GIF:
            default:
                Screenshot.writeToFile(new File(saveFile), width, height, !saveBackground);
                break;
            case PDF: {
                BufferedImage bi = Screenshot.readToBufferedImage(width, height, !saveBackground);
                PDDocument doc = new PDDocument();
                PDRectangle rect = new PDRectangle(width, height);
                PDPage page = new PDPage(rect);
                doc.addPage(page);
                PDXObjectImage ximage = new PDJpeg(doc, bi);
                PDPageContentStream contentStream = new PDPageContentStream(doc, page);
                contentStream.drawImage(ximage, 0, 0);
                contentStream.close();
                doc.save(saveFile);
                doc.close();
                break;
            }
            }
        } catch (GLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (COSVisitorException e) {
            e.printStackTrace();
        }
    }
}