Esempio n. 1
0
  public static float fitText(
      Font font, String text, Rectangle rect, float maxFontSize, boolean newlinesAllowed) {
    float minFontSize = 1f;
    float potentialFontSize;
    while (true) {
      potentialFontSize = (maxFontSize + minFontSize) / 2.0f;
      font.setSize(potentialFontSize);

      LinkedList<Chunk> lineChunks = splitScrambleToLineChunks(text, font, rect.getWidth());
      if (!newlinesAllowed && lineChunks.size() > 1) {
        // If newlines are not allowed, and we had to split the text into more than
        // one line, then potentialFontSize is too large.
        maxFontSize = potentialFontSize;
      } else {
        // The font size seems to be a pretty good estimate for how
        // much vertical space a row actually takes up.
        float totalHeight = lineChunks.size() * potentialFontSize;
        if (totalHeight < rect.getHeight()) {
          minFontSize = potentialFontSize;
        } else {
          maxFontSize = potentialFontSize;
        }
      }
      if (maxFontSize - minFontSize < FITTEXT_FONTSIZE_PRECISION) {
        // Err on the side of too small, because being too large will screw up
        // layout.
        potentialFontSize = minFontSize;
        break;
      }
    }
    return potentialFontSize;
  }
Esempio n. 2
0
  private static void booklet(String input) throws Exception {
    String output = input.replace(".pdf", "-booklet.pdf");
    PdfReader reader = new PdfReader(input);
    int n = reader.getNumberOfPages();
    Rectangle pageSize = reader.getPageSize(1);

    System.out.println("Input page size: " + pageSize);
    Document doc = new Document(PageSize.A4.rotate(), 0, 0, 0, 0);
    PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(output));
    doc.open();
    splitLine(doc, writer);
    int[] pages = new int[(n + 3) / 4 * 4];
    int x = 1, y = pages.length;
    for (int i = 0; i < pages.length; ) {
      pages[i++] = y--;
      pages[i++] = x++;
      pages[i++] = x++;
      pages[i++] = y--;
    }
    PdfContentByte cb = writer.getDirectContent();
    float bottom = (doc.top() - pageSize.getHeight()) / 2 + kOffset;
    float left = doc.right() / 2 - (pageSize.getWidth() + kTextWidth) / 2 - kMargin;
    float right = doc.right() / 2 - (pageSize.getWidth() - kTextWidth) / 2 + kMargin;

    for (int i = 0; i < pages.length; ) {
      PdfImportedPage page = getPage(writer, reader, pages[i++]);
      if (page != null) cb.addTemplate(page, left, bottom);

      page = getPage(writer, reader, pages[i++]);
      if (page != null) cb.addTemplate(page, right, bottom);

      doc.newPage();
    }
    doc.close();
  }
Esempio n. 3
0
 private void exportGraphToPdf(mxGraph graph, String filename) {
   Rectangle bounds =
       new Rectangle(trackScheme.getGUI().graphComponent.getViewport().getViewSize());
   // step 1
   com.itextpdf.text.Rectangle pageSize =
       new com.itextpdf.text.Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
   com.itextpdf.text.Document document = new com.itextpdf.text.Document(pageSize);
   // step 2
   PdfWriter writer = null;
   Graphics2D g2 = null;
   try {
     writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
     // step 3
     document.open();
     // step 4
     PdfContentByte canvas = writer.getDirectContent();
     g2 = canvas.createGraphics(pageSize.getWidth(), pageSize.getHeight());
     trackScheme.getGUI().graphComponent.getViewport().paintComponents(g2);
   } catch (FileNotFoundException e) {
     e.printStackTrace();
   } catch (DocumentException e) {
     e.printStackTrace();
   } finally {
     g2.dispose();
     document.close();
   }
 }
Esempio n. 4
0
  //	---------------- for footer and header....------------------
  @Override
  public void onEndPage(PdfWriter writer, Document document) {

    try {

      Rectangle page = document.getPageSize();

      // we will print the header using the code below

      PdfPTable headTable = getHeader();
      headTable.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());

      // the table is printed at the spcific location

      headTable.writeSelectedRows(0, -1, document.leftMargin(), 830, writer.getDirectContent());
      // we will print the footer using the code below

      PdfPTable footTable = getFooter();

      footTable.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());

      footTable.writeSelectedRows(
          0, -1, document.leftMargin(), document.bottomMargin() - 560, writer.getDirectContent());

    } catch (Exception ex) {

      ex.printStackTrace();
    }

    // TODO Auto-generated method stub

  }
 private void applyPadding(Rectangle r, boolean isLeft) {
   if (isLeft) {
     r.setRight(r.getRight() - hPadding);
   } else {
     r.setLeft(r.getLeft() + hPadding);
   }
 }
Esempio n. 6
0
 // FIXME: Manage Exceptions
 private OutputStream marcarReporteProgramaRechazado(final Programa programa, InputStream src)
     throws IOException, DocumentException {
   PdfReader reader = null;
   reader = new PdfReader(src);
   int n = reader.getNumberOfPages();
   ByteArrayOutputStream os = new ByteArrayOutputStream();
   PdfStamper stamper = null;
   stamper = new PdfStamper(reader, os);
   // text watermark
   Font f = new Font(Font.FontFamily.HELVETICA, 65);
   Phrase p = new Phrase("RECHAZADO", f);
   // transparency
   PdfGState gs1 = new PdfGState();
   gs1.setFillOpacity(0.3f);
   // properties
   PdfContentByte over;
   Rectangle pagesize;
   float x, y;
   // loop over every page
   for (int i = 1; i <= n; i++) {
     pagesize = reader.getPageSizeWithRotation(i);
     x = (pagesize.getLeft() + pagesize.getRight()) / 2;
     y = (pagesize.getTop() + pagesize.getBottom()) / 2;
     over = stamper.getOverContent(i);
     over.saveState();
     over.setGState(gs1);
     ColumnText.showTextAligned(over, Element.ALIGN_CENTER, p, x, y, 45);
   }
   stamper.close();
   reader.close();
   return os;
 }
