예제 #1
0
  public static void createPdf2(
      List<Participant> participants,
      boolean exportName,
      boolean exportGroup,
      boolean exportRenseignement,
      OutputStream out)
      throws IOException, DocumentException {
    Document document = new Document(PageSize.A4.rotate());
    // document.setMargins(0,0,0,0);
    PdfWriter writer = PdfWriter.getInstance(document, out);
    document.open();
    PdfContentByte cb = writer.getDirectContent();

    float documentTop = document.top();
    float documentBottom = document.bottom();
    float documentHeight = documentTop - documentBottom;
    float left = document.left();
    float right = document.right();
    float width = right - left;

    cb.rectangle(left, documentBottom, width, documentHeight);
    cb.stroke();

    float nameHeightPercent = 0.35f;
    float groupHeightPercent = 0.25f;

    float nameTop = documentTop;
    float nameBottom = nameTop;
    if (exportName) {
      nameBottom = nameTop - (documentHeight * nameHeightPercent);
    }
    float groupeTop = nameBottom;
    float groupeBottom = nameBottom;
    if (exportGroup) {
      groupeBottom = groupeTop - (documentHeight * groupHeightPercent);
    }
    float barcodeTop = groupeBottom;
    float barcodeBottom = documentBottom;

    ColumnText columnText;

    for (Participant participant : participants) {

      float nameFontSize = 65f;
      float groupFontSize = 45f;
      float renseignementFontSize = 35f;

      if (exportName) {
        columnText = new ColumnText(cb);

        columnText.setSimpleColumn(left, nameTop, right, nameBottom);
        // cb.rectangle(left, nameBottom, width, (nameTop - nameBottom));
        // cb.stroke();

        columnText.setExtraParagraphSpace(0f);
        columnText.setAdjustFirstLine(false);
        columnText.setIndent(0);

        String txt = participant.getNom().toUpperCase() + " " + participant.getPrenom();

        float previousPos = columnText.getYLine();
        columnText.setText(null);
        columnText.addElement(createCleanParagraph(txt, nameFontSize, true));
        while (ColumnText.hasMoreText(columnText.go(true))) {
          nameFontSize = nameFontSize - 0.5f;
          columnText.setText(null);
          columnText.addElement(createCleanParagraph(txt, nameFontSize, true));
          columnText.setYLine(previousPos);
        }

        columnText.setText(null);
        columnText.addElement(createCleanParagraph(txt, nameFontSize, true));
        columnText.setYLine(previousPos);
        columnText.go(false);
      }

      if (exportGroup) {
        columnText = new ColumnText(cb);

        columnText.setSimpleColumn(document.left(), groupeTop, document.right(), groupeBottom);
        float groupeHeight = groupeTop - groupeBottom;
        // cb.rectangle(document.left(), groupeTop - groupeHeight, document.right() -
        // document.left(), groupeHeight);
        // cb.stroke();

        columnText.setExtraParagraphSpace(0f);
        columnText.setAdjustFirstLine(false);
        columnText.setIndent(0);
        columnText.setFollowingIndent(0);

        String txt1 = participant.getGroupe();
        String txt2 = exportRenseignement ? participant.getRenseignements() : null;

        float previousPos = columnText.getYLine();
        columnText.setText(null);
        // columnText.addElement(createCleanParagraph(txt1,groupFontSize,true,txt2,renseignementFontSize,false));
        columnText.addElement(createCleanParagraph(txt1, groupFontSize, true));
        columnText.addElement(createCleanParagraph(txt2, renseignementFontSize, false));
        while (ColumnText.hasMoreText(columnText.go(true))) {
          groupFontSize = groupFontSize - 0.5f;
          renseignementFontSize = renseignementFontSize - 0.5f;
          columnText.setText(null);
          // columnText.addElement(createCleanParagraph(txt1,groupFontSize,true,txt2,renseignementFontSize,false));
          columnText.addElement(createCleanParagraph(txt1, groupFontSize, true));
          columnText.addElement(createCleanParagraph(txt2, renseignementFontSize, false));
          columnText.setYLine(previousPos);
        }

        columnText.setText(null);
        // columnText.addElement(createCleanParagraph(txt1,groupFontSize,true,txt2,renseignementFontSize,false));
        columnText.addElement(createCleanParagraph(txt1, groupFontSize, true));
        columnText.addElement(createCleanParagraph(txt2, renseignementFontSize, false));
        columnText.setYLine(previousPos);
        columnText.go(false);
      }

      {
        columnText = new ColumnText(cb);

        barcodeTop = barcodeTop - 12f;
        columnText.setSimpleColumn(left, barcodeTop, right, barcodeBottom);
        float barcodeHeight = barcodeTop - barcodeBottom;
        // cb.rectangle(left, barcodeTop - barcodeHeight, width, barcodeHeight);
        // cb.stroke();

        columnText.setExtraParagraphSpace(0f);
        columnText.setAdjustFirstLine(false);
        columnText.setIndent(0);

        float previousPos = columnText.getYLine();
        columnText.setText(null);
        columnText.addElement(
            createCleanBarcode(cb, participant.getNumero(), width, barcodeHeight));
        columnText.go(false);
      }

      document.newPage();
    }

    document.close();
  }