private static Element createCleanBarcode(
     PdfContentByte cb, String numero, float width, float height) {
   Barcode128 code128 = new Barcode128();
   code128.setCode(numero);
   Image barcodeImage = code128.createImageWithBarcode(cb, null, null);
   barcodeImage.setAlignment(Image.ALIGN_CENTER);
   // barcodeImage.setWidthPercentage(75f);
   barcodeImage.scaleToFit(width, height);
   return barcodeImage;
 }
Exemple #2
0
 /**
  * Adds the different pages inside a single TIFF file to a document.
  *
  * @param document the document to which the pages have to be added
  * @param path the path to the TIFF file
  * @throws DocumentException
  * @throws IOException
  */
 public static void addTif(Document document, String path) throws DocumentException, IOException {
   RandomAccessFileOrArray ra = new RandomAccessFileOrArray(path);
   int n = TiffImage.getNumberOfPages(ra);
   Image img;
   for (int i = 1; i <= n; i++) {
     img = TiffImage.getTiffImage(ra, i);
     img.scaleToFit(523, 350);
     document.add(img);
   }
 }
  /**
   * Creates a PDF with information about the movies
   *
   * @param filename the name of the PDF file that will be created.
   * @throws DocumentException
   * @throws IOException
   * @throws SQLException
   */
  public void createPdf(String filename) throws IOException, DocumentException, SQLException {

    // step 1
    Document document = new Document();
    // step 2
    PdfWriter.getInstance(document, new FileOutputStream(filename));
    // step 3
    document.open();
    Rectangle rect = new Rectangle(0, 806, 36, 842);
    rect.setBackgroundColor(BaseColor.RED);
    document.add(rect);
    document.add(new Paragraph("Hello World!"));
    Image image = Image.getInstance(RESOURCE);
    image.scaleToFit(500, 500);
    // step 4
    document.add(image);
    // step 5
    document.close();
  }
  protected void printPatientCard(AdminPerson person) {
    try {
      table = new PdfPTable(1000);
      table.setWidthPercentage(100);
      PdfPTable table2 = new PdfPTable(1000);
      table.setWidthPercentage(100);

      // Professional card
      cell = createLabel(" ", 32, 1, Font.BOLD);
      cell.setColspan(1000);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
      cell.setPadding(0);
      table2.addCell(cell);

      cell = new PdfPCell(table2);
      cell.setColspan(1000);
      cell.setBorder(PdfPCell.NO_BORDER);
      table.addCell(cell);
      cell = createValueCell(" ");
      cell.setColspan(1000);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPadding(0);
      table.addCell(cell);

      table2 = new PdfPTable(1000);
      // Name
      cell =
          createLabel(
              ScreenHelper.getTranNoLink("web", "lastname", sPrintLanguage) + ":",
              6,
              1,
              Font.ITALIC);
      cell.setColspan(300);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPadding(1);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      table2.addCell(cell);
      cell = createLabel(person.lastname, 8, 1, Font.BOLD);
      cell.setColspan(700);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPadding(1);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      table2.addCell(cell);

      // FirstName
      cell =
          createLabel(
              ScreenHelper.getTranNoLink("web", "firstname", sPrintLanguage) + ":",
              6,
              1,
              Font.ITALIC);
      cell.setColspan(300);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPadding(1);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      table2.addCell(cell);
      cell = createLabel(person.firstname, 8, 1, Font.BOLD);
      cell.setColspan(700);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPadding(1);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      table2.addCell(cell);

      // Date of birth
      cell =
          createLabel(
              ScreenHelper.getTranNoLink("web", "dateofbirth", sPrintLanguage) + ":",
              6,
              1,
              Font.ITALIC);
      cell.setColspan(300);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPadding(1);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      table2.addCell(cell);
      cell = createLabel(person.dateOfBirth, 8, 1, Font.BOLD);
      cell.setColspan(700);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPadding(1);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      table2.addCell(cell);

      // Registration number
      cell =
          createLabel(
              ScreenHelper.getTranNoLink("web", "idcode", sPrintLanguage) + ":", 6, 1, Font.ITALIC);
      cell.setColspan(300);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      cell.setPadding(1);
      table2.addCell(cell);
      cell = createLabel(person.comment, 8, 1, Font.BOLD);
      cell.setColspan(700);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPadding(1);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      table2.addCell(cell);

      // Barcode
      PdfContentByte cb = docWriter.getDirectContent();
      Barcode39 barcode39 = new Barcode39();
      barcode39.setCode("A" + person.comment);
      barcode39.setAltText("");
      barcode39.setSize(1);
      barcode39.setBaseline(0);
      barcode39.setBarHeight(20);
      Image image = barcode39.createImageWithBarcode(cb, null, null);
      cell = new PdfPCell(image);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setVerticalAlignment(PdfPCell.ALIGN_TOP);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      cell.setColspan(1000);
      cell.setPadding(0);
      table2.addCell(cell);

      cell = new PdfPCell(table2);
      cell.setColspan(700);
      cell.setBorder(PdfPCell.NO_BORDER);
      table.addCell(cell);

      // Photo
      image = null;
      try {
        image = Image.getInstance(DatacenterHelper.getPatientPicture(person.comment));
      } catch (Exception e1) {
        e1.printStackTrace();
      }
      if (image != null) {
        image.scaleToFit(130, 72);
        cell = new PdfPCell(image);
      } else {
        cell = new PdfPCell();
      }
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
      cell.setColspan(300);
      cell.setPadding(0);
      table.addCell(cell);

      // Horizontal line
      cell = new PdfPCell();
      cell.setBorder(PdfPCell.BOTTOM);
      cell.setColspan(1000);
      table.addCell(cell);

      table2 = new PdfPTable(2000);
      cell =
          createLabel(
              ScreenHelper.getTranNoLink("web", "deliverydate", sPrintLanguage), 6, 1, Font.ITALIC);
      cell.setColspan(550);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPaddingRight(5);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
      table2.addCell(cell);
      cell = createLabel(ScreenHelper.stdDateFormat.format(new java.util.Date()), 6, 1, Font.BOLD);
      cell.setColspan(450);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      cell.setPadding(0);
      table2.addCell(cell);

      // Expiry data
      cell =
          createLabel(
              ScreenHelper.getTranNoLink("web", "expirydate", sPrintLanguage), 6, 1, Font.ITALIC);
      cell.setColspan(550);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setPaddingRight(5);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
      table2.addCell(cell);
      long day = 24 * 3600 * 1000;
      long year = 365 * day;
      long period = MedwanQuery.getInstance().getConfigInt("cardvalidityperiod", 5) * year - day;
      cell =
          createLabel(
              ScreenHelper.stdDateFormat.format(
                  new java.util.Date(new java.util.Date().getTime() + period)),
              6,
              1,
              Font.BOLD);
      cell.setColspan(450);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      cell.setPadding(0);
      table2.addCell(cell);

      cell = new PdfPCell(table2);
      cell.setColspan(1000);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
      cell.setPadding(1);
      table.addCell(cell);

      cell =
          createLabel(
              ScreenHelper.getTranNoLink("web", "cardfooter2", sPrintLanguage), 6, 1, Font.ITALIC);
      cell.setColspan(1000);
      cell.setBorder(PdfPCell.NO_BORDER);
      cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
      cell.setPadding(0);
      table.addCell(cell);

      doc.add(table);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public byte[] make(Firm firm, User user) {

    /*
     * make metadata for the document
     */
    Language language = DOC.getLanguage();

    GregorianCalendar gregCalendar = new GregorianCalendar();
    gregCalendar.setTime(DOC.getDate());

    SimpleDateFormat sdf = DateFormats.DOT_DATE_FORMAT();
    DecimalFormat COMMA_3 = NumberFormats.THREE_AFTER_COMMA();
    DecimalFormat COMMA_2 = NumberFormats.TWO_AFTER_COMMA();
    DecimalFormat DISCOUNT = NumberFormats.DISCOUNT_FORMAT();

    Font TR_12_B = DocumentFonts.TIMES_ROMAN_12_BOLD();
    Font TR_12 = DocumentFonts.TIMES_ROMAN_12();
    Font TR_10_B = DocumentFonts.TIMES_ROMAN_10_BOLD();
    Font TR_10 = DocumentFonts.TIMES_ROMAN_10();
    Font TR_10_I = DocumentFonts.TIMES_ROMAN_10_ITALIC();
    Font TR_10_U_B = DocumentFonts.TIMES_ROMAN_10_UNDERLINE_BOLD();
    Font TR_8_I = DocumentFonts.TIMES_ROMAN_8_ITALIC();

    com.itextpdf.text.Document blackBoard = null;
    com.itextpdf.text.Document finalDocument = null;

    ByteArrayOutputStream byteOutputStream = null;
    ByteArrayOutputStream temporaryByteStream = null;
    PdfReader reader;

    HeaderFooter pageEvent = new HeaderFooter(firm, true, true);

    boolean wasError = false;

    /*
     * make the document
     */

    try {

      // make an empty cell and paragraph, going to need them later multiple times
      PdfPCell emptyCell = new PdfPCell(new Phrase(" "));
      emptyCell.setBorder(Rectangle.NO_BORDER);

      Paragraph emptyParagraph = new Paragraph(" ");

      /*
       * First paragraph of the page, included in every page afterwards
       */
      Paragraph pageStartParagraph = new Paragraph();
      PdfPTable headerTable = new PdfPTable(2); // table with 2 columns
      headerTable.setWidths(new int[] {170, 300}); // table column sizes
      headerTable.setWidthPercentage(100);

      // Type cell
      headerTable.addCell(
          new PdfPCell(
              new Phrase(
                  language.get("orderConfirmation") + ":  " + DOC.getFullNumber(), TR_12_B)));

      // language client
      PdfPCell languageClient =
          new PdfPCell(new Phrase(language.get("capitalClient") + " :", TR_10_U_B));
      languageClient.setHorizontalAlignment(Element.ALIGN_RIGHT);
      languageClient.setBorder(Rectangle.NO_BORDER);
      headerTable.addCell(languageClient);

      pageStartParagraph.add(headerTable);

      // CE mark
      if (DOC.isShowCE()) {
        PdfPTable CETable = new PdfPTable(2);
        CETable.setWidthPercentage(100);
        CETable.setWidths(new int[] {25, 445});

        // CE image
        Image ce = null;
        try {
          ce = Image.getInstance(getClass().getResource("/images/ce.png"));
          ce.scaleToFit(24, 24);
          ce.setAlignment(Element.ALIGN_CENTER);
        } catch (Exception e) {
        }

        PdfPCell ceCell = new PdfPCell(ce);
        ceCell.setBorder(Rectangle.NO_BORDER);
        CETable.addCell(ceCell);

        // CE description
        PdfPCell CEspecCell = new PdfPCell(new Phrase(DOC.getCeSpecification(), TR_10));

        CEspecCell.setBorder(Rectangle.NO_BORDER);
        CETable.addCell(CEspecCell);

        pageStartParagraph.add(CETable);
      }

      PdfPTable headerTableSecond = new PdfPTable(2); // table with 2 columns
      headerTableSecond.setWidths(new int[] {170, 300}); // table column sizes
      headerTableSecond.setWidthPercentage(100);

      // Date cell
      PdfPCell dateCell = new PdfPCell(new Phrase(DOC.getFormatedDate(), TR_10));
      dateCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
      dateCell.setBorder(Rectangle.NO_BORDER);
      headerTableSecond.addCell(dateCell);

      // client name cell
      String fullClientName;
      if (DOC.getClient().getSelectedContactPerson().getID() == 0) { // contact person not selected
        fullClientName = DOC.getClient().getName();
      } else {
        fullClientName =
            DOC.getClient().getName() + ", " + DOC.getClient().getSelectedContactPerson().getName();
      }
      PdfPCell clientNameCell = new PdfPCell(new Phrase(fullClientName, TR_10_B));
      clientNameCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
      clientNameCell.setBorder(Rectangle.NO_BORDER);
      headerTableSecond.addCell(clientNameCell);

      // empty cell
      headerTableSecond.addCell(emptyCell);

      // client phone cell
      PdfPCell clientPhoneCell = new PdfPCell(new Phrase(DOC.getClient().getPhone(), TR_10_I));
      clientPhoneCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
      clientPhoneCell.setBorder(Rectangle.NO_BORDER);
      headerTableSecond.addCell(clientPhoneCell);

      // empty cell
      headerTableSecond.addCell(emptyCell);

      // client email cell
      PdfPCell clientEmailCell = new PdfPCell(new Phrase(DOC.getClient().getEmail(), TR_10_I));
      clientEmailCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
      clientEmailCell.setBorder(Rectangle.NO_BORDER);
      headerTableSecond.addCell(clientEmailCell);

      // done with second header table
      pageStartParagraph.add(headerTableSecond);

      /*
       * Let's make a copy for later page checking
       */
      blackBoard = new com.itextpdf.text.Document();

      temporaryByteStream = new ByteArrayOutputStream();
      PdfWriter writer = PdfWriter.getInstance(blackBoard, temporaryByteStream);
      pageEvent.setPageStartParagraph(pageStartParagraph);
      writer.setPageEvent(pageEvent);

      blackBoard.open();

      /*
       * First paragraph
       */
      Paragraph firstParagraph = new Paragraph();

      // thank the customer
      firstParagraph.add(new Phrase(language.get("thanksForBuying"), TR_10_I));

      /*
       * Products header table
       */
      PdfPTable productsHeaderRow;
      int[] productsTableWidths;

      if (DOC.isShowDiscount()) {
        productsHeaderRow = new PdfPTable(7);
        productsTableWidths = new int[] {8, 93, 17, 12, 20, 15, 25};
      } else {
        productsHeaderRow = new PdfPTable(6);
        productsTableWidths = new int[] {8, 110, 17, 12, 20, 23};
      }
      productsHeaderRow.setWidthPercentage(100);
      productsHeaderRow.setWidths(productsTableWidths);

      // products table header row

      PdfPCell headerNRCell = new PdfPCell(new Phrase(language.get("nr"), TR_12));
      headerNRCell.setHorizontalAlignment(Element.ALIGN_CENTER);
      productsHeaderRow.addCell(headerNRCell);

      productsHeaderRow.addCell(new PdfPCell(new Phrase(language.get("specification"), TR_12)));

      PdfPCell headerAmountCell = new PdfPCell(new Phrase(language.get("amount"), TR_12));
      headerAmountCell.setHorizontalAlignment(Element.ALIGN_CENTER);
      productsHeaderRow.addCell(headerAmountCell);

      PdfPCell headerUnitCell = new PdfPCell(new Phrase(language.get("unit"), TR_12));
      headerUnitCell.setHorizontalAlignment(Element.ALIGN_CENTER);
      productsHeaderRow.addCell(headerUnitCell);

      PdfPCell headerPriceCell = new PdfPCell(new Phrase(language.get("price"), TR_12));
      headerPriceCell.setHorizontalAlignment(Element.ALIGN_CENTER);
      productsHeaderRow.addCell(headerPriceCell);

      if (DOC.isShowDiscount()) {
        PdfPCell headerDiscountCell = new PdfPCell(new Phrase(language.get("discount"), TR_12));
        headerDiscountCell.setHorizontalAlignment(Element.ALIGN_CENTER);
        productsHeaderRow.addCell(headerDiscountCell);
      }

      PdfPCell headerSumCell = new PdfPCell(new Phrase(language.get("sum"), TR_12));
      headerSumCell.setHorizontalAlignment(Element.ALIGN_CENTER);
      productsHeaderRow.addCell(headerSumCell);

      // add all the products

      double totalSum = 0;

      int lastPageNumber = 0;
      boolean firstProductInPage = true;
      boolean updateBlackBoard = false;

      for (int i = 0; i < DOC.getProducts().size(); i++) {

        Product product = DOC.getProducts().get(i);

        // check the language and make string accordingly
        String productName = "", productUnit = "";
        if (language.getType().equals(Language.TYPE_ESTONIAN)) {
          productName = product.getName();
          productUnit = product.getUnit();
        } else if (language.getType().equals(Language.TYPE_ENGLISH)) {
          productName = product.getE_name();
          productUnit = product.getE_unit();
        }

        // each products has a separate table with the same size as header
        PdfPTable productRow = new PdfPTable(productsHeaderRow.getNumberOfColumns());
        productRow.setWidthPercentage(100);
        productRow.setWidths(productsTableWidths);

        PdfPCell NRCEll = new PdfPCell(new Phrase("" + (i + 1), TR_10));
        NRCEll.setHorizontalAlignment(Element.ALIGN_CENTER);
        NRCEll.setBorderColor(BaseColor.LIGHT_GRAY);

        // name cell with additional info

        PdfPCell nameCell = null;

        if (product
            .hasAdditionalInformation()) { // user has added additional information for this product

          Paragraph nameAndInfoParagraph = new Paragraph();

          nameAndInfoParagraph.add(new Phrase(productName, TR_10));
          nameAndInfoParagraph.add(Chunk.NEWLINE);
          nameAndInfoParagraph.add(new Phrase(product.getAdditional_Info(), TR_8_I));

          nameCell = new PdfPCell(new Phrase(nameAndInfoParagraph));
        } else {
          nameCell = new PdfPCell(new Phrase(productName, TR_10));
        }
        nameCell.setBorderColor(BaseColor.LIGHT_GRAY);

        // amount cell
        PdfPCell amountCell = new PdfPCell(new Phrase(product.getAmount() + "", TR_10));
        amountCell.setHorizontalAlignment(Element.ALIGN_CENTER);
        amountCell.setBorderColor(BaseColor.LIGHT_GRAY);

        // unit cell
        PdfPCell unitCell = new PdfPCell(new Phrase(productUnit, TR_10));
        unitCell.setHorizontalAlignment(Element.ALIGN_CENTER);
        unitCell.setBorderColor(BaseColor.LIGHT_GRAY);

        // price cell
        PdfPCell priceCell = new PdfPCell(new Phrase(COMMA_3.format(product.getPrice()), TR_10));
        priceCell.setHorizontalAlignment(Element.ALIGN_CENTER);
        priceCell.setBorderColor(BaseColor.LIGHT_GRAY);

        // discount cell
        PdfPCell discountCell = null;
        if (DOC.isShowDiscount()) {
          discountCell = new PdfPCell(new Phrase(DISCOUNT.format(product.getDiscount()), TR_10));
          discountCell.setHorizontalAlignment(Element.ALIGN_CENTER);
          discountCell.setBorderColor(BaseColor.LIGHT_GRAY);
        }

        // sum cell
        PdfPCell sumCell = new PdfPCell(new Phrase(COMMA_2.format(product.getTotalSum()), TR_10));
        sumCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        sumCell.setBorderColor(BaseColor.LIGHT_GRAY);

        // add all the cells
        productRow.addCell(NRCEll);
        productRow.addCell(nameCell);
        productRow.addCell(amountCell);
        productRow.addCell(unitCell);
        productRow.addCell(priceCell);
        if (DOC.isShowDiscount()) {
          productRow.addCell(discountCell);
        }
        productRow.addCell(sumCell);

        // add the product row to the blackboard
        blackBoard.add(productRow);

        /*
         * check if we changed a page with the last product add,
         */

        int currentPageNumber = writer.getPageNumber();

        if (currentPageNumber > lastPageNumber) {

          blackBoard.add(productsHeaderRow); // add the table header again to the new page
          blackBoard.add(emptyParagraph);
          firstProductInPage = true;
          lastPageNumber++;
        }

        if (firstProductInPage) { // first product row needs to have black edges on top

          productRow.deleteLastRow(); // delete the last row and re-add later with changed color

          if (currentPageNumber
              > 1) { // eliminate the possibility of too-long additional info ending up half paged
                     // on old page
            firstParagraph.add(new Chunk(Chunk.NEXTPAGE));
          }

          firstParagraph.add(emptyParagraph); // empty space between the page header
          firstParagraph.add(productsHeaderRow);

          firstProductInPage = false;
          updateBlackBoard = true;

          float borderWidth = NRCEll.getBorderWidth();

          NRCEll.setUseVariableBorders(true);
          nameCell.setUseVariableBorders(true);
          amountCell.setUseVariableBorders(true);
          unitCell.setUseVariableBorders(true);
          priceCell.setUseVariableBorders(true);
          if (DOC.isShowDiscount()) {
            discountCell.setUseVariableBorders(true);
          }
          sumCell.setUseVariableBorders(true);

          NRCEll.setBorderColorTop(BaseColor.BLACK);
          nameCell.setBorderColorTop(BaseColor.BLACK);
          amountCell.setBorderColorTop(BaseColor.BLACK);
          unitCell.setBorderColorTop(BaseColor.BLACK);
          priceCell.setBorderColorTop(BaseColor.BLACK);
          if (DOC.isShowDiscount()) {
            discountCell.setBorderColorTop(BaseColor.BLACK);
          }
          sumCell.setBorderColorTop(BaseColor.BLACK);

          /*
           * this is needed, because SOMEWHY color changing also changes the width
           */
          NRCEll.setBorderWidth(borderWidth / 2);
          nameCell.setBorderWidth(borderWidth / 2);
          amountCell.setBorderWidth(borderWidth / 2);
          unitCell.setBorderWidth(borderWidth / 2);
          priceCell.setBorderWidth(borderWidth / 2);
          if (DOC.isShowDiscount()) {
            discountCell.setBorderWidth(borderWidth / 2);
          }
          sumCell.setBorderWidth(borderWidth / 2);

          productRow.addCell(NRCEll);
          productRow.addCell(nameCell);
          productRow.addCell(amountCell);
          productRow.addCell(unitCell);
          productRow.addCell(priceCell);
          if (DOC.isShowDiscount()) {
            productRow.addCell(discountCell);
          }
          productRow.addCell(sumCell);
        }

        totalSum += product.getTotalSum();

        firstParagraph.add(productRow);

        /*
         *  update the blackBoard, because we added another page to the document, update the page number
         */
        if (updateBlackBoard) {

          blackBoard.close();
          blackBoard = new com.itextpdf.text.Document();

          try {
            temporaryByteStream.close();
          } catch (Exception x) {
          } // close right after done for memory release
          temporaryByteStream = new ByteArrayOutputStream();
          writer = PdfWriter.getInstance(blackBoard, temporaryByteStream);
          pageEvent.resetPagesTotal();
          writer.setPageEvent(pageEvent);

          blackBoard.open();
          blackBoard.add(firstParagraph);
          updateBlackBoard = false;
        }
      }

      /*
       * last paragraph, includes page end information
       */

      Paragraph lastParagraph = new Paragraph();

      /*
       * information table
       */
      PdfPTable infoTable = new PdfPTable(3);
      infoTable.setWidthPercentage(100);
      infoTable.setWidths(new int[] {159, 65, 32});

      Paragraph infoParagraph = new Paragraph();

      // valid due date
      String validDueDate = " ";
      if (DOC.getValidDue() != 0) { // user has set the validDue for the document
        validDueDate =
            language.get("dueDate")
                + "  "
                + sdf.format(
                    new Date(
                        gregCalendar.getTimeInMillis()
                            + (DOC.getValidDue() * 24 * 60 * 60 * 1000L)));
      }

      infoParagraph.add(new Phrase(validDueDate, TR_10));
      infoParagraph.add(Chunk.NEWLINE);

      // instructions for the buyer
      infoParagraph.add(new Phrase(language.get("please") + " ", TR_10));
      infoParagraph.add(new Phrase(DOC.getFullNumber(), TR_10_B));

      PdfPCell infoCell = new PdfPCell(infoParagraph);
      infoCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
      infoTable.addCell(infoCell);

      // the money info
      Paragraph languageParagraph = new Paragraph();

      // language sum
      languageParagraph.add(new Phrase(language.get("sumTotalEuro") + "    :", TR_10));
      languageParagraph.add(Chunk.NEWLINE);

      // language VAT
      languageParagraph.add(new Phrase(language.get("vat") + "    :", TR_10));
      languageParagraph.add(Chunk.NEWLINE);

      // language total
      languageParagraph.add(new Phrase(language.get("totalPay") + "    :", TR_10_B));

      PdfPCell languageCell = new PdfPCell(languageParagraph);
      languageCell.setUseVariableBorders(true);
      languageCell.setBorderColorRight(BaseColor.WHITE);
      languageCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
      infoTable.addCell(languageCell);

      // numbers paragraph
      Paragraph numbersParagaph = new Paragraph();

      // sum
      numbersParagaph.add(new Phrase("" + COMMA_2.format(totalSum), TR_10));
      numbersParagaph.add(Chunk.NEWLINE);

      // VAT 20%
      numbersParagaph.add(new Phrase("" + COMMA_2.format(totalSum * 0.2), TR_10));
      numbersParagaph.add(Chunk.NEWLINE);

      // total
      numbersParagaph.add(new Phrase("" + COMMA_2.format(totalSum + (totalSum * 0.2)), TR_10_B));

      PdfPCell numbersCell = new PdfPCell(numbersParagaph);
      numbersCell.setUseVariableBorders(true);
      numbersCell.setBorderColorLeft(BaseColor.WHITE);
      numbersCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
      infoTable.addCell(numbersCell);

      lastParagraph.add(infoTable);

      int pageNumberAfterProducts = writer.getPageNumber();

      /*
       * shipment data
       */
      PdfPTable shipmentTable = new PdfPTable(2);
      shipmentTable.setWidthPercentage(100);
      shipmentTable.setWidths(new int[] {30, 140});

      Paragraph shipLangParagraph = new Paragraph();

      // time
      shipLangParagraph.add(new Phrase(language.get("shipmentTime") + ":", TR_10_B));
      shipLangParagraph.add(Chunk.NEWLINE);

      // address
      shipLangParagraph.add(new Phrase(language.get("shipmentAddress") + ":", TR_10_B));
      shipLangParagraph.add(Chunk.NEWLINE);

      // payment requirement
      shipLangParagraph.add(new Phrase(language.get("paymentRequirement") + ":", TR_10_B));

      // add to the table
      PdfPCell shipLangCell = new PdfPCell(shipLangParagraph);
      shipLangCell.setBorder(Rectangle.NO_BORDER);
      shipmentTable.addCell(shipLangCell);

      // document shipment data

      Paragraph shipmentParagraph = new Paragraph();

      // time
      shipmentParagraph.add(new Phrase(DOC.getShipmentTime(), TR_10));
      shipmentParagraph.add(Chunk.NEWLINE);

      // place
      shipmentParagraph.add(new Phrase(DOC.getShipmentAddress(), TR_10));
      shipmentParagraph.add(Chunk.NEWLINE);

      // address
      shipmentParagraph.add(new Phrase(DOC.getPaymentRequirement(), TR_10));

      // add to the table
      PdfPCell shipmentCell = new PdfPCell(shipmentParagraph);
      shipmentCell.setBorder(Rectangle.NO_BORDER);

      shipmentTable.addCell(shipmentCell);

      lastParagraph.add(shipmentTable);

      /*
       * changeable data for user
       */

      lastParagraph.add(new Paragraph(language.get("orderSpecification"), TR_8_I));

      /*
       * creator data
       */

      Paragraph creatorParagraph = new Paragraph();

      creatorParagraph.add(new Phrase(language.get("orderCreator") + ":  ", TR_10_B));

      if (!user.getName().equals("")) {
        creatorParagraph.add(new Phrase(user.getName(), TR_10));
      }
      if (!user.getPhone().equals("")) {
        creatorParagraph.add(new Phrase(";  tel  " + user.getPhone(), TR_10));
      }
      if (!user.getEmail().equals("")) {
        creatorParagraph.add(new Phrase(";  e-mail:  " + user.getEmail(), TR_10));
      }
      if (!user.getSkype().equals("")) {
        creatorParagraph.add(new Phrase(";  skype:  " + user.getSkype(), TR_10));
      }

      lastParagraph.add(creatorParagraph);
      lastParagraph.add(emptyParagraph);

      /*
       * signature, client name and date
       */

      Paragraph agreementParagraph = new Paragraph(language.get("agreeOrder") + ":", TR_10);
      agreementParagraph.setAlignment(Rectangle.ALIGN_CENTER);
      lastParagraph.add(agreementParagraph);

      lastParagraph.add(emptyParagraph);

      // input data table
      PdfPTable receiverTable = new PdfPTable(3);

      // te dots for data input
      PdfPCell dotsCell =
          new PdfPCell(new Phrase("............................................", TR_10_B));
      dotsCell.setHorizontalAlignment(Rectangle.ALIGN_CENTER);
      dotsCell.setBorder(Rectangle.NO_BORDER);

      receiverTable.addCell(dotsCell);
      receiverTable.addCell(dotsCell);
      receiverTable.addCell(dotsCell);

      // date
      PdfPCell receiverDateCell = new PdfPCell(new Phrase(language.get("date"), TR_10));
      receiverDateCell.setHorizontalAlignment(Rectangle.ALIGN_CENTER);
      receiverDateCell.setBorder(Rectangle.NO_BORDER);
      receiverTable.addCell(receiverDateCell);

      PdfPCell receiverNameCell = new PdfPCell(new Phrase(language.get("capitalClient"), TR_10));
      receiverNameCell.setHorizontalAlignment(Rectangle.ALIGN_CENTER);
      receiverNameCell.setBorder(Rectangle.NO_BORDER);
      receiverTable.addCell(receiverNameCell);

      PdfPCell receiverSignatureCell = new PdfPCell(new Phrase(language.get("signature"), TR_10));
      receiverSignatureCell.setHorizontalAlignment(Rectangle.ALIGN_CENTER);
      receiverSignatureCell.setBorder(Rectangle.NO_BORDER);
      receiverTable.addCell(receiverSignatureCell);

      lastParagraph.add(receiverTable);

      /*
       * finally add all the info to the document
       */

      blackBoard.add(lastParagraph);

      try {
        blackBoard.close();
      } catch (Exception x) {
      }
      reader = new PdfReader(temporaryByteStream.toByteArray());
      try {
        temporaryByteStream.close();
      } catch (Exception x) {
      } // close right after done for memory release
      int pageNumberAfterFinalEnd = reader.getNumberOfPages();

      /*
       * now we make the final and last document that is ready to be sent to user
       */

      finalDocument = new com.itextpdf.text.Document();

      byteOutputStream = new ByteArrayOutputStream();
      writer = PdfWriter.getInstance(finalDocument, byteOutputStream);
      pageEvent.setPageCount(pageEvent.getPageCount(), true);
      writer.setPageEvent(pageEvent);

      finalDocument.open();

      /*
       * check the last paragraph fit for our document
       */
      if (pageNumberAfterFinalEnd
          > pageNumberAfterProducts) { // add the final paragraph to the new page
        finalDocument.add(firstParagraph);
        finalDocument.newPage();
        finalDocument.add(emptyParagraph);
        finalDocument.add(lastParagraph);
      } else {
        finalDocument.add(firstParagraph);
        if (DOC.getProducts().size()
            == 0) { // we have no products, just to make it look nicer, add a blank
          finalDocument.add(emptyParagraph);
        }
        finalDocument.add(lastParagraph);
      }

    } catch (Exception x) {
      x.printStackTrace();
      wasError = true;
    } finally {
      try {
        temporaryByteStream.close();
      } catch (Exception x) {
      }
      try {
        byteOutputStream.close();
      } catch (Exception x) {
      }
      try {
        blackBoard.close();
      } catch (Exception x) {
      }
      try {
        finalDocument.close();
      } catch (Exception x) {
      }
    }

    if (wasError) {
      return null;
    }
    if (byteOutputStream == null) {
      return null;
    }

    return byteOutputStream.toByteArray();
  }