Example usage for com.lowagie.text DocumentException DocumentException

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

Introduction

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

Prototype

public DocumentException() 

Source Link

Document

Constructs a DocumentException without a message.

Usage

From source file:oscar.oscarEncounter.oscarConsultationRequest.pageUtil.EctConsultationFormRequestPrintPdf.java

License:Open Source License

public void printPdf() throws IOException, DocumentException {

    EctConsultationFormRequestUtil reqForm = new EctConsultationFormRequestUtil();
    reqForm.estRequestFromId((String) request.getAttribute("reqId"));

    //make sure we have data to print
    if (reqForm == null)
        throw new DocumentException();

    // init req form info
    reqForm.specAddr = request.getParameter("address");
    if (reqForm.specAddr == null) {
        reqForm.specAddr = new String();
    }//from   ww  w  . j a v  a 2s.c  om
    reqForm.specPhone = request.getParameter("phone");
    if (reqForm.specPhone == null) {
        reqForm.specPhone = "";
    }
    reqForm.specFax = request.getParameter("fax");
    if (reqForm.specFax == null) {
        reqForm.specFax = "";
    }

    //Create new file to save form to
    String path = OscarProperties.getInstance().getProperty("DOCUMENT_DIR");
    String fileName = path + "ConsultationRequestForm-" + UtilDateUtilities.getToday("yyyy-MM-dd.hh.mm.ss")
            + ".pdf";
    FileOutputStream out = new FileOutputStream(fileName);

    //Create the document we are going to write to
    document = new Document();
    writer = PdfWriter.getInstance(document, out);

    //Use the template located at '/oscar/oscarEncounter/oscarConsultationRequest/props'
    reader = new PdfReader("/oscar/oscarEncounter/oscarConsultationRequest/props/consultationFormRequest.pdf");
    Rectangle pSize = reader.getPageSize(1);
    width = pSize.getWidth();
    height = pSize.getHeight();
    document.setPageSize(pSize);

    document.addTitle("Consultation Form Request");
    document.addCreator("OSCAR");
    document.open();

    //Create the font we are going to print to
    bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

    cb = writer.getDirectContent();
    ct = new ColumnText(cb);
    cb.setColorStroke(new Color(0, 0, 0));

    // start writing the pdf document
    PdfImportedPage page1 = writer.getImportedPage(reader, 1);
    cb.addTemplate(page1, 1, 0, 0, 1, 0, 0);
    addFooter();
    setAppointmentInfo(reqForm);

    // add the dynamically positioned text elements
    float dynamicHeight = 0;
    dynamicHeight = addDynamicPositionedText("Reason For Consultation: ", reqForm.reasonForConsultation,
            dynamicHeight, reqForm);
    dynamicHeight = addDynamicPositionedText("Pertinent Clinical Information: ", reqForm.clinicalInformation,
            dynamicHeight, reqForm);
    dynamicHeight = addDynamicPositionedText("Significant Concurrent Problems: ", reqForm.concurrentProblems,
            dynamicHeight, reqForm);
    dynamicHeight = addDynamicPositionedText("Current Medications: ", reqForm.currentMedications, dynamicHeight,
            reqForm);
    dynamicHeight = addDynamicPositionedText("Allergies: ", reqForm.allergies, dynamicHeight, reqForm);

    document.close();
    reader.close();
    writer.close();
    out.close();

    // combine the recently created pdf with any pdfs that were added to the consultation request form
    combinePDFs(fileName);

}

From source file:oscar.oscarLab.ca.all.pageUtil.LabPDFCreator.java

License:Open Source License

public void printPdf() throws IOException, DocumentException {

    // check that we have data to print
    if (handler == null)
        throw new DocumentException();

    //response.setContentType("application/pdf");  //octet-stream
    //response.setHeader("Content-Disposition", "attachment; filename=\""+handler.getPatientName().replaceAll("\\s", "_")+"_LabReport.pdf\"");

    //Create the document we are going to write to
    document = new Document();
    //PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
    PdfWriter writer = PdfWriter.getInstance(document, os);

    //Set page event, function onEndPage will execute each time a page is finished being created
    writer.setPageEvent(this);

    document.setPageSize(PageSize.LETTER);
    document.addTitle("Title of the Document");
    document.addCreator("OSCAR");
    document.open();//from w ww . j a v  a 2 s  .  c o  m

    //Create the fonts that we are going to use
    bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    font = new Font(bf, 9, Font.NORMAL);
    boldFont = new Font(bf, 10, Font.BOLD);
    redFont = new Font(bf, 9, Font.NORMAL, Color.RED);

    // add the header table containing the patient and lab info to the document
    createInfoTable();

    // add the tests and test info for each header
    ArrayList<String> headers = handler.getHeaders();
    for (int i = 0; i < headers.size(); i++)
        addLabCategory(headers.get(i));

    // add end of report table
    PdfPTable table = new PdfPTable(1);
    table.setWidthPercentage(100);
    PdfPCell cell = new PdfPCell();
    cell.setBorder(0);
    cell.setPhrase(new Phrase("  "));
    table.addCell(cell);
    cell.setBorder(15);
    cell.setBackgroundColor(new Color(210, 212, 255));
    cell.setPhrase(new Phrase("END OF REPORT", boldFont));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    table.addCell(cell);
    document.add(table);

    document.close();

    os.flush();
}

From source file:oscar.oscarPrevention.pageUtil.PreventionPrintPdf.java

License:Open Source License

