public void execute(PdfContentByte cb, BaseFont bf) {
   String t = sanitizeText(getText());
   Barcode128 code128 = new Barcode128();
   code128.setGenerateChecksum(true);
   code128.setCode(t);
   if (!isShowText()) {
     code128.setFont(null);
   }
   code128.setBarHeight(getSize());
   Image img = code128.createImageWithBarcode(cb, getColor(), getColor());
   // float w = code128.getBarcodeSize().width() / 2;
   img.setAbsolutePosition(getOffsetX(), getOffsetY());
   img.setRotationDegrees(getDirection());
   try {
     cb.addImage(img);
   } catch (DocumentException e) {
     // Do nothing for the time being...
   }
 }
  protected void handleDocument(Document document, PdfWriter writer, Map model)
      throws DocumentException {

    java.util.List sampleList = (java.util.List) model.get("list");
    Integer scapeNo = (Integer) model.get("scapeNo");
    Iterator ir = sampleList.iterator();

    try {

      PdfContentByte cb = writer.getDirectContent();
      BaseFont bf = BaseFont.createFont("Helvetica", "winansi", false);

      int scapeN = scapeNo.intValue();
      int i = scapeN;
      int pageI = i;

      while (ir.hasNext()) {

        Sample sample = (Sample) ir.next();
        String internalId = sample.getPatient().getIntSampleId();

        PdfTemplate template = cb.createTemplate(templateW, templateL);

        // USING ean8 BARCODE
        Barcode128 code = new Barcode128();
        code.setCodeType(Barcode.CODE128);
        code.setX(0.75f);
        // log.debug("the x is " + codeEAN.getX());
        code.setBarHeight(barcodeSize);
        code.setSize(0);
        // code.setCode(formatBarcode(sampleId));
        code.setCode(internalId);
        Image imageBar = code.createImageWithBarcode(template, null, null);

        template.beginText();
        template.setFontAndSize(bf, fontSize);
        template.moveText(x, y2);
        template.showText(internalId);
        template.endText();

        template.addImage(imageBar, imageBar.width(), 0, 0, imageBar.height(), x, y1);

        int yNo = pageI / columnNo + 1;
        int xNo = pageI % columnNo;

        int templateX = marginX + (xNo * templateW);
        int templateY = totalPageY - marginY - (yNo * templateL);

        // log.debug("the i is "+i + " the adjustY is " + tempYAdjust + " the templateY is " +
        // templateY);

        cb.addTemplate(template, templateX, templateY);

        i++;
        pageI++;

        if ((i % (rowNo * columnNo)) == 0) {
          pageI = 0;

          // we go to a new page
          document.newPage();
        }
      }

      document.close();
      System.out.println("Finished.");
    } catch (Exception de) {
      de.printStackTrace();
    }
  }