Esempio n. 7
0
 public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvas) {
   PdfContentByte cb = canvas[PdfPTable.BACKGROUNDCANVAS];
   cb.roundRectangle(
       rect.getLeft() + 1.5f,
       rect.getBottom() + 1.5f,
       rect.getWidth() - 3,
       rect.getHeight() - 3,
       4);
   cb.setCMYKColorFill(0x00, 0x00, 0x00, 0x00);
   cb.fill();
 }
 public void onEndPage(PdfWriter writer, Document document) {
   Rectangle rect = writer.getBoxSize("art");
   // ColumnText.showTextAligned(writer.getDirectContent(),Element.ALIGN_CENTER, new
   // Phrase(grupo), rect.getRight(), rect.getBottom(), 0);
   ColumnText.showTextAligned(
       writer.getDirectContent(),
       Element.ALIGN_CENTER,
       new Phrase("Pagina " + pages),
       rect.getLeft(),
       rect.getBottom(),
       0);
 }
Esempio n. 9
0
 public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvas) {
   PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
   cb.roundRectangle(
       rect.getLeft() + 1.5f,
       rect.getBottom() + 1.5f,
       rect.getWidth() - 3,
       rect.getHeight() - 3,
       4);
   cb.setLineWidth(1.5f);
   cb.setCMYKColorStrokeF(color[0], color[1], color[2], color[3]);
   cb.stroke();
 }
 /**
  * Draws the info about the movie.
  *
  * @throws DocumentException
  */
 protected void drawMovieInfo(Screening screening, PdfContentByte directcontent)
     throws DocumentException {
   if (screening.isPress()) {
     Rectangle rect = getPosition(screening);
     ColumnText.showTextAligned(
         directcontent,
         Element.ALIGN_CENTER,
         press,
         (rect.getLeft() + rect.getRight()) / 2,
         rect.getBottom() + rect.getHeight() / 4,
         0);
   }
 }
Esempio n. 11
0
 public void addColumn(PdfWriter writer, Rectangle rect, boolean useAscender)
     throws DocumentException {
   rect.setBorder(Rectangle.BOX);
   rect.setBorderWidth(0.5f);
   rect.setBorderColor(BaseColor.RED);
   PdfContentByte cb = writer.getDirectContent();
   cb.rectangle(rect);
   Phrase p = new Phrase("This text is added at the top of the column.");
   ColumnText ct = new ColumnText(cb);
   ct.setSimpleColumn(rect);
   ct.setUseAscender(useAscender);
   ct.addText(p);
   ct.go();
 }
Esempio n. 12
0
  public void process(ObjectIO input, ObjectIO output) {

    try {

      // OJO pasarle el idemisor

      String idemisor = "1";

      recuperaInput(input);

      Rectangle pageSize = new Rectangle(PageSize.A4);
      // pageSize.setBackgroundColor(Color.TRANSLUCENT);
      pageSize.rotate();
      documento = new Document(pageSize);

      System.out.println("WIDTH " + documento.getPageSize().getWidth());
      System.out.println("HEIGHT " + documento.getPageSize().getHeight());

      namefile = "BarCode_" + idemisor + "_codigoBARRAS.pdf";
      filecrea = "C://DATOS//LRA//tmp//" + namefile;
      // filecrea = PropiedadesJLet.getInstance().getProperty("path.gen.recibo") +"emisor_"+
      // idemisor +"/"+ namefile;
      System.out.println(
          "***************************Se va a crear este codigo de barras: " + filecrea);
      writer = PdfWriter.getInstance(documento, new FileOutputStream(filecrea));
      documento.open();

      pintaCodigoBarras();

      absText(writer, texto1xx, xpostex1, ypostex1, 8);

      absText(writer, texto2xx, xpostex2, ypostex2, 8);

      try {
        output.setValue("idemisor", idemisor);
        output.setValue("filecrea", namefile);
        output.setValue("txmensaj", "Generacion Ok - ");
      } catch (Exception e) {
        e.printStackTrace();
      }

    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    } finally {
      documento.close();
    }
  }
Esempio n. 13
0
 /**
  * Gets the X value for centering the watermark image
  *
  * @param r rectangle
  * @param img image
  * @return
  */
 protected static float getCenterX(final Rectangle r, final Image img) {
   float x = 0;
   final float pdfwidth = r.getWidth();
   final float imgwidth = img.getWidth();
   x = (pdfwidth - imgwidth) / 2;
   return x;
 }
Esempio n. 14
0
 /**
  * Gets the Y value for centering the watermark image
  *
  * @param r rectangle
  * @param img image
  * @return
  */
 protected static float getCenterY(final Rectangle r, final Image img) {
   float y = 0;
   final float pdfheight = r.getHeight();
   final float imgheight = img.getHeight();
   y = (pdfheight - imgheight) / 2;
   return y;
 }
  public boolean fits() {
    final boolean simulate = true;

    // try adding into a single infinite column to calc height
    ColumnTextBuilder ctb = b.reuseColumnTextBuilder();

    b.reuseRectangleBuilder(tempR).copyPositionsFrom(rectangle);

    tempR.setBottom(-10000f);
    tempR.setRight(horCenter());

    applyPadding(tempR, true);

    ctb.setSimpleColumn(tempR);

    float yBefore = ctb.getYLine();

    contentAdder.add(ctb, simulate);

    int status = ctb.go(simulate);

    if (ColumnText.hasMoreText(status)) {
      return false;
    }

    float yAfter = ctb.getYLine();

    final float height = yBefore - yAfter;

    logger.trace("height: {}", height);

    //        if(height > rectangle.getHeight()) {
    //            return false;
    //        }

    // now try adding into two actual columns
    final float columnHeight = height / 2 + additionalSpaceForColumn;

    if (!addToTwoColumns(simulate, columnHeight)) {
      return false;
    }

    addToTwoColumns(false, columnHeight);

    return true;
  }
