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();
    }
  }
示例#2
0
 private void setPaint(boolean invert, double xoffset, double yoffset, boolean fill) {
   if (paint instanceof Color) {
     Color color = (Color) paint;
     int alpha = color.getAlpha();
     if (fill) {
       if (alpha != currentFillGState) {
         currentFillGState = alpha;
         PdfGState gs = fillGState[alpha];
         if (gs == null) {
           gs = new PdfGState();
           gs.setFillOpacity((float) alpha / 255f);
           fillGState[alpha] = gs;
         }
         cb.setGState(gs);
       }
       cb.setColorFill(color);
     } else {
       if (alpha != currentStrokeGState) {
         currentStrokeGState = alpha;
         PdfGState gs = strokeGState[alpha];
         if (gs == null) {
           gs = new PdfGState();
           gs.setStrokeOpacity((float) alpha / 255f);
           strokeGState[alpha] = gs;
         }
         cb.setGState(gs);
       }
       cb.setColorStroke(color);
     }
   } else if (paint instanceof GradientPaint) {
     GradientPaint gp = (GradientPaint) paint;
     Point2D p1 = gp.getPoint1();
     transform.transform(p1, p1);
     Point2D p2 = gp.getPoint2();
     transform.transform(p2, p2);
     Color c1 = gp.getColor1();
     Color c2 = gp.getColor2();
     PdfShading shading =
         PdfShading.simpleAxial(
             cb.getPdfWriter(),
             (float) p1.getX(),
             normalizeY((float) p1.getY()),
             (float) p2.getX(),
             normalizeY((float) p2.getY()),
             c1,
             c2);
     PdfShadingPattern pat = new PdfShadingPattern(shading);
     if (fill) cb.setShadingFill(pat);
     else cb.setShadingStroke(pat);
   } else if (paint instanceof TexturePaint) {
     try {
       TexturePaint tp = (TexturePaint) paint;
       BufferedImage img = tp.getImage();
       Rectangle2D rect = tp.getAnchorRect();
       com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null);
       PdfPatternPainter pattern = cb.createPattern(image.width(), image.height());
       AffineTransform inverse = this.normalizeMatrix();
       inverse.translate(rect.getX(), rect.getY());
       inverse.scale(rect.getWidth() / image.width(), -rect.getHeight() / image.height());
       double[] mx = new double[6];
       inverse.getMatrix(mx);
       pattern.setPatternMatrix(
           (float) mx[0],
           (float) mx[1],
           (float) mx[2],
           (float) mx[3],
           (float) mx[4],
           (float) mx[5]);
       image.setAbsolutePosition(0, 0);
       pattern.addImage(image);
       if (fill) cb.setPatternFill(pattern);
       else cb.setPatternStroke(pattern);
     } catch (Exception ex) {
       if (fill) cb.setColorFill(Color.gray);
       else cb.setColorStroke(Color.gray);
     }
   } else {
     try {
       BufferedImage img = null;
       int type = BufferedImage.TYPE_4BYTE_ABGR;
       if (paint.getTransparency() == Transparency.OPAQUE) {
         type = BufferedImage.TYPE_3BYTE_BGR;
       }
       img = new BufferedImage((int) width, (int) height, type);
       Graphics2D g = (Graphics2D) img.getGraphics();
       g.transform(transform);
       AffineTransform inv = transform.createInverse();
       Shape fillRect = new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight());
       fillRect = inv.createTransformedShape(fillRect);
       g.setPaint(paint);
       g.fill(fillRect);
       if (invert) {
         AffineTransform tx = new AffineTransform();
         tx.scale(1, -1);
         tx.translate(-xoffset, -yoffset);
         g.drawImage(img, tx, null);
       }
       com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null);
       PdfPatternPainter pattern = cb.createPattern(width, height);
       image.setAbsolutePosition(0, 0);
       pattern.addImage(image);
       if (fill) cb.setPatternFill(pattern);
       else cb.setPatternStroke(pattern);
     } catch (Exception ex) {
       if (fill) cb.setColorFill(Color.gray);
       else cb.setColorStroke(Color.gray);
     }
   }
 }