Example #3
0
  public void generatePdf(java.lang.String memNo) {

    java.lang.Process wait_for_Pdf2Show;

    java.util.Calendar cal = java.util.Calendar.getInstance();

    java.util.Date dateStampPdf = cal.getTime();

    java.lang.String pdfDateStamp = dateStampPdf.toString();

    try {

      java.io.File tempFile =
          java.io.File.createTempFile("REP" + this.getDateLable() + "_", ".pdf");

      tempFile.deleteOnExit();

      java.lang.Runtime rt = java.lang.Runtime.getRuntime();

      java.lang.String debitTotal = null;

      java.lang.String creditTotal = null;

      int titleFont = 0;
      int bodyFont = 0;
      float Widths = 0;
      float Heights = 0;
      float Margins = 0;

      try {
        connectDB.setAutoCommit(false);

        java.sql.Statement stm1 = connectDB.createStatement();
        java.sql.ResultSet rse1 =
            stm1.executeQuery(
                "select header_font,body_font,width,height,margins from receipt_pref");
        while (rse1.next()) {
          titleFont = rse1.getInt(1);
          bodyFont = rse1.getInt(2);
          Widths = rse1.getFloat(3);
          Heights = rse1.getFloat(4);
          Margins = rse1.getFloat(5);
        }
      } catch (java.sql.SQLException sq) {

        try {
          connectDB.rollback();
        } catch (java.sql.SQLException sql) {
          //   javax.swing.JOptionPane.showMessageDialog(this,sql.getMessage(),"Error
          // Message!",javax.swing.JOptionPane.ERROR_MESSAGE);
        }
        System.out.println(sq.getMessage());
        //   javax.swing.JOptionPane.showMessageDialog(this,sq.getMessage(),
        // "Error",javax.swing.JOptionPane.ERROR_MESSAGE);

      }
      com.lowagie.text.Font pFontHeader2 =
          FontFactory.getFont(FontFactory.HELVETICA, bodyFont, Font.BOLD);

      com.lowagie.text.Font pFontHeader =
          FontFactory.getFont(FontFactory.HELVETICA, bodyFont, Font.NORMAL);
      com.lowagie.text.Font pFontHeader1 =
          FontFactory.getFont(FontFactory.HELVETICA, titleFont, Font.BOLD);
      com.lowagie.text.Font pFontHeader11 =
          FontFactory.getFont(FontFactory.TIMES_ITALIC, 8, Font.BOLD);

      // com.lowagie.text.Document docPdf = new com.lowagie.text.Document(new
      // Rectangle(java.lang.Float.parseFloat(System.getProperty("papersize_width")),
      // java.lang.Float.parseFloat(System.getProperty("papersize_legnth"))),java.lang.Float.parseFloat(System.getProperty("receiptPageMargin")),java.lang.Float.parseFloat(System.getProperty("receiptPageMargin")),java.lang.Float.parseFloat(System.getProperty("receiptPageMargin")),java.lang.Float.parseFloat(System.getProperty("receiptPageMargin")));
      ///// com.lowagie.text.Document docPdf = new com.lowagie.text.Document(new Rectangle((Widths),
      // Heights), Margins, Margins, Margins, Margins);
      com.lowagie.text.Document docPdf = new com.lowagie.text.Document(PageSize.A4.rotate());

      //            com.lowagie.text.Document docPdf = new com.lowagie.text.Document(new
      // Rectangle(java.lang.Float.parseFloat(System.getProperty("papersize_width")),
      // java.lang.Float.parseFloat(System.getProperty("papersize_legnth"))));

      try {

        try {

          com.lowagie.text.pdf.PdfWriter.getInstance(
              docPdf, new java.io.FileOutputStream(tempFile));

          String compName = null;
          String date = null;
          PdfWriter writer = PdfWriter.getInstance(docPdf, new FileOutputStream(tempFile));
          docPdf.open();
          PdfContentByte cb = writer.getDirectContent();
          String footers = null;
          try {

            com.lowagie.text.pdf.PdfPTable table = new com.lowagie.text.pdf.PdfPTable(6);

            int headerwidths[] = {15, 15, 15, 15, 15, 15};

            table.setWidths(headerwidths);

            table.setWidthPercentage((100));
            table.getDefaultCell().setColspan(6);
            table.getDefaultCell().setFixedHeight(55);
            table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
            table.addCell(Image.getInstance(com.afrisoftech.lib.CompanyLogo.getPath2Logo()));
            table.getDefaultCell().setFixedHeight(16);
            Phrase phrase = new Phrase("");

            // table.addCell(phrase);
            table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);
            table.getDefaultCell().setColspan(1);
            table.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
            table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

            try {

              String patAge = "-";

              java.sql.Statement st = connectDB.createStatement();
              java.sql.Statement st1 = connectDB.createStatement();
              java.sql.Statement st2 = connectDB.createStatement();
              java.sql.Statement st5 = connectDB.createStatement();
              java.sql.Statement st6 = connectDB.createStatement();
              java.sql.Statement st51 = connectDB.createStatement();
              java.sql.Statement st3 = connectDB.createStatement();
              java.sql.ResultSet rset3 =
                  st3.executeQuery(
                      "select hospital_name,box_no,main_telno,main_faxno from pb_hospitalprofile");

              java.sql.ResultSet rs =
                  st.executeQuery(
                      "select patient_no,patient_name,discharge_no,discharge_date,doctor, date_part('hour', discharge_time::time) ||':' || date_part('min', discharge_time::time) ||':'||round(date_part('sec', discharge_time::time)),admission_date,user_name,ward from hp_patient_discharge cb where inv_no = '"
                          + invoiceNo
                          + "'");

              System.out.println(MNo);

              while (rset3.next()) {
                table.getDefaultCell().setColspan(6);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
                phrase = new Phrase(rset3.getObject(1).toString(), pFontHeader1);
                table.addCell(phrase);

                phrase = new Phrase("P.O. BOX : " + rset3.getObject(2).toString(), pFontHeader2);
                table.addCell(phrase);

                phrase =
                    new Phrase(
                        "TEL : "
                            + rset3.getObject(3).toString()
                            + "  FAX : "
                            + rset3.getObject(4).toString(),
                        pFontHeader2);
                table.addCell(phrase);
              }
              //  table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);
              //  table.getDefaultCell().setBorderWidth(Rectangle.TOP);

              table.getDefaultCell().setColspan(6);
              phrase = new Phrase("PATIENT RELEASE FORM", pFontHeader1);

              table.addCell(phrase);

              java.sql.PreparedStatement pstmtHeader =
                  connectDB.prepareStatement("SELECT nextval('release_seq_no'), current_user");
              java.sql.ResultSet rsetheader = pstmtHeader.executeQuery();

              table.getDefaultCell().setColspan(2);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Form No : 501".toUpperCase(), pFontHeader2);

              table.addCell(phrase);
              while (rsetheader.next()) {
                table.getDefaultCell().setColspan(2);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase =
                    new Phrase("Serial No : ".toUpperCase() + rsetheader.getInt(1), pFontHeader2);

                table.addCell(phrase);
                table.getDefaultCell().setColspan(2);
                phrase =
                    new Phrase(
                        "Printed by : ".toUpperCase() + rsetheader.getString(2).toUpperCase(),
                        pFontHeader2);

                table.addCell(phrase);
              }

              table.getDefaultCell().setColspan(2);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              // phrase = new Phrase("Internal Claim No : ".toUpperCase(), pFontHeader);

              //  table.addCell(phrase);

              java.sql.PreparedStatement pstmtClaimNo =
                  connectDB.prepareStatement(
                      "SELECT claim_no FROM hp_patient_discharge WHERE patient_no = ?");

              pstmtClaimNo.setString(1, MNo);

              java.sql.ResultSet rsetClaimNo = pstmtClaimNo.executeQuery();

              table.getDefaultCell().setColspan(2);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);

              //    while (rsetClaimNo.next()) {

              //        phrase = new Phrase("Internal Claim No : " + invoiceNo, pFontHeader);

              //    }

              //  table.addCell(phrase);

              table.getDefaultCell().setColspan(2);
              String claimNumber = "";
              while (rsetClaimNo.next()) {
                claimNumber = rsetClaimNo.getString(1).toUpperCase(); /* + rsheader.getString(6)*/
              }
              System.out.println("Printing claim number : [" + claimNumber + "]");
              phrase =
                  new Phrase(
                      "NHIF Claim Number : ".toUpperCase() + claimNumber.toUpperCase(),
                      pFontHeader);
              table.addCell(phrase);

              while (rs.next()) {

                table.getDefaultCell().setColspan(2);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase =
                    new Phrase(
                        "Discharge No : ".toUpperCase() + rs.getString(3).toUpperCase(),
                        pFontHeader);

                table.addCell(phrase);

                table.getDefaultCell().setColspan(2);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase =
                    new Phrase(
                        "Date : ".toUpperCase() + rs.getObject(4).toString().toUpperCase(),
                        pFontHeader);

                table.addCell(phrase);
                table.getDefaultCell().setColspan(2);
                phrase =
                    new Phrase(
                        "TIME : ".toUpperCase() + rs.getString(6).toUpperCase(), pFontHeader);

                table.addCell(phrase);

                patNo = rs.getString(1);

                table.getDefaultCell().setColspan(2);
                phrase =
                    new Phrase("Patient No : ".toUpperCase() + patNo.toUpperCase(), pFontHeader);

                table.addCell(phrase);
                table.getDefaultCell().setColspan(2);
                phrase =
                    new Phrase(
                        "Patient Name : ".toUpperCase() + rs.getObject(2).toString().toUpperCase(),
                        pFontHeader);
                patientName = rs.getObject(2).toString();
                table.addCell(phrase);
                table.getDefaultCell().setColspan(2);
                java.sql.ResultSet rsw =
                    st1.executeQuery(
                        "SELECT ('now'::date-year_of_birth::date)/365 FROM hp_inpatient_register WHERE patient_no =  '"
                            + rs.getString(1)
                            + "'");
                while (rsw.next()) {
                  phrase =
                      new Phrase(
                          "Age: ".toUpperCase() + rsw.getObject(1).toString().toUpperCase(),
                          pFontHeader);

                  table.addCell(phrase);
                }
                table.getDefaultCell().setColspan(2);
                phrase =
                    new Phrase(
                        "Admission Date : ".toUpperCase() + rs.getString(7).toUpperCase(),
                        pFontHeader);

                table.addCell(phrase);

                table.getDefaultCell().setColspan(2);
                phrase =
                    new Phrase(
                        "Discharge Date : ".toUpperCase()
                            + rs.getObject(4).toString().toUpperCase(),
                        pFontHeader);

                table.addCell(phrase);
                table.getDefaultCell().setColspan(2);
                phrase =
                    new Phrase(
                        "Ward : ".toUpperCase() + rs.getObject(9).toString().toUpperCase(),
                        pFontHeader);

                table.addCell(phrase);
              }
              table.getDefaultCell().setColspan(6);
              phrase = new Phrase("  ", pFontHeader);

              table.addCell(phrase);
              phrase = new Phrase("Method of Payment(Tick where appropriate)", pFontHeader11);

              table.addCell(phrase);

              phrase =
                  new Phrase(
                      "Cash [   ] / Cheque [   ] / N.H.I.F [   ] / CO-OP [   ] / M.R.M [   ] / OTHERS.......................................................... Final Invoice No : "
                          + invoiceNo.toUpperCase(),
                      pFontHeader2);

              table.addCell(phrase);

              table.getDefaultCell().setColspan(6);
              phrase = new Phrase(" ", pFontHeader);
              table.addCell(phrase);
              table.getDefaultCell().setColspan(2);
              phrase =
                  new Phrase("Finance Clearance : ____________________".toUpperCase(), pFontHeader);
              table.addCell(phrase);

              phrase = new Phrase("Sign/Stamp: ______________________".toUpperCase(), pFontHeader);
              table.addCell(phrase);
              phrase =
                  new Phrase(
                      "Check-Out Date and Time: _____________________".toUpperCase(), pFontHeader);

              table.addCell(phrase);

              table.getDefaultCell().setColspan(6);
              phrase = new Phrase(" ", pFontHeader);
              table.addCell(phrase);
              table.getDefaultCell().setColspan(2);
              phrase =
                  new Phrase(
                      "Nurse Clearance : _______________________".toUpperCase(), pFontHeader);
              table.addCell(phrase);

              phrase = new Phrase("Sign/Stamp: ______________________".toUpperCase(), pFontHeader);
              table.addCell(phrase);
              phrase =
                  new Phrase(
                      "Check-Out Date and Time: ______________________".toUpperCase(), pFontHeader);

              table.addCell(phrase);
              table.getDefaultCell().setColspan(6);
              phrase = new Phrase(" ", pFontHeader);
              table.addCell(phrase);
              table.getDefaultCell().setColspan(2);
              phrase =
                  new Phrase("Security Clearance: ___________________".toUpperCase(), pFontHeader);
              table.addCell(phrase);

              phrase = new Phrase("Sign/Stamp: ___________________".toUpperCase(), pFontHeader);
              table.addCell(phrase);
              phrase =
                  new Phrase(
                      "Check-Out Date and Time: ____________________".toUpperCase(), pFontHeader);

              table.addCell(phrase);
              table.getDefaultCell().setColspan(6);

              //  table.addCell(phrase);
              table.getDefaultCell().setColspan(6);
              phrase = new Phrase(" ", pFontHeader);
              table.addCell(phrase);
              phrase =
                  new Phrase(
                      "NB This form should be filled in duplicate, one copy to be retained in the ward and the other one be left at the Main Gate. PLEASE NOTE THAT CHECK-OUT DATE MUST BE THE DATE THE PATIENT LEAVES THE HOSPITAL",
                      pFontHeader11);

              table.addCell(phrase);
              phrase = new Phrase(" ");
              table.addCell(phrase);
              Barcode128 code128 = new Barcode128();
              code128.setCode(patNo + " " + patientName + " " + invoiceNo);
              code128.setBarHeight(20);

              code128.setTextAlignment(Element.ALIGN_CENTER);

              docPdf.add(table);

              System.out.println(cb);

              docPdf.add(code128.createImageWithBarcode(cb, null, null));

            } catch (java.sql.SQLException SqlExec) {

              SqlExec.printStackTrace();

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), SqlExec.getMessage());
            }

            // }

          } catch (com.lowagie.text.BadElementException BadElExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }

        } catch (java.io.FileNotFoundException fnfExec) {

          javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), fnfExec.getMessage());
        }
      } catch (com.lowagie.text.DocumentException lwDocexec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), lwDocexec.getMessage());
      }

      docPdf.close();
      com.afrisoftech.lib.PDFRenderer.renderPDF(tempFile);

    } catch (java.io.IOException IOexec) {

      javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), IOexec.getMessage());
    }
  }