Esempio n. 16
0
 public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
   try {
     PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
     PdfPatternPainter patternPainter =
         cb.createPattern(image.getScaledWidth(), image.getScaledHeight());
     image.setAbsolutePosition(0, 0);
     patternPainter.addImage(image);
     cb.saveState();
     cb.setPatternFill(patternPainter);
     cb.rectangle(
         position.getLeft(), position.getBottom(), position.getWidth(), position.getHeight());
     cb.fill();
     cb.restoreState();
   } catch (DocumentException e) {
     throw new ExceptionConverter(e);
   }
 }
Esempio n. 17
0
 /**
  * Implementation of the cellLayout method.
  *
  * @see com.itextpdf.text.pdf.PdfPCellEvent#cellLayout( com.itextpdf.text.pdf.PdfPCell,
  *     com.itextpdf.text.Rectangle, com.itextpdf.text.pdf.PdfContentByte[])
  */
 public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
   try {
     PdfAnnotation annotation =
         PdfAnnotation.createFileAttachment(
             writer,
             new Rectangle(
                 position.getLeft() - 20,
                 position.getTop() - 15,
                 position.getLeft() - 5,
                 position.getTop() - 5),
             description,
             fs);
     annotation.setName(description);
     writer.addAnnotation(annotation);
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
  /**
   * 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();
  }
  private void setColumn(
      float height, float horCenter, boolean left, ColumnTextBuilder ctb, boolean simulate) {
    b.reuseRectangleBuilder(tempR).copyPositionsFrom(rectangle);

    tempR.setBottom(tempR.getTop() - height);

    if (left) {
      tempR.setRight(horCenter);
    } else {
      tempR.setLeft(horCenter);
      tempR.setBottom(-10000f);
    }

    applyPadding(tempR, left);

    logger.debug("setting column to: {}", tempR);

    ctb.setSimpleColumn(tempR);
  }
Esempio n. 20
0
 /**
  * Create a pushbutton for a key
  *
  * @param writer the PdfWriter
  * @param rect the position of the key
  * @param btn the label for the key
  * @param script the script to be executed when the button is pushed
  */
 public void addPushButton(PdfWriter writer, Rectangle rect, String btn, String script) {
   float w = rect.getWidth();
   float h = rect.getHeight();
   PdfFormField pushbutton = PdfFormField.createPushButton(writer);
   pushbutton.setFieldName("btn_" + btn);
   pushbutton.setWidget(rect, PdfAnnotation.HIGHLIGHT_PUSH);
   PdfContentByte cb = writer.getDirectContent();
   pushbutton.setAppearance(
       PdfAnnotation.APPEARANCE_NORMAL, createAppearance(cb, btn, BaseColor.GRAY, w, h));
   pushbutton.setAppearance(
       PdfAnnotation.APPEARANCE_ROLLOVER, createAppearance(cb, btn, BaseColor.RED, w, h));
   pushbutton.setAppearance(
       PdfAnnotation.APPEARANCE_DOWN, createAppearance(cb, btn, BaseColor.BLUE, w, h));
   pushbutton.setAdditionalActions(PdfName.U, PdfAction.javaScript(script, writer));
   pushbutton.setAdditionalActions(
       PdfName.E, PdfAction.javaScript("this.showMove('" + btn + "');", writer));
   pushbutton.setAdditionalActions(PdfName.X, PdfAction.javaScript("this.showMove(' ');", writer));
   writer.addAnnotation(pushbutton);
 }
Esempio n. 21
0
  private static void crop(String input) throws Exception {
    String output = input.replace(".pdf", "-crop.pdf");
    PdfReader reader = new PdfReader(input);
    final int n = reader.getNumberOfPages();
    Rectangle pageSize = reader.getPageSize(1);

    System.out.println("Input page size: " + pageSize);
    float left = (pageSize.getWidth() - kTextWidth) / 2 - kMargin;
    float right = pageSize.getWidth() - left;
    float bottom = (pageSize.getHeight() - kTextHeight) / 2;
    float top = pageSize.getHeight() - bottom;
    PdfRectangle rect = new PdfRectangle(left, bottom + kOffset, right, top + kOffset);
    for (int i = 1; i <= n; i++) {
      PdfDictionary pageDict = reader.getPageN(i);
      pageDict.put(PdfName.CROPBOX, rect);
    }
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(output));
    stamper.close();
  }
Esempio n. 22
0
 void applyRotation(PdfDictionary pageN, ByteBuffer out) {
   if (!cstp.rotateContents) return;
   Rectangle page = reader.getPageSizeWithRotation(pageN);
   int rotation = page.getRotation();
   switch (rotation) {
     case 90:
       out.append(PdfContents.ROTATE90);
       out.append(page.getTop());
       out.append(' ').append('0').append(PdfContents.ROTATEFINAL);
       break;
     case 180:
       out.append(PdfContents.ROTATE180);
       out.append(page.getRight());
       out.append(' ');
       out.append(page.getTop());
       out.append(PdfContents.ROTATEFINAL);
       break;
     case 270:
       out.append(PdfContents.ROTATE270);
       out.append('0').append(' ');
       out.append(page.getRight());
       out.append(PdfContents.ROTATEFINAL);
       break;
   }
 }
