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:pl.vane.pdf.factory.PDFWriter.java

License:Open Source License

public static void drawString(PDPageContentStream stream, PDFont font, int fontSize, double leading, float x,
        float y, String text) throws IOException {
    stream.setStrokingColor(Color.BLACK);
    stream.setNonStrokingColor(Color.BLACK);
    stream.beginText();//w  w w. java 2  s  . co  m
    stream.setFont(font, fontSize);
    stream.setLeading(leading);
    stream.newLineAtOffset(x, y);
    for (String line : text.split("\n")) {
        stream.showText(line);
        stream.newLine();
    }
    stream.endText();

}

From source file:src.controller.TableController.java

/**
 * Dessine un rectangle/*from   w  ww .j a v  a  2s  .c  o m*/
 *
 * @param contentStream
 * @param posX
 * @param posY
 * @param width
 * @param height
 */
private void printRectangle(PDPageContentStream contentStream, float posX, float posY, float width,
        float height, Color backgroundColor) {
    try {
        printLine(contentStream, posX, posY, posX + width, posY);
        printLine(contentStream, posX, posY, posX, posY - height);
        printLine(contentStream, posX + width, posY, posX + width, posY - height);
        printLine(contentStream, posX, posY - height, posX + width, posY - height);

        contentStream.addRect(posX, posY - height, width, height);
        contentStream.setNonStrokingColor(
                new java.awt.Color((float) backgroundColor.getRed(), (float) backgroundColor.getGreen(),
                        (float) backgroundColor.getBlue(), (float) backgroundColor.getOpacity()));
        contentStream.fill();
    } catch (Exception e) {
        System.out.println(e.toString());
    }
}

From source file:src.controller.TextController.java

/**
 * Ajoute du texte dans le contentStream spcifi
 *
 * @param contentStream/*  ww w  .j a va  2s  .co  m*/
 * @param text
 * @param posX
 * @param posY
 * @param fontSize
 * @param font
 */
public void addText(PDPageContentStream contentStream, String text, float posX, float posY, int fontSize,
        PDType1Font font) {
    try {
        String[] lines = text.split("\n");
        for (int i = 0; i < lines.length; i++) {
            contentStream.beginText();
            contentStream.setFont(font, fontSize);
            contentStream.setNonStrokingColor(Color.BLACK);
            contentStream.newLineAtOffset(posX, posY - (fontSize * i + 5));
            contentStream.showText(lines[i]);
            contentStream.endText();
        }
    } catch (IOException e) {
        System.out.println(e.toString());
    }
}

From source file:stepReport.reports.model.savePDFModel.java

