Example usage for org.apache.pdfbox.pdmodel PDPageContentStream setNonStrokingColor

List of usage examples for org.apache.pdfbox.pdmodel PDPageContentStream setNonStrokingColor

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDPageContentStream setNonStrokingColor.

Prototype

@Deprecated
public void setNonStrokingColor(float[] components) throws IOException 

Source Link

Document

Set the color components of current non-stroking color space.

Usage

From source file:uia.pdf.gridbag.model.TextCell.java

License:Apache License

@Override
public void accept(ContentView cv, GridBagDrawer view, PDPageContentStream contentStream, Point bottomLeft,
        Map<String, Object> data) {
    if (this.text.value == null) {
        return;/*  w  ww  . ja  va  2  s .co m*/
    }

    try {
        PDFont font = cv.getDoc().getFont();

        int fontSize1 = PDFUtil.fixFontSzie(this.text.value, font, this.text.getFontSize(), getWidth());
        int cw1 = PDFUtil.getContentWidth(this.text.value, font, fontSize1);
        int ch1 = PDFUtil.getContentHeight(this.text.value, font, fontSize1);

        int fontSize2 = PDFUtil.fixFontSzie(this.subText.value, font, this.subText.getFontSize(), getWidth());
        int cw2 = PDFUtil.getContentWidth(this.subText.value, font, fontSize2);
        int ch2 = PDFUtil.getContentHeight(this.subText.value, font, fontSize2);

        int textLine = bottomLeft.y + getHeight() / 2 - 3;
        if ("NEAR".equals(getVAlignment())) {
            textLine = bottomLeft.y + getHeight() - ch1 - 3;
        } else if ("FAR".equals(getVAlignment())) {
            textLine = ch2 == 0 ? bottomLeft.y + 3 : bottomLeft.y + ch2 + 9;
        } else {
            textLine = bottomLeft.y + ch2 + (getHeight() - (ch1 + ch2)) / 2 + 1;
        }

        contentStream.setFont(font, fontSize1);
        contentStream.setNonStrokingColor(this.text.foreground);
        contentStream.beginText();
        if ("NEAR".equalsIgnoreCase(getAlignment())) {
            contentStream.newLineAtOffset(bottomLeft.x + 2, textLine);
        } else if ("FAR".equalsIgnoreCase(getAlignment())) {
            contentStream.newLineAtOffset(bottomLeft.x + getWidth() - cw1 - 2, textLine);
        } else {
            contentStream.newLineAtOffset(bottomLeft.x + (getWidth() - cw1) / 2, textLine);
        }
        contentStream.showText(this.text.value);
        contentStream.endText();

        if (this.subText.value != null) {
            textLine -= ch2;
            contentStream.setFont(font, fontSize2);
            contentStream.setNonStrokingColor(this.subText.foreground);
            contentStream.beginText();
            if ("NEAR".equalsIgnoreCase(getAlignment())) {
                contentStream.newLineAtOffset(bottomLeft.x + 2, textLine);
            } else if ("FAR".equalsIgnoreCase(getAlignment())) {
                contentStream.newLineAtOffset(bottomLeft.x + getWidth() - cw2 - 2, textLine);
            } else {
                contentStream.newLineAtOffset(bottomLeft.x + (getWidth() - cw2) / 2, textLine);
            }
            contentStream.showText(this.subText.value);
            contentStream.endText();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Utils.PDF.java

public static void print(String nFactura, Date fecha, String empleado, ClienteVO cliente,
        List<ProductosCanasta> productos, BigDecimal subtotal) throws IOException {
    DateFormat dateAnio = new SimpleDateFormat("yyyy");
    DateFormat dateMes = new SimpleDateFormat("MM");
    DateFormat dateDia = new SimpleDateFormat("dd");
    String direccion = "facturas" + "/" + dateAnio.format(fecha) + "/" + dateMes.format(fecha) + "/"
            + dateDia.format(fecha);//from w ww.  j a  va 2 s .c om
    File dir = new File(direccion);
    if (dir.exists()) {
        System.out.println("Ya exitiste la carpeta");
    } else {
        dir.mkdirs();
    }
    DateFormat dateF = new SimpleDateFormat("kk-mm-ss_dd-MM-yyyy");
    String fileName = direccion + "/" + dateF.format(fecha) + ".pdf";
    String imagem = "bill-512.png";
    System.out.println("Se creo su factura");
    PDDocument doc = new PDDocument();
    PDPage page = new PDPage(PDRectangle.A4);
    PDPageContentStream content = new PDPageContentStream(doc, page);
    doc.addPage(page);

    PDImageXObject pdImage = null;
    try {
        pdImage = PDImageXObject.createFromFile(imagem, doc);
    } catch (IOException e1) {
        System.out.println("ERROR no cargo logo factura");
    }
    float scale = 0.2f;

    // Titulo factura     
    content.beginText();
    content.setFont(PDType1Font.HELVETICA, 26);
    content.setNonStrokingColor(Color.BLUE);
    content.newLineAtOffset(250, 785);
    content.showText("Factura Bazar A&J");
    content.endText();

    // Logo
    content.drawImage(pdImage, 30, 725, pdImage.getWidth() * scale, pdImage.getHeight() * scale);

    //Cuadro info empresa
    content.setNonStrokingColor(Color.BLACK);
    content.addRect(25, 700, 280, 1);
    content.setNonStrokingColor(Color.BLACK);
    content.addRect(304, 700, 1, -65);
    content.setNonStrokingColor(Color.BLACK);
    content.addRect(25, 634, 280, 1);
    content.setNonStrokingColor(Color.BLACK);
    content.addRect(25, 700, 1, -65);
    content.fill();

    // Texto info empresa
    content.beginText();
    content.setLeading(15); // da el salto de pagina
    content.setFont(PDType1Font.COURIER, 10);
    content.newLineAtOffset(30, 685);
    content.showText("Direccion: " + "Coop. Universitaria Mz 258 Solar 9");
    content.newLine();
    content.showText("Ciudad   : " + "Guayaquil, Ecuador");
    content.newLine();
    content.showText("Telefono : " + "042937914");
    content.newLine();
    content.showText("RUC      : " + "1710034065");
    content.endText();

    //Cuadro datos factura
    content.setNonStrokingColor(Color.BLACK); //arriba
    content.addRect(380, 700, 200, 1);
    content.setNonStrokingColor(Color.BLACK); // derecha
    content.addRect(579, 700, 1, -65);
    content.setNonStrokingColor(Color.BLACK); // abajo
    content.addRect(380, 634, 200, 1);
    content.setNonStrokingColor(Color.BLACK); // izquierda
    content.addRect(380, 700, 1, -65);
    content.fill();

    DateFormat dateFormat = new SimpleDateFormat("dd - MM - yyyy");
    DateFormat dateFormath = new SimpleDateFormat("h:mm a");
    // Texto datos factura
    content.beginText();
    content.setLeading(15); // da el salto de pagina
    content.setFont(PDType1Font.COURIER, 10);
    content.newLineAtOffset(390, 685);
    content.showText("Factura N: " + nFactura);
    content.newLine();
    content.showText("Fecha    : " + dateFormat.format(fecha));
    content.newLine();
    content.showText("Hora     : " + dateFormath.format(fecha));
    content.newLine();
    content.showText("Empleado : " + empleado);
    content.endText();

    // Linea Divisoria
    content.setNonStrokingColor(Color.BLUE); // abajo
    content.addRect(0, 620, 595, 1);
    content.fill();

    // Datos Cliente
    content.beginText();
    content.setLeading(18); // da el salto de pagina
    content.setNonStrokingColor(Color.BLACK);
    content.setFont(PDType1Font.COURIER, 13);
    content.newLineAtOffset(30, 600);
    content.showText("Nombre   : " + cliente.getNombre_C() + cliente.getApellido_C());
    content.newLine();
    content.showText("Direcion : " + cliente.getDireccion_C());
    content.newLine();
    content.showText("Telefono : " + cliente.getConvencional_C());
    content.endText();

    content.setNonStrokingColor(Color.BLUE); // abajo
    content.addRect(0, 550, 595, 1);
    content.fill();

    content.setNonStrokingColor(Color.BLACK); // numero
    content.addRect(30, 510, 50, 15);
    content.fill();
    content.beginText();
    content.setLeading(0);
    content.setNonStrokingColor(Color.WHITE);
    content.setFont(PDType1Font.COURIER_BOLD, 13);
    content.newLineAtOffset(45, 515);
    content.showText("N.");
    content.endText();

    content.setNonStrokingColor(Color.BLACK); // cantidad
    content.addRect(90, 510, 40, 15);
    content.fill();
    content.beginText();
    content.setLeading(18); // da el salto de pagina
    content.setNonStrokingColor(Color.WHITE);
    content.setFont(PDType1Font.COURIER_BOLD, 13);
    content.newLineAtOffset(94, 515);
    content.showText("Cant");
    content.endText();

    content.setNonStrokingColor(Color.BLACK); // Descripcion
    content.addRect(140, 510, 270, 15);
    content.fill();
    content.beginText();
    content.setLeading(18); // da el salto de pagina
    content.setNonStrokingColor(Color.WHITE);
    content.setFont(PDType1Font.COURIER_BOLD, 13);
    content.newLineAtOffset(230, 515);
    content.showText("Descripcion");
    content.endText();

    content.setNonStrokingColor(Color.BLACK); // cantidad
    content.addRect(430, 510, 70, 15);
    content.fill();
    content.beginText();
    content.setLeading(18); // da el salto de pagina
    content.setNonStrokingColor(Color.WHITE);
    content.setFont(PDType1Font.COURIER_BOLD, 13);
    content.newLineAtOffset(440, 515);
    content.showText("P.Unit");
    content.endText();

    content.setNonStrokingColor(Color.BLACK); // importe
    content.addRect(510, 510, 70, 15);
    content.fill();
    content.beginText();
    content.setLeading(18); // da el salto de pagina
    content.setNonStrokingColor(Color.WHITE);
    content.setFont(PDType1Font.COURIER_BOLD, 13);
    content.newLineAtOffset(515, 515);
    content.showText("Importe");
    content.endText();

    int cont = 1;
    int vertical = 490;
    // Productos
    for (ProductosCanasta p : productos) {
        content.beginText();
        content.setNonStrokingColor(Color.BLACK);
        content.setFont(PDType1Font.COURIER, 13);
        content.newLineAtOffset(45, vertical);
        content.showText("" + cont);
        content.endText();

        content.beginText();
        content.setNonStrokingColor(Color.BLACK);
        content.setFont(PDType1Font.COURIER, 13);
        content.newLineAtOffset(105, vertical);
        content.showText("" + p.getCantidad());
        content.endText();

        content.beginText();
        content.setNonStrokingColor(Color.BLACK);
        content.setFont(PDType1Font.COURIER, 11);
        content.newLineAtOffset(145, vertical);
        content.showText("" + p.getNombre());
        content.endText();

        content.beginText();
        content.setNonStrokingColor(Color.BLACK);
        content.setFont(PDType1Font.COURIER, 13);
        content.newLineAtOffset(440, vertical);
        content.showText("" + p.getPrecio_venta());
        content.endText();

        content.beginText();
        content.setNonStrokingColor(Color.BLACK);
        content.setFont(PDType1Font.COURIER, 13);
        content.newLineAtOffset(520, vertical);
        content.showText("" + p.getPrecio_venta().multiply(new BigDecimal(p.getCantidad())));
        content.endText();

        cont++;
        vertical -= 20;
    }

    // max x pagina 595 x 841.8898
    content.setNonStrokingColor(Color.BLUE); // total
    content.addRect(0, 60, 595, 1);
    content.fill();

    /// TOTAL
    content.beginText();
    content.setNonStrokingColor(Color.BLACK);
    content.setFont(PDType1Font.COURIER_BOLD, 20);
    content.newLineAtOffset(500, 35);
    content.showText(subtotal.multiply(new BigDecimal(0.14)).add(subtotal).setScale(2, 3).toString());
    content.endText();

    content.beginText();
    content.newLineAtOffset(510, 20);
    content.setFont(PDType1Font.COURIER_BOLD, 15);
    content.showText("TOTAL");
    content.endText();

    // IVA
    content.beginText();
    content.setNonStrokingColor(Color.BLACK);
    content.setFont(PDType1Font.COURIER_BOLD, 20);
    content.newLineAtOffset(430, 35);
    content.showText("" + 14 + "%");
    content.endText();

    content.beginText();
    content.newLineAtOffset(430, 20);
    content.setFont(PDType1Font.COURIER_BOLD, 15);
    content.showText("IVA");
    content.endText();

    // Subtotal
    content.beginText();
    content.setNonStrokingColor(Color.BLACK);
    content.setFont(PDType1Font.COURIER_BOLD, 20);
    content.newLineAtOffset(300, 35);
    content.showText(subtotal.toString());
    content.endText();

    content.beginText();
    content.newLineAtOffset(300, 20);
    content.setFont(PDType1Font.COURIER_BOLD, 15);
    content.showText("SUBTOTAL");
    content.endText();

    content.close();
    doc.save(fileName);
    doc.close();
    Process p = Runtime.getRuntime().exec(new String[] { "xpdf", fileName });
    System.out.println("your file created in : " + System.getProperty("user.dir"));
}