Esempio n. 23
0
    public PDFRenderTargetImpl(
        PdfContentByte cb,
        double width,
        double height,
        double topLeftX,
        double topLeftY,
        Rectangle size,
        float marginLeft,
        float marginRight,
        float marginTop,
        float marginBottom,
        boolean landscape) {
      this.cb = cb;
      this.marginTop = marginTop;
      this.marginLeft = marginLeft;
      this.marginBottom = marginBottom;
      this.marginRight = marginRight;
      this.pageSize = size;
      this.landscape = landscape;

      double centerX = topLeftX + width / 2;
      double centerY = topLeftY + height / 2;

      // Transform
      double pageWidth = size.getWidth() - marginLeft - marginRight;
      double pageHeight = size.getHeight() - marginTop - marginBottom;
      double ratioWidth = pageWidth / width;
      double ratioHeight = pageHeight / height;
      double scale = (float) (ratioWidth < ratioHeight ? ratioWidth : ratioHeight);
      double translateX = (marginLeft + pageWidth / 2.) / scale;
      double translateY = (marginBottom + pageHeight / 2.) / scale;
      cb.transform(AffineTransform.getTranslateInstance(-centerX * scale, centerY * scale));
      cb.transform(AffineTransform.getScaleInstance(scale, scale));
      cb.transform(AffineTransform.getTranslateInstance(translateX, translateY));

      FontFactory.register("/org/gephi/preview/fonts/LiberationSans.ttf", "ArialMT");
    }
Esempio n. 24
0
 /**
  * Create a rectangle for the visible signature using the selected position and signature size
  *
  * @param position
  * @param width
  * @param height
  * @return
  */
 private static Rectangle positionSignature(
     final String position,
     final Rectangle pageRect,
     final int width,
     final int height,
     final int marginX,
     final int marginY) {
   final float pageHeight = pageRect.getHeight();
   final float pageWidth = pageRect.getWidth();
   Rectangle r = null;
   if (position.equals(DigitalSigningDTO.POSITION_BOTTOMLEFT)) {
     r = new Rectangle(marginX, marginY, width + marginX, height + marginY);
   } else if (position.equals(DigitalSigningDTO.POSITION_BOTTOMRIGHT)) {
     r =
         new Rectangle(
             pageWidth - width - marginX, height + marginY, pageWidth - marginX, marginY);
   } else if (position.equals(DigitalSigningDTO.POSITION_TOPLEFT)) {
     r =
         new Rectangle(
             marginX, pageHeight - marginY, width + marginX, pageHeight - height - marginY);
   } else if (position.equals(DigitalSigningDTO.POSITION_TOPRIGHT)) {
     r =
         new Rectangle(
             pageWidth - width - marginX,
             pageHeight - marginY,
             pageWidth - marginX,
             pageHeight - height - marginY);
   } else if (position.equals(DigitalSigningDTO.POSITION_CENTER)) {
     r =
         new Rectangle(
             (pageWidth / 2) - (width / 2),
             (pageHeight / 2) - (height / 2),
             (pageWidth / 2) + (width / 2),
             (pageHeight / 2) + (height / 2));
   }
   return r;
 }
  private boolean addToTwoColumns(boolean simulate, float height) {
    ColumnTextBuilder ctb;
    int status;
    ctb = b.reuseColumnTextBuilder();

    b.reuseRectangleBuilder(tempR).copyPositionsFrom(rectangle);

    final float horCenter = horCenter();

    setColumn(height, horCenter, true, ctb, simulate);

    contentAdder.add(ctb, simulate);

    status = ctb.go(simulate);

    yLine = ctb.getYLine();

    if (!ColumnText.hasMoreText(status) && simulate) {
      return true;
    }

    setColumn(height, horCenter, false, ctb, simulate);

    status = ctb.go(simulate);

    yLine = Math.min(yLine, ctb.getYLine());

    // right column may not fit the rect, check if does fit the page
    if (simulate) {
      if (yLine < rectangle.getBottom()) {
        return false;
      }
    }

    return !ColumnText.hasMoreText(status);
  }
 public TwoColumnsSectionBuilder newPage(float pageTop, float pageBottom) {
   rectangle.setTop(pageTop);
   rectangle.setBottom(pageBottom);
   return this;
 }
Esempio n. 27
0
  private static PdfReader createPdf(
      String globalTitle, Date creationDate, ScrambleRequest scrambleRequest)
      throws DocumentException, IOException {
    azzert(scrambleRequest.scrambles.length > 0);
    ByteArrayOutputStream pdfOut = new ByteArrayOutputStream();
    Rectangle pageSize = PageSize.LETTER;
    Document doc = new Document(pageSize, 0, 0, 75, 75);
    PdfWriter docWriter = PdfWriter.getInstance(doc, pdfOut);

    docWriter.setBoxSize(
        "art", new Rectangle(36, 54, pageSize.getWidth() - 36, pageSize.getHeight() - 54));

    doc.addCreationDate();
    doc.addProducer();
    if (globalTitle != null) {
      doc.addTitle(globalTitle);
    }

    doc.open();
    // Note that we ignore scrambleRequest.copies here.
    addScrambles(docWriter, doc, scrambleRequest, globalTitle);
    doc.close();

    // TODO - is there a better way to convert from a PdfWriter to a PdfReader?
    PdfReader pr = new PdfReader(pdfOut.toByteArray());
    if (scrambleRequest.fmc) {
      // We don't watermark the FMC sheets because they already have
      // the competition name on them.
      return pr;
    }

    pdfOut = new ByteArrayOutputStream();
    doc = new Document(pageSize, 0, 0, 75, 75);
    docWriter = PdfWriter.getInstance(doc, pdfOut);
    doc.open();

    PdfContentByte cb = docWriter.getDirectContent();

    for (int pageN = 1; pageN <= pr.getNumberOfPages(); pageN++) {
      PdfImportedPage page = docWriter.getImportedPage(pr, pageN);

      doc.newPage();
      cb.addTemplate(page, 0, 0);

      Rectangle rect = pr.getBoxSize(pageN, "art");

      // Header
      ColumnText.showTextAligned(
          cb,
          Element.ALIGN_LEFT,
          new Phrase(Utils.SDF.format(creationDate)),
          rect.getLeft(),
          rect.getTop(),
          0);

      ColumnText.showTextAligned(
          cb,
          Element.ALIGN_CENTER,
          new Phrase(globalTitle),
          (pageSize.getLeft() + pageSize.getRight()) / 2,
          pageSize.getTop() - 60,
          0);

      ColumnText.showTextAligned(
          cb,
          Element.ALIGN_CENTER,
          new Phrase(scrambleRequest.title),
          (pageSize.getLeft() + pageSize.getRight()) / 2,
          pageSize.getTop() - 45,
          0);

      if (pr.getNumberOfPages() > 1) {
        ColumnText.showTextAligned(
            cb,
            Element.ALIGN_RIGHT,
            new Phrase(pageN + "/" + pr.getNumberOfPages()),
            rect.getRight(),
            rect.getTop(),
            0);
      }

      // Footer
      String generatedBy = "Generated by " + Utils.getProjectName() + "-" + Utils.getVersion();
      ColumnText.showTextAligned(
          cb,
          Element.ALIGN_CENTER,
          new Phrase(generatedBy),
          (pageSize.getLeft() + pageSize.getRight()) / 2,
          pageSize.getBottom() + 40,
          0);
    }

    doc.close();

    // TODO - is there a better way to convert from a PdfWriter to a PdfReader?
    pr = new PdfReader(pdfOut.toByteArray());
    return pr;

    //      The PdfStamper class doesn't seem to be working.
    //      pdfOut = new ByteArrayOutputStream();
    //      PdfStamper ps = new PdfStamper(pr, pdfOut);
    //
    //      for(int pageN = 1; pageN <= pr.getNumberOfPages(); pageN++) {
    //          PdfContentByte pb = ps.getUnderContent(pageN);
    //          Rectangle rect = pr.getBoxSize(pageN, "art");
    //          System.out.println(rect.getLeft());
    //          System.out.println(rect.getWidth());
    //          ColumnText.showTextAligned(pb,
    //                  Element.ALIGN_LEFT, new Phrase("Hello people!"), 36, 540, 0);
    ////            ColumnText.showTextAligned(pb,
    ////                    Element.ALIGN_CENTER, new Phrase("HELLO WORLD"),
    ////                    (rect.getLeft() + rect.getRight()) / 2, rect.getTop(), 0);
    //      }
    //      ps.close();
    //      return ps.getReader();
  }
  // --- GENERATE PDF DOCUMENT BYTES -------------------------------------------------------------
  public ByteArrayOutputStream generatePDFDocumentBytes(
      final HttpServletRequest req, StringBuffer patientids, String printlanguage)
      throws Exception {
    sPrintLanguage = printlanguage;
    ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
    docWriter = PdfWriter.getInstance(doc, baosPDF);
    docWriter.setPageEvent(new AMCEndPage());
    this.req = req;

    String sURL = req.getRequestURL().toString();
    if (sURL.indexOf("openclinic/") > 0) {
      sURL = sURL.substring(0, sURL.indexOf("openclinic/"));
    }

    String sContextPath = req.getContextPath() + "/";
    HttpSession session = req.getSession();
    String sProjectDir = (String) session.getAttribute("activeProjectDir");

    this.url = sURL;
    this.contextPath = sContextPath;
    this.projectDir = sProjectDir;

    try {
      doc.addProducer();
      doc.addAuthor(user.person.firstname + " " + user.person.lastname);
      doc.addCreationDate();
      doc.addCreator("OpenClinic Software");
      Rectangle rectangle =
          new Rectangle(
              0,
              0,
              new Float(MedwanQuery.getInstance().getConfigInt("patientCardWidth", 540) * 72 / 254)
                  .floatValue(),
              new Float(MedwanQuery.getInstance().getConfigInt("patientCardHeight", 860) * 72 / 254)
                  .floatValue());
      doc.setPageSize(rectangle.rotate());
      doc.setMargins(10, 10, 10, 10);
      doc.setJavaScript_onLoad("print();\r");
      doc.open();

      // add content to document
      String[] persons = patientids.toString().split(";");
      for (int n = 0; n < persons.length; n++) {
        AdminPerson person = DatacenterHelper.getPatientRecord(persons[n]);
        if (person != null) {
          printPatientCard(person);
          doc.newPage();
        }
      }
    } catch (Exception e) {
      baosPDF.reset();
      e.printStackTrace();
    } finally {
      if (doc != null) doc.close();
      if (docWriter != null) docWriter.close();
    }

    if (baosPDF.size() < 1) {
      throw new DocumentException("document has no bytes");
    }

    return baosPDF;
  }
