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

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

Introduction

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

Prototype

public PDPageContentStream(PDDocument document, PDPage sourcePage, AppendMode appendContent, boolean compress,
        boolean resetContext) throws IOException 

Source Link

Document

Create a new PDPage content stream.

Usage

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

License:Apache License

private void drawRow(PDPage page, Map<String, Object> rowCells, int row) throws IOException {
    PDFont font = this.pdf.getFont();
    PDPageContentStream contentStream = new PDPageContentStream(this.pdf.getDocument(), page, AppendMode.APPEND,
            false, false);/*from w ww.  j  a va2  s  .c om*/
    contentStream.setFont(font, this.model.getFontSize());

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

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

    contentStream.close();
}

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

License:Apache License

@Override
public PDPage drawBookmarks(PDPage page, List<PDOutlineItem> ois) throws IOException {
    PDFont font = this.pdf.getFont();
    PDPageContentStream contentStream = new PDPageContentStream(this.pdf.getDocument(), page, AppendMode.APPEND,
            false, false);//w  w  w. j  a  va 2  s .co m
    contentStream.setFont(font, 14);
    for (PDOutlineItem oi : ois) {
        if (this.rowV - 16 < getBottom()) {
            contentStream.close();
            page = newPage();
            contentStream = new PDPageContentStream(this.pdf.getDocument(), page, AppendMode.APPEND, false,
                    false);
            contentStream.setFont(font, 14);
        }
        contentStream.beginText();
        contentStream.newLineAtOffset(getLeft(), this.rowV - 16);
        contentStream.showText(oi.getTitle().trim());
        contentStream.endText();
        this.rowV -= 16;
    }
    contentStream.close();
    this.rowV -= 10;
    this.tableTop = this.rowV;

    return page;
}

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

License:Apache License

private PDPage drawColumns(PDPage page) throws IOException {
    if (!this.drawColumn) {
        return page;
    }/* w w  w. j a va2 s.  co m*/

    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;/*from  w  ww .  jav a  2s. com*/
    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.grid.GridView.java

License:Apache License

private void drawGridLine(PDPage page) throws IOException {
    PDPageContentStream contentStream = new PDPageContentStream(this.pdf.getDocument(), page, AppendMode.APPEND,
            false, false);//from ww w.ja v  a  2 s .com
    contentStream.setLineWidth(1.0f);

    ColumnModel[] cms = this.model.getColumnModels();
    int columnH = getLeft();
    for (int i = 0; i < cms.length; i++) {
        columnH += cms[i].getWidth();
        contentStream.moveTo(columnH, this.tableTop);
        contentStream.lineTo(columnH, this.rowV);
        contentStream.stroke();

    }
    contentStream.addRect(getLeft(), this.rowV, (float) this.getPaper().getDrawableSize().getWidth(),
            this.tableTop - this.rowV);
    contentStream.stroke();

    contentStream.close();

    this.rowV -= 10;
}

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);// 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  w w  w.j a  va 2  s .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();
}

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

License:Apache License

@Override
public PDPage drawBookmarks(PDPage page, List<PDOutlineItem> ois) throws IOException {

    PDFont font = this.pdf.getFont();
    PDPageContentStream contentStream = new PDPageContentStream(this.pdf.getDocument(), page, AppendMode.APPEND,
            false, false);//from  w w  w. j a  v a 2s.com
    contentStream.setFont(font, 14);

    int rowV = getTop();
    for (PDOutlineItem oi : ois) {
        contentStream.beginText();
        contentStream.newLineAtOffset(getLeft(), rowV - 16);
        contentStream.showText(oi.getTitle().trim());
        contentStream.endText();
        rowV -= 16;
    }
    contentStream.close();

    return page;
}

From source file:uia.pdf.PDFMaker.java

License:Apache License

private void createIndex() throws IOException {
    A4Paper a4 = new A4Paper();

    PDPage page = a4.createPage();//  ww  w .java  2 s  . c  om
    if (this.doc.getPages().getCount() > 0) {
        this.doc.getPages().insertBefore(page, this.doc.getPage(0));
    } else {
        this.doc.addPage(page);
    }

    PDPageContentStream contentStream = new PDPageContentStream(this.doc, page, AppendMode.APPEND, false,
            false);

    contentStream.setFont(this.font, 16);
    int cw = PDFUtil.getContentWidth("INDEX", this.font, 11);
    contentStream.beginText();
    contentStream.newLineAtOffset(a4.getLeft() + (a4.getDrawableSize().width - cw) / 2, a4.getTop() - 12);
    contentStream.showText("INDEX");
    contentStream.endText();

    contentStream.setFont(this.font, 11);
    contentStream.setLineWidth(0.5f);
    int top = a4.getTop() - 20;
    for (BookmarkPage bp : this.bookmarkPages) {
        if (top <= a4.getBottom()) {
            contentStream.close();

            PDPage nextPage = a4.createPage();
            this.doc.getPages().insertAfter(nextPage, page);
            contentStream = new PDPageContentStream(this.doc, nextPage, AppendMode.APPEND, false, false);
            contentStream.setFont(this.font, 11);

            top = a4.getTop() - 20;
        }

        int cw1 = PDFUtil.getContentWidth(bp.text, this.font, 11);
        int cw2 = PDFUtil.getContentWidth(bp.pageNo, this.font, 11);

        int right = a4.getRight() - 18;
        int w = 4 * (int) Math.ceil((right - (a4.getLeft() + cw1 + 10)) / 4);
        contentStream.setLineDashPattern(new float[] { 1, 3 }, 0);
        contentStream.moveTo(right - w - 1, top - 10);
        contentStream.lineTo(right, top - 10);
        contentStream.stroke();

        contentStream.beginText();
        contentStream.newLineAtOffset(a4.getLeft(), top - 10);
        contentStream.showText(bp.text);
        contentStream.endText();

        contentStream.beginText();
        contentStream.newLineAtOffset(a4.getRight() - cw2, top - 10);
        contentStream.showText(bp.pageNo);
        contentStream.endText();

        top -= 15;
    }

    contentStream.close();
}

From source file:uia.pdf.PDFMakerTest.java

License:Apache License

@Test
public void justTest() throws Exception {
    PDDocument doc = new PDDocument();
    try {//from  w  w  w.  ja  v a  2  s.  c  o m
        PDPage page = new PDPage();
        doc.addPage(page);

        PDPageContentStream contents = new PDPageContentStream(doc, page, AppendMode.APPEND, false, false);
        contents.moveTo(20, 650);
        contents.lineTo(500, 650);
        contents.stroke();
        contents.close();

        PDImageXObject pdImage = PDImageXObject
                .createFromFile(PDFMakerTest.class.getResource("sample.jpg").getFile(), doc);
        contents = new PDPageContentStream(doc, page, AppendMode.APPEND, false, false);
        contents.drawImage(pdImage, 20, 700);
        contents.close();

        contents = new PDPageContentStream(doc, page, AppendMode.APPEND, false, false);
        contents.moveTo(20, 550);
        contents.lineTo(500, 550);
        contents.stroke();
        contents.close();

        doc.save("C:\\TEMP\\IMAGE.PDF");
    } finally {
        doc.close();
    }

}