public void printPdf(String[] headerIds, HttpServletRequest request, OutputStream outputStream)
        throws IOException, DocumentException {
    //make sure we have data to print
    //String[] headerIds = request.getParameterValues("printHP");
    if (headerIds == null)
        throw new DocumentException();

    //Create the document we are going to write to
    document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);
    document.setPageSize(PageSize.LETTER);
    document.open();/*from   ww w . ja va  2s. c o  m*/

    //Create the font we are going to print to
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    Font font = new Font(bf, FONTSIZE, Font.NORMAL);
    float leading = font.getCalculatedLeading(LINESPACING);

    //set up document title and header
    String title = "Preventions for " + request.getParameter("nameAge");
    String hin = "HIN: " + request.getParameter("hin");
    String mrp = request.getParameter("mrp");
    if (mrp != null) {
        Properties prop = (Properties) request.getSession().getAttribute("providerBean");
        mrp = "MRP: " + prop.getProperty(mrp, "unknown");
    }

    ClinicData clinicData = new ClinicData();
    clinicData.refreshClinicData();
    String[] clinic = new String[] { clinicData.getClinicName(), clinicData.getClinicAddress(),
            clinicData.getClinicCity() + ", " + clinicData.getClinicProvince(), clinicData.getClinicPostal(),
            clinicData.getClinicPhone(), title, hin, mrp };

    //Header will be printed at top of every page beginning with p2
    Phrase headerPhrase = new Phrase(LEADING, title, font);
    HeaderFooter header = new HeaderFooter(headerPhrase, false);
    header.setAlignment(HeaderFooter.ALIGN_CENTER);
    document.setHeader(header);

    //Write title with top and bottom borders on p1
    cb = writer.getDirectContent();
    cb.setColorStroke(new Color(0, 0, 0));
    cb.setLineWidth(0.5f);

    cb.moveTo(document.left(), document.top());
    cb.lineTo(document.right(), document.top());
    cb.stroke();
    cb.setFontAndSize(bf, FONTSIZE);

    upperYcoord = document.top() - (font.getCalculatedLeading(LINESPACING) * 2f);
    cb.beginText();
    for (int idx = 0; idx < clinic.length; ++idx) {
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, clinic[idx], document.right() / 2f, upperYcoord, 0f);
        upperYcoord -= font.getCalculatedLeading(LINESPACING);
    }

    cb.endText();
    cb.moveTo(document.left(), upperYcoord);
    cb.lineTo(document.right(), upperYcoord);
    cb.stroke();

    //get top y-coord for starting to print columns
    upperYcoord = cb.getYTLM() - font.getCalculatedLeading(LINESPACING * 2f);

    int subIdx;
    String preventionHeader, procedureAge, procedureDate;

    //1 - obtain number of lines of incoming prevention data
    numLines = 0;
    for (int idx = 0; idx < headerIds.length; ++idx) {
        ++numLines;
        subIdx = 0;
        while (request.getParameter("preventProcedureAge" + headerIds[idx] + "-" + subIdx) != null) {
            ++subIdx;
            numLines += 3;
        }
        numLines += 2;
    }

    //2 - calculate max num of lines a page can hold and number of pages of data we have
    pageHeight = upperYcoord - document.bottom();
    maxLines = (int) Math.floor(pageHeight / (font.getCalculatedLeading(LINESPACING) + 4d));
    numPages = (int) Math.ceil(numLines / ((double) maxLines * NUMCOLS));

    //3 - Start the column
    ct = new ColumnText(cb);
    ct.setSimpleColumn(document.left(), document.bottom(), document.right() / 2f, upperYcoord);

    linesToBeWritten = linesWritten = 0;
    boolean pageBreak = false;

    curPage = 1;
    totalLinesWritten = 0;

    //add promotext to current page
    addPromoText();

    //if we have > 1 element but less than a page of data, shrink maxLines so we can try to balance text in columns
    if (headerIds.length > 1) {
        if (curPage == numPages) {
            maxLines = numLines / NUMCOLS;
        }
    }

    //now we can start to print the prevention data
    for (int idx = 0; idx < headerIds.length; ++idx) {

        linesToBeWritten = 4; //minimum lines for header and one prevention item
        pageBreak = checkColumnFill(ct, "", font, pageBreak); //if necessary break before we print prevention header

        preventionHeader = request.getParameter("preventionHeader" + headerIds[idx]);
        Phrase procHeader = new Phrase(LEADING, "Prevention " + preventionHeader + "\n", font);
        ct.addText(procHeader);
        subIdx = 0;

        while ((procedureAge = request
                .getParameter("preventProcedureAge" + headerIds[idx] + "-" + subIdx)) != null) {
            procedureDate = request.getParameter("preventProcedureDate" + headerIds[idx] + "-" + subIdx);

            linesToBeWritten = 3;
            pageBreak = checkColumnFill(ct, preventionHeader, font, pageBreak);

            Phrase procedure = new Phrase(LEADING, "     " + procedureAge + "\n", font);
            procedure.add("     " + procedureDate + "\n\n");
            ct.addText(procedure);
            ct.go();
            linesWritten += ct.getLinesWritten();
            totalLinesWritten += ct.getLinesWritten();
            ++subIdx;
        }

    }

    ColumnText.showTextAligned(cb, Phrase.ALIGN_CENTER, new Phrase("-" + curPage + "-"), document.right() / 2f,
            document.bottom() - (document.bottomMargin() / 2f), 0f);

    document.close();
}