Esempio n. 29
0
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
      Rectangle rect = writer.getBoxSize("art");

      Image imghead = null;
      PdfContentByte cbhead = null;

      try {
        imghead = Image.getInstance("LogoSapito5.png");
        imghead.setAbsolutePosition(0, 0);
        imghead.setAlignment(Image.ALIGN_CENTER);
        imghead.scalePercent(10f);
        cbhead = writer.getDirectContent();
        PdfTemplate tp = cbhead.createTemplate(100, 150);
        tp.addImage(imghead);
        cbhead.addTemplate(tp, 100, 715);

      } catch (BadElementException e) {
        e.printStackTrace();
      } catch (DocumentException e) {
        e.printStackTrace();
      } catch (IOException ex) {
        Logger.getLogger(Cuentaspagar.class.getName()).log(Level.SEVERE, null, ex);
      }

      Phrase headPhraseImg =
          new Phrase(cbhead + "", FontFactory.getFont(FontFactory.TIMES_ROMAN, 7, Font.NORMAL));

      Calendar c1 = Calendar.getInstance();
      Calendar c2 = new GregorianCalendar();
      String dia, mes, annio;
      dia = Integer.toString(c1.get(Calendar.DATE));
      mes = Integer.toString(c1.get(Calendar.MONTH));
      annio = Integer.toString(c1.get(Calendar.YEAR));
      java.util.Date fecha = new Date();
      String fechis = dia + "/" + mes + "/" + annio;
      Paragraph parrafo5 =
          new Paragraph(
              fechis,
              FontFactory.getFont(FontFactory.TIMES_ROMAN, 11, Font.NORMAL, BaseColor.BLACK));
      ColumnText.showTextAligned(
          writer.getDirectContent(),
          Element.ALIGN_CENTER,
          new Phrase(parrafo5),
          rect.getRight(450),
          rect.getTop(-80),
          0);

      Paragraph parrafo7 =
          new Paragraph(
              "Empresa Sapito S.A. de C.V.",
              FontFactory.getFont(FontFactory.TIMES_ROMAN, 16, Font.BOLD, BaseColor.BLACK));
      ColumnText.showTextAligned(
          writer.getDirectContent(),
          Element.ALIGN_CENTER,
          new Phrase(parrafo7),
          rect.getBottom(250),
          rect.getTop(-60),
          0);

      Paragraph parrafo8 =
          new Paragraph(
              "Cuentas por cobrar",
              FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD, BaseColor.BLACK));
      ColumnText.showTextAligned(
          writer.getDirectContent(),
          Element.ALIGN_CENTER,
          new Phrase(parrafo8),
          rect.getBottom(250),
          rect.getTop(-40),
          0);

      ColumnText.showTextAligned(
          writer.getDirectContent(),
          Element.ALIGN_BOTTOM,
          new Phrase(
              "      _________________________________________________________________________________    "),
          rect.getBorder(),
          rect.getTop(-24),
          0);

      ColumnText.showTextAligned(
          writer.getDirectContent(),
          Element.ALIGN_BOTTOM,
          new Phrase(
              "      _________________________________________________________________________________    "),
          rect.getBorder(),
          rect.getTop(-20),
          0);

      Paragraph parrafo6 =
          new Paragraph(
              String.format("Pág %d", writer.getPageNumber()),
              FontFactory.getFont(FontFactory.TIMES_ROMAN, 11, Font.NORMAL, BaseColor.BLACK));
      ColumnText.showTextAligned(
          writer.getDirectContent(),
          Element.ALIGN_CENTER,
          new Phrase(parrafo6),
          rect.getRight(-35),
          rect.getTop(-80),
          0);
    }