public void savePDFSemanal(File file, String[][] matrizDados) {

    if (matrizDados == null)
        return;//from  ww w  .j  av  a  2 s  .  com

    //Cria o documento
    PDDocument document = new PDDocument();

    //Vou criando as paginas dinamicamente de acordo com o numero de registros a serem impressos.
    //Para cada 25 registros, crio uma nova pagina
    //O valor de k vai ser atualizado durante o loop de impressao de registros,
    //assim como o loop de impressao de registro comeca a partir do valor de k
    int k = 1;
    int pagina = 0;
    while (k < matrizDados.length) {
        //Variavel com o numero da pagina
        pagina++;
        //Adiciona uma pagina
        PDPage page = new PDPage();
        //Configura o padrao de tamanho da pagina
        page.setMediaBox(PDRectangle.A4);
        //Configura a orientacao
        page.setRotation(90);
        //Adiciona a pagina ao documento
        document.addPage(page);
        PDFont font;
        //Obtem a largura da pagina
        float pageWidth = page.getMediaBox().getWidth();

        try {
            //abre o buffer pra edicao da pagina
            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            //Gira a pagina em 90 graus
            contentStream.transform(new Matrix(0, 1, -1, 0, pageWidth, 0));

            PDImageXObject pdImage = PDImageXObject.createFromFile("./step2.png", document);

            contentStream.drawImage(pdImage, 30, 520);

            //Define a cor da letra
            contentStream.setNonStrokingColor(Color.BLACK);
            //Abre pra edicao escrita
            contentStream.beginText();

            //Configura a fonte de titulo e o tamanho no buffer
            font = PDType1Font.COURIER_BOLD;
            contentStream.setFont(font, 18);
            contentStream.setLeading(14.5f);

            contentStream.newLineAtOffset(250, 530);
            contentStream.showText("Resumo de Horas semanais");

            //Imprime o numero da pagina
            font = PDType1Font.COURIER;
            contentStream.setFont(font, 12);

            contentStream.newLineAtOffset(490, 0);
            contentStream.showText("Pag " + Integer.toString(pagina));

            //Define o ponto de partida em X e Y
            contentStream.newLineAtOffset(-700, -50);
            //Define a fonte do cabecalho
            font = PDType1Font.COURIER_BOLD;
            contentStream.setFont(font, 12);
            //carrega o cabecalho com nome, profissao, itera pra cada data da semana e depois o total
            String titulo = StringUtils.rightPad("Nome", 20) + StringUtils.rightPad("Profissao", 16);
            for (int i = 2; i < matrizDados[0].length; i++)
                titulo += matrizDados[0][i] + "  ";

            //Escreve o cabecalho
            contentStream.showText(titulo);
            //Troca a fonte pra normal
            font = PDType1Font.COURIER;
            contentStream.setFont(font, 12);
            //TODO criar loop duplo para criar pagina e depois imprimir o dado enquanto houver dados a serem impressos
            contentStream.newLine();

            //Para cada linha da matriz recebida, vou formatar os campos nome, profissao, cada data da semana e o total pra imprimir na linha
            //Tenho que comecar a partir de k porque pode nao ser a primeira pagina. 

            //Configuro o limite baseado se eu estou ou nao na ultima pagina
            int limite = (k + savePDFModel.REGISTROS_PAGINA < matrizDados.length - 1)
                    ? savePDFModel.REGISTROS_PAGINA
                    : matrizDados.length - k;

            for (int i = 0; i < limite; i++) {
                String nome = this.formatName(matrizDados[i + k][0]);
                String profissao = this.formatProfissao(matrizDados[i + k][1]);
                String linha = nome + profissao;
                for (int j = 2; j < matrizDados[i].length; j++)
                    linha += StringUtils.rightPad(matrizDados[i + k][j], 10);

                contentStream.showText(linha);
                contentStream.newLine();
            }
            k += limite;

            //Imprime o total em negrito quando chega no final
            System.out.println(k);
            if (k >= matrizDados.length) {
                font = PDType1Font.COURIER_BOLD;
                contentStream.setFont(font, 12);
                Double[] totais = new Double[matrizDados[0].length - 2];
                for (int i = 0; i < totais.length; i++)
                    totais[i] = 0.0;

                for (int i = 1; i < matrizDados.length; i++) {
                    for (int j = 2; j < matrizDados[i].length; j++) {
                        if (!matrizDados[i][j].equals(""))
                            totais[j - 2] += Double.parseDouble(matrizDados[i][j]);
                    }
                }
                String linhaTot = StringUtils.rightPad("Totais", 36);
                for (int i = 0; i < totais.length; i++) {
                    linhaTot += StringUtils.rightPad(totais[i].toString(), 10);
                }
                contentStream.showText(linhaTot);
                //Imprime a linha de assinatura
                this.signatureLine(contentStream);
            }
            contentStream.endText();
            contentStream.close();

        } catch (javax.imageio.IIOException ex) {
            JOptionPane.showMessageDialog(new JFrame(), "Imagem step2.png no encontrada");
            return;
        } catch (IOException ex) {
            Logger.getLogger(savePDFModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    try {
        //Esse save vai dentro do loop?
        document.save(file);
        document.close();
    } catch (IOException ex) {
        Logger.getLogger(savePDFModel.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:uia.pdf.grid.DefaultCellRenderer.java

License:Apache License

private int nowrap(PDPageContentStream contentStream, Point topLeft, AbstractGridView view, ColumnModel cm,
        Object value, int row, int col) {
    int h = view.getFontSize();

    try {/*ww  w. j  a v  a  2 s  .com*/
        PDFont font = view.getDoc().getFont();
        h = PDFUtil.getContentHeight("", font, view.getFontSize());

        if (cm.getBackground() != null) {
            contentStream.setNonStrokingColor(cm.getBackground());
            contentStream.addRect(topLeft.x, topLeft.y - h - 8, cm.getWidth(), h + 7);
            contentStream.fill();
            contentStream.setNonStrokingColor(Color.black);
        }

        String content = view.getDoc().getValueParserFactory().parse(value);
        int contentWidth = PDFUtil.getContentWidth(content, font, view.getFontSize());
        if (cm.getWidth() - contentWidth < 4) {
            content = PDFUtil.cutContent(content, font, view.getFontSize(), cm.getWidth() - 4);
            contentWidth = PDFUtil.getContentWidth(content, font, view.getFontSize());
        }
        contentWidth = Math.min(cm.getWidth() - 4, contentWidth);

        int offset = 3;
        if (cm.getHorizontalAlignment() == AlignmentType.FAR) {
            offset = cm.getWidth() - contentWidth - 3;
        } else if (cm.getHorizontalAlignment() == AlignmentType.CENTER) {
            offset = (cm.getWidth() - contentWidth) / 2;
        }

        contentStream.beginText();
        contentStream.newLineAtOffset(topLeft.x + offset, topLeft.y - h - 3);
        contentStream.showText(content);
        contentStream.endText();

    } catch (Exception ex) {

    }

    return h + 8;
}

From source file:uia.pdf.grid.GridView.java

License:Apache License

private PDPage drawColumns(PDPage page) throws IOException {
    if (!this.drawColumn) {
        return page;
    }//from w  w w  .ja v  a 2 s. com

    PDFont font = this.pdf.getFont();

    ColumnModel[] cms = this.model.getColumnModels();
    int hh = 0;
    for (int i0 = 0; i0 < cms.length; i0++) {
        ArrayList<String> cs = new ArrayList<String>();
        int h0 = PDFUtil.split(cms[i0].getDisplayName(), font, getFontSize(), cms[i0].getWidth() - 4, cs);
        hh = Math.max(hh, h0);
    }

    if (this.rowV - (4 * hh) < getBottom()) {
        page = newPage();
    }

    PDPageContentStream contentStream = new PDPageContentStream(this.pdf.getDocument(), page, AppendMode.APPEND,
            false, false);
    contentStream.setFont(font, this.fontSize);

    DefaultCellRenderer renderer = new DefaultCellRenderer();
    for (int i = 0; i < cms.length; i++) {
        if (i == 0) {
            this.columnH = getLeft();
        } else {
            this.columnH += cms[i - 1].getWidth();
        }

        contentStream.setNonStrokingColor(new Color(232, 232, 232));
        contentStream.addRect(this.columnH, this.rowV - hh, cms[i].getWidth(), hh);
        contentStream.fill();
        contentStream.setNonStrokingColor(new Color(0, 0, 0));

        String content = cms[i].getDisplayName();
        int offset = (cms[i].getWidth() - PDFUtil.getContentWidth(content, font, this.fontSize)) / 2;
        if (this.columnH + offset > getRight()) {
            break;
        }

        ColumnModel other = cms[i].clone();
        other.setWrap(true);
        other.setHorizontalAlignment(AlignmentType.CENTER);
        other.setBackground(new Color(232, 232, 232));
        renderer.paint(contentStream, new Point(this.columnH, this.rowV), this, other, content, -1, i);

        //contentStream.beginText();
        //contentStream.newLineAtOffset(this.columnH + offset, this.rowV - h - 3);
        //contentStream.showText(content);
        //contentStream.endText();
    }
    contentStream.moveTo(getLeft(), this.rowV - hh);
    contentStream.lineTo(getRight(), this.rowV - hh);
    contentStream.stroke();
    this.rowV -= hh;
    contentStream.close();

    return page;
}

From source file:uia.pdf.grid.GridView.java

License:Apache License

private PDPage drawRow(PDPage page, Map<String, Object> rowCells, int row, boolean forceNewPage)
        throws IOException {
    PDPage currPage = page;/*www  .j a v a2 s .c  om*/
    if (forceNewPage || (this.rowV - 12) < getBottom()) {
        drawGridLine(page);
        currPage = newPage();
        if (this.columnEachPage) {
            currPage = drawColumns(currPage);
        }
    }

    PDFont font = this.pdf.getFont();
    PDPageContentStream contentStream = new PDPageContentStream(this.pdf.getDocument(), currPage,
            AppendMode.APPEND, false, false);
    contentStream.setFont(font, this.fontSize);

    ColumnModel[] cms = this.model.getColumnModels();
    int h = Short.MIN_VALUE;
    for (int col = 0; col < cms.length; col++) {
        ColumnModel cm = cms[col];
        if (col == 0) {
            this.columnH = getLeft();
        } else {
            this.columnH += cms[col - 1].getWidth();
        }

        CellRenderer cr = this.model.getCellRenderer(0, col);
        h = Math.max(h, cr.paint(contentStream, new Point(this.columnH, this.rowV), this, cm,
                rowCells.get(cm.getId()), row, col));
    }
    this.rowV -= h;

    // handle overlap at footer area
    if (!forceNewPage && this.rowV < getBottom()) {
        contentStream.setNonStrokingColor(Color.white);
        contentStream.addRect(getLeft(), this.rowV, (float) this.getPaper().getDrawableSize().getWidth(), h);
        contentStream.fill();
        contentStream.setNonStrokingColor(Color.black);
        contentStream.close();
        this.rowV += h;
        return drawRow(page, rowCells, row, true);
    } else {

        contentStream.setLineWidth(0.1f);
        contentStream.moveTo(getLeft(), this.rowV);
        contentStream.lineTo(getRight(), this.rowV);
        contentStream.stroke();

        contentStream.close();
    }

    return currPage;
}

From source file:uia.pdf.gridbag.DefaultBindCellRenderer.java

License:Apache License

@Override
public void paint(PDPageContentStream contentStream, Point bottomLeft, ContentView view, Cell cell,
        Object value) throws IOException {
    PDFont font = view.getDoc().getFont();
    int fontSize = cell.getFontSize();
    contentStream.setFont(font, fontSize);
    String content = view.getDoc().getValueParserFactory().parse(value);
    if (content == null) {
        return;/*ww  w. j a  v  a2  s.c  om*/
    }

    int cw = PDFUtil.getContentWidth(content, font, fontSize);
    int ch = PDFUtil.getContentHeight(content, font, fontSize);
    int textLine = bottomLeft.y + (cell.getHeight() - ch) / 2;

    contentStream.setNonStrokingColor(Color.black);
    contentStream.beginText();
    if ("NEAR".equalsIgnoreCase(cell.getAlignment())) {
        contentStream.newLineAtOffset(bottomLeft.x + 2, textLine);
    } else if ("FAR".equalsIgnoreCase(cell.getAlignment())) {
        contentStream.newLineAtOffset(bottomLeft.x + cell.getWidth() - cw - 2, textLine);
    } else {
        contentStream.newLineAtOffset(bottomLeft.x + (cell.getWidth() - cw) / 2, textLine);
    }
    contentStream.showText(content);
    contentStream.endText();
}

From source file:uia.pdf.gridbag.GridBagDrawer.java

License:Apache License

public void drawEx(ContentView cv, PDPage page, Map<String, Map<String, Object>> gridsData) throws IOException {
    this.gbLayout.load(cv.getWidth(), cv.getHeight());

    PDPageContentStream contentStream = new PDPageContentStream(cv.getDoc().getDocument(), page,
            AppendMode.APPEND, false, false);
    PDFont font = cv.getDoc().getFont();
    contentStream.setFont(font, 9);/*from w w  w.  ja v  a  2  s.c o  m*/

    Point topLeft = cv.getTopLeft();
    for (GridBag grid : this.gbLayout.getGrids()) {
        contentStream.setLineWidth(0.5f);

        Map<String, Object> data = gridsData.get(grid.name);

        if (grid.background != null) {
            contentStream.setNonStrokingColor(grid.background);
            contentStream.addRect(topLeft.x + grid.x, topLeft.y - grid.height - grid.y, grid.width,
                    grid.height);
            contentStream.fill();
        }

        ArrayList<Cell> colorBorder = new ArrayList<Cell>();
        // draw grid
        if (grid.borderEnabled) {
            for (Cell[] cells : grid.cells) {
                for (Cell cell : cells) {
                    contentStream.setLineWidth(cell.borderSize);

                    Point cellBottomLeft = cell.bottomLeft(topLeft);
                    Color background = cell.getBackground();
                    if (background != null) {
                        contentStream.setNonStrokingColor(background);
                        contentStream.addRect(cellBottomLeft.x, cellBottomLeft.y, cell.getWidth(),
                                cell.getHeight());
                        contentStream.fill();
                    }

                    contentStream.addRect(cellBottomLeft.x, cellBottomLeft.y, cell.getWidth(),
                            cell.getHeight());
                    contentStream.stroke();

                    if (cell.borderColor != null) {
                        colorBorder.add(cell);
                    }
                }
            }

            // draw border
            for (Cell cell : colorBorder) {
                Point cellBottomLeft = cell.bottomLeft(topLeft);
                contentStream.setLineWidth(cell.borderSize);
                contentStream.setStrokingColor(cell.borderColor);
                contentStream.addRect(cellBottomLeft.x, cellBottomLeft.y, cell.getWidth(), cell.getHeight());
                contentStream.stroke();
            }
        }

        if (data == null) {
            continue;
        }

        // draw data
        int r = 0;
        for (Cell[] cells : grid.cells) {
            int c = 0;
            for (Cell cell : cells) {
                if (cell.col != c && cell.row != r) {
                    continue;
                }
                cell.accept(cv, this, contentStream, cell.bottomLeft(topLeft), data);
                c++;
            }
            r++;
        }

        if (grid.borderEnabled) {
            contentStream.setStrokingColor(grid.borderColor);
            contentStream.setLineWidth(1.0f);
            contentStream.addRect(topLeft.x + grid.x, topLeft.y - grid.height - grid.y, grid.width,
                    grid.height);
            contentStream.stroke();
        }

        contentStream.setStrokingColor(Color.black);
    }
    contentStream.close();
}

From source file:uia.pdf.gridbag.GridBagDrawer.java

License:Apache License

public void draw(ContentView cv, PDPage page, Map<String, Object> data) throws IOException {
    this.gbLayout.load(cv.getWidth(), cv.getHeight());

    PDPageContentStream contentStream = new PDPageContentStream(cv.getDoc().getDocument(), page,
            AppendMode.APPEND, false, false);
    PDFont font = cv.getDoc().getFont();
    contentStream.setFont(font, 9);/*from  ww w .j a  v  a 2s .c  om*/

    Point topLeft = cv.getTopLeft();
    for (GridBag grid : this.gbLayout.getGrids()) {
        contentStream.setLineWidth(0.5f);

        if (grid.background != null) {
            contentStream.setNonStrokingColor(grid.background);
            contentStream.addRect(topLeft.x + grid.x, topLeft.y - grid.height - grid.y, grid.width,
                    grid.height);
            contentStream.fill();
        }

        ArrayList<Cell> colorBorder = new ArrayList<Cell>();
        // draw grid
        if (grid.borderEnabled) {
            for (Cell[] cells : grid.cells) {
                for (Cell cell : cells) {
                    contentStream.setLineWidth(cell.borderSize);

                    Point cellBottomLeft = cell.bottomLeft(topLeft);
                    Color background = cell.getBackground();
                    if (background != null) {
                        contentStream.setNonStrokingColor(background);
                        contentStream.addRect(cellBottomLeft.x, cellBottomLeft.y, cell.getWidth(),
                                cell.getHeight());
                        contentStream.fill();
                    }

                    contentStream.addRect(cellBottomLeft.x, cellBottomLeft.y, cell.getWidth(),
                            cell.getHeight());
                    contentStream.stroke();

                    if (cell.borderColor != null) {
                        colorBorder.add(cell);
                    }
                }
            }

            // draw border
            for (Cell cell : colorBorder) {
                Point cellBottomLeft = cell.bottomLeft(topLeft);
                contentStream.setLineWidth(cell.borderSize);
                contentStream.setStrokingColor(cell.borderColor);
                contentStream.addRect(cellBottomLeft.x, cellBottomLeft.y, cell.getWidth(), cell.getHeight());
                contentStream.stroke();
            }
        }

        // draw data
        int r = 0;
        for (Cell[] cells : grid.cells) {
            int c = 0;
            for (Cell cell : cells) {
                if (cell.col != c && cell.row != r) {
                    continue;
                }
                cell.accept(cv, this, contentStream, cell.bottomLeft(topLeft), data);
                c++;
            }
            r++;
        }

        if (grid.borderEnabled) {
            contentStream.setStrokingColor(grid.borderColor);
            contentStream.setLineWidth(1.0f);
            contentStream.addRect(topLeft.x + grid.x, topLeft.y - grid.height - grid.y, grid.width,
                    grid.height);
            contentStream.stroke();
        }

        contentStream.setStrokingColor(Color.black);
    }
    contentStream.close();
}