Esempio n. 30
0
  private static void addScrambles(
      PdfWriter docWriter, Document doc, ScrambleRequest scrambleRequest, String globalTitle)
      throws DocumentException, IOException {
    if (scrambleRequest.fmc) {
      Rectangle pageSize = doc.getPageSize();
      for (int i = 0; i < scrambleRequest.scrambles.length; i++) {
        String scramble = scrambleRequest.scrambles[i];
        PdfContentByte cb = docWriter.getDirectContent();
        float LINE_THICKNESS = 0.5f;
        BaseFont bf =
            BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

        int bottom = 30;
        int left = 35;
        int right = (int) (pageSize.getWidth() - left);
        int top = (int) (pageSize.getHeight() - bottom);

        int height = top - bottom;
        int width = right - left;

        int solutionBorderTop = bottom + (int) (height * .5);
        int scrambleBorderTop = solutionBorderTop + 40;

        int competitorInfoBottom = top - (int) (height * .15);
        int gradeBottom = competitorInfoBottom - 50;
        int competitorInfoLeft = right - (int) (width * .45);

        int rulesRight = competitorInfoLeft;

        int padding = 5;

        // Outer border
        cb.setLineWidth(2f);
        cb.moveTo(left, top);
        cb.lineTo(left, bottom);
        cb.lineTo(right, bottom);
        cb.lineTo(right, top);

        // Solution border
        cb.moveTo(left, solutionBorderTop);
        cb.lineTo(right, solutionBorderTop);

        // Rules bottom border
        cb.moveTo(left, scrambleBorderTop);
        cb.lineTo(rulesRight, scrambleBorderTop);

        // Rules right border
        cb.lineTo(rulesRight, gradeBottom);

        // Grade bottom border
        cb.moveTo(competitorInfoLeft, gradeBottom);
        cb.lineTo(right, gradeBottom);

        // Competitor info bottom border
        cb.moveTo(competitorInfoLeft, competitorInfoBottom);
        cb.lineTo(right, competitorInfoBottom);

        // Competitor info left border
        cb.moveTo(competitorInfoLeft, gradeBottom);
        cb.lineTo(competitorInfoLeft, top);

        // Solution lines
        int availableSolutionWidth = right - left;
        int availableSolutionHeight = scrambleBorderTop - bottom;
        int lineWidth = 25;
        // int linesX = (availableSolutionWidth/lineWidth + 1)/2;
        int linesX = 10;
        int linesY = (int) Math.ceil(1.0 * WCA_MAX_MOVES_FMC / linesX);

        cb.setLineWidth(LINE_THICKNESS);
        cb.stroke();

        //              int allocatedX = (2*linesX-1)*lineWidth;
        int excessX = availableSolutionWidth - linesX * lineWidth;
        int moveCount = 0;
        solutionLines:
        for (int y = 0; y < linesY; y++) {
          for (int x = 0; x < linesX; x++) {
            if (moveCount >= WCA_MAX_MOVES_FMC) {
              break solutionLines;
            }
            int xPos = left + x * lineWidth + (x + 1) * excessX / (linesX + 1);
            int yPos = solutionBorderTop - (y + 1) * availableSolutionHeight / (linesY + 1);
            cb.moveTo(xPos, yPos);
            cb.lineTo(xPos + lineWidth, yPos);
            moveCount++;
          }
        }

        float UNDERLINE_THICKNESS = 0.2f;
        cb.setLineWidth(UNDERLINE_THICKNESS);
        cb.stroke();

        cb.beginText();
        int availableScrambleSpace = right - left - 2 * padding;
        int scrambleFontSize = 20;
        String scrambleStr = "Scramble: " + scramble;
        float scrambleWidth;
        do {
          scrambleFontSize--;
          scrambleWidth = bf.getWidthPoint(scrambleStr, scrambleFontSize);
        } while (scrambleWidth > availableScrambleSpace);

        cb.setFontAndSize(bf, scrambleFontSize);
        int scrambleY =
            3 + solutionBorderTop + (scrambleBorderTop - solutionBorderTop - scrambleFontSize) / 2;
        cb.showTextAligned(PdfContentByte.ALIGN_LEFT, scrambleStr, left + padding, scrambleY, 0);
        cb.endText();

        int availableScrambleWidth = right - rulesRight;
        int availableScrambleHeight = gradeBottom - scrambleBorderTop;
        Dimension dim =
            scrambleRequest.scrambler.getPreferredSize(
                availableScrambleWidth - 2, availableScrambleHeight - 2);
        PdfTemplate tp = cb.createTemplate(dim.width, dim.height);
        Graphics2D g2 = new PdfGraphics2D(tp, dim.width, dim.height, new DefaultFontMapper());

        try {
          Svg svg = scrambleRequest.scrambler.drawScramble(scramble, scrambleRequest.colorScheme);
          drawSvgToGraphics2D(svg, g2, dim);
        } catch (InvalidScrambleException e) {
          l.log(Level.INFO, "", e);
        } finally {
          g2.dispose();
        }

        cb.addImage(
            Image.getInstance(tp),
            dim.width,
            0,
            0,
            dim.height,
            rulesRight + (availableScrambleWidth - dim.width) / 2,
            scrambleBorderTop + (availableScrambleHeight - dim.height) / 2);

        ColumnText ct = new ColumnText(cb);

        int fontSize = 15;
        int marginBottom = 10;
        int offsetTop = 27;
        boolean showScrambleCount = scrambleRequest.scrambles.length > 1;
        if (showScrambleCount) {
          offsetTop -= fontSize + 2;
        }

        cb.beginText();
        cb.setFontAndSize(bf, fontSize);
        cb.showTextAligned(
            PdfContentByte.ALIGN_CENTER,
            globalTitle,
            competitorInfoLeft + (right - competitorInfoLeft) / 2,
            top - offsetTop,
            0);
        offsetTop += fontSize + 2;
        cb.endText();

        cb.beginText();
        cb.setFontAndSize(bf, fontSize);
        cb.showTextAligned(
            PdfContentByte.ALIGN_CENTER,
            scrambleRequest.title,
            competitorInfoLeft + (right - competitorInfoLeft) / 2,
            top - offsetTop,
            0);
        cb.endText();

        if (showScrambleCount) {
          cb.beginText();
          offsetTop += fontSize + 2;
          cb.setFontAndSize(bf, fontSize);
          cb.showTextAligned(
              PdfContentByte.ALIGN_CENTER,
              "Scramble " + (i + 1) + " of " + scrambleRequest.scrambles.length,
              competitorInfoLeft + (right - competitorInfoLeft) / 2,
              top - offsetTop,
              0);
          cb.endText();
        }

        offsetTop += fontSize + marginBottom;

        cb.beginText();
        fontSize = 15;
        cb.setFontAndSize(bf, fontSize);
        cb.showTextAligned(
            PdfContentByte.ALIGN_LEFT,
            "Competitor: __________________",
            competitorInfoLeft + padding,
            top - offsetTop,
            0);
        offsetTop += fontSize + marginBottom;
        cb.endText();

        cb.beginText();

        fontSize = 15;
        cb.setFontAndSize(bf, fontSize);
        cb.showTextAligned(
            PdfContentByte.ALIGN_LEFT, "WCA ID:", competitorInfoLeft + padding, top - offsetTop, 0);

        cb.setFontAndSize(bf, 19);
        int wcaIdLength = 63;
        cb.showTextAligned(
            PdfContentByte.ALIGN_LEFT,
            "_ _ _ _  _ _ _ _  _ _",
            competitorInfoLeft + padding + wcaIdLength,
            top - offsetTop,
            0);

        offsetTop += fontSize + (int) (marginBottom * 1.8);
        cb.endText();

        cb.beginText();
        fontSize = 11;
        cb.setFontAndSize(bf, fontSize);
        cb.showTextAligned(
            PdfContentByte.ALIGN_CENTER,
            "DO NOT FILL IF YOU ARE THE COMPETITOR",
            competitorInfoLeft + (right - competitorInfoLeft) / 2,
            top - offsetTop,
            0);
        offsetTop += fontSize + marginBottom;
        cb.endText();

        cb.beginText();
        fontSize = 11;
        cb.setFontAndSize(bf, fontSize);
        cb.showTextAligned(
            PdfContentByte.ALIGN_CENTER,
            "Graded by: _______________ Result: ______",
            competitorInfoLeft + (right - competitorInfoLeft) / 2,
            top - offsetTop,
            0);
        offsetTop += fontSize + marginBottom;
        cb.endText();

        cb.beginText();
        cb.setFontAndSize(bf, 25f);
        int MAGIC_NUMBER = 40; // kill me now
        cb.showTextAligned(
            PdfContentByte.ALIGN_CENTER,
            "Fewest Moves",
            left + (competitorInfoLeft - left) / 2,
            top - MAGIC_NUMBER,
            0);
        cb.endText();

        com.itextpdf.text.List rules = new com.itextpdf.text.List(com.itextpdf.text.List.UNORDERED);
        rules.add("Notate your solution by writing one move per bar.");
        rules.add("To delete moves, clearly erase/blacken them.");
        rules.add("Face moves F, B, R, L, U, and D are clockwise.");
        rules.add("Rotations x, y, and z follow R, U, and F.");
        rules.add("' inverts a move; 2 doubles a move. (e.g.: U', U2)");
        rules.add("w makes a face move into two layers. (e.g.: Uw)");
        rules.add("A [lowercase] move is a cube rotation. (e.g.: [u])");

        ct.addElement(rules);
        int rulesTop = competitorInfoBottom + 55;
        ct.setSimpleColumn(
            left + padding,
            scrambleBorderTop,
            competitorInfoLeft - padding,
            rulesTop,
            0,
            Element.ALIGN_LEFT);
        ct.go();

        rules = new com.itextpdf.text.List(com.itextpdf.text.List.UNORDERED);
        rules.add("You have 1 hour to find a solution.");
        rules.add("Your solution length will be counted in OBTM.");
        int maxMoves = WCA_MAX_MOVES_FMC;
        rules.add("Your solution must be at most " + maxMoves + " moves, including rotations.");
        rules.add(
            "Your solution must not be directly derived from any part of the scrambling algorithm.");
        ct.addElement(rules);
        MAGIC_NUMBER = 150; // kill me now
        ct.setSimpleColumn(
            left + padding,
            scrambleBorderTop,
            rulesRight - padding,
            rulesTop - MAGIC_NUMBER,
            0,
            Element.ALIGN_LEFT);
        ct.go();

        doc.newPage();
      }
    } else {
      Rectangle pageSize = doc.getPageSize();

      float sideMargins = 100 + doc.leftMargin() + doc.rightMargin();
      float availableWidth = pageSize.getWidth() - sideMargins;
      float vertMargins = doc.topMargin() + doc.bottomMargin();
      float availableHeight = pageSize.getHeight() - vertMargins;
      if (scrambleRequest.extraScrambles.length > 0) {
        availableHeight -= 20; // Yeee magic numbers. This should make space for the headerTable.
      }
      int scramblesPerPage =
          Math.min(MAX_SCRAMBLES_PER_PAGE, scrambleRequest.getAllScrambles().size());
      int maxScrambleImageHeight =
          (int) (availableHeight / scramblesPerPage - 2 * SCRAMBLE_IMAGE_PADDING);

      int maxScrambleImageWidth =
          (int)
              (availableWidth / 2); // We don't let scramble images take up more than half the page
      if (scrambleRequest.scrambler.getShortName().equals("minx")) {
        // TODO - If we allow the megaminx image to be too wide, the
        // megaminx scrambles get really tiny. This tweak allocates
        // a more optimal amount of space to the scrambles. This is possible
        // because the scrambles are so uniformly sized.
        maxScrambleImageWidth = 190;
      }

      Dimension scrambleImageSize =
          scrambleRequest.scrambler.getPreferredSize(maxScrambleImageWidth, maxScrambleImageHeight);

      // First do a dry run just to see if any scrambles require highlighting.
      // Then do the real run, and force highlighting on every scramble
      // if any scramble required it.
      boolean forceHighlighting = false;
      for (boolean dryRun : new boolean[] {true, false}) {
        String scrambleNumberPrefix = "";
        TableAndHighlighting tableAndHighlighting =
            createTable(
                docWriter,
                doc,
                sideMargins,
                scrambleImageSize,
                scrambleRequest.scrambles,
                scrambleRequest.scrambler,
                scrambleRequest.colorScheme,
                scrambleNumberPrefix,
                forceHighlighting);
        if (dryRun) {
          if (tableAndHighlighting.highlighting) {
            forceHighlighting = true;
            continue;
          }
        } else {
          doc.add(tableAndHighlighting.table);
        }

        if (scrambleRequest.extraScrambles.length > 0) {
          PdfPTable headerTable = new PdfPTable(1);
          headerTable.setTotalWidth(new float[] {availableWidth});
          headerTable.setLockedWidth(true);

          PdfPCell extraScramblesHeader = new PdfPCell(new Paragraph("Extra scrambles"));
          extraScramblesHeader.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
          extraScramblesHeader.setPaddingBottom(3);
          headerTable.addCell(extraScramblesHeader);
          if (!dryRun) {
            doc.add(headerTable);
          }

          scrambleNumberPrefix = "E";
          TableAndHighlighting extraTableAndHighlighting =
              createTable(
                  docWriter,
                  doc,
                  sideMargins,
                  scrambleImageSize,
                  scrambleRequest.extraScrambles,
                  scrambleRequest.scrambler,
                  scrambleRequest.colorScheme,
                  scrambleNumberPrefix,
                  forceHighlighting);
          if (dryRun) {
            if (tableAndHighlighting.highlighting) {
              forceHighlighting = true;
              continue;
            }
          } else {
            doc.add(extraTableAndHighlighting.table);
          }
        }
      }
    }
    doc.newPage();
  }