Example usage for com.itextpdf.text.pdf PdfContentByte getInternalBuffer

List of usage examples for com.itextpdf.text.pdf PdfContentByte getInternalBuffer

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfContentByte getInternalBuffer.

Prototype

public ByteBuffer getInternalBuffer() 

Source Link

Document

Gets the internal buffer.

Usage

From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpContentOperator.java

License:Open Source License

public void invoke(PdfContentStreamProcessor pdfContentStreamProcessor, PdfLiteral operator,
        ArrayList<PdfObject> operands) throws Exception {
    String operatorStr = operator.toString();
    PdfContentByte canvas = cleanUpStrategy.getContext().getCanvas();
    PRStream xFormStream = null;/*from  w w w.java 2  s  .co m*/
    boolean disableOutput = pathConstructionOperators.contains(operatorStr)
            || pathPaintingOperators.contains(operatorStr) || clippingPathOperators.contains(operatorStr);
    GraphicsState gs = pdfContentStreamProcessor.gs();

    // key - number of a string in the TJ operator, value - number following the string; the first number without string (if it's presented) is stored under 0.
    // BE AWARE: zero-length strings are ignored!!!
    Map<Integer, Float> structuredTJoperands = null;

    if ("Do".equals(operatorStr)) {
        if (operands.size() == 2 && operands.get(0).isName()) {
            PdfDictionary xObjResources = cleanUpStrategy.getContext().getResources()
                    .getAsDict(PdfName.XOBJECT);

            if (xObjResources != null) {
                PdfStream xObj = xObjResources.getAsStream((PdfName) operands.get(0));

                if (xObj instanceof PRStream && xObj.getAsName(PdfName.SUBTYPE) != null
                        && xObj.getAsName(PdfName.SUBTYPE).compareTo(PdfName.FORM) == 0) {
                    xFormStream = (PRStream) xObj;
                    cleanUpStrategy.registerNewContext(xObj.getAsDict(PdfName.RESOURCES), null);
                }
            }
        }
    }

    originalContentOperator.invoke(pdfContentStreamProcessor, operator, operands);
    List<PdfCleanUpContentChunk> chunks = cleanUpStrategy.getChunks();

    if (xFormStream != null) {
        xFormStream.setData(cleanUpStrategy.getContext().getCanvas()
                .toPdf(cleanUpStrategy.getContext().getCanvas().getPdfWriter()));
        cleanUpStrategy.popContext();
        canvas = cleanUpStrategy.getContext().getCanvas();
    }

    if ("Do".equals(operatorStr)) {
        if (chunks.size() > 0 && chunks.get(0) instanceof PdfCleanUpContentChunk.Image) {
            PdfCleanUpContentChunk.Image chunk = (PdfCleanUpContentChunk.Image) chunks.get(0);

            if (chunk.isVisible()) {
                PdfDictionary xObjResources = cleanUpStrategy.getContext().getResources()
                        .getAsDict(PdfName.XOBJECT);
                PRStream imageStream = (PRStream) xObjResources.getAsStream((PdfName) operands.get(0));
                updateImageStream(imageStream, chunk.getNewImageData());
            } else {
                disableOutput = true;
            }
        }
    } else if (lineStyleOperators.contains(operatorStr)) {
        disableOutput = true;
    } else if (textShowingOperators.contains(operatorStr)
            && !allChunksAreVisible(cleanUpStrategy.getChunks())) {
        disableOutput = true;

        if ("'".equals(operatorStr)) {
            canvas.getInternalBuffer().append(TStar);
        } else if ("\"".equals(operatorStr)) {
            operands.get(0).toPdf(canvas.getPdfWriter(), canvas.getInternalBuffer());
            canvas.getInternalBuffer().append(Tw);

            operands.get(1).toPdf(canvas.getPdfWriter(), canvas.getInternalBuffer());
            canvas.getInternalBuffer().append(TcTStar);
        } else if ("TJ".equals(operatorStr)) {
            structuredTJoperands = structureTJarray((PdfArray) operands.get(0));
        }

        writeTextChunks(structuredTJoperands, chunks, canvas, gs.getCharacterSpacing(), gs.getWordSpacing(),
                gs.getFontSize(), gs.getHorizontalScaling());
    } else if (pathPaintingOperators.contains(operatorStr)) {
        writePath(operatorStr, canvas, gs.getColorSpaceStroke());
    } else if (strokeColorOperators.contains(operatorStr)) {
        // Replace current color with the new one.
        cleanUpStrategy.getContext().popStrokeColor();
        cleanUpStrategy.getContext().pushStrokeColor(operands);
    } else if ("q".equals(operatorStr)) {
        cleanUpStrategy.getContext().pushStrokeColor(cleanUpStrategy.getContext().peekStrokeColor());
    } else if ("Q".equals(operatorStr)) {
        cleanUpStrategy.getContext().popStrokeColor();
    }

    if (!disableOutput) {
        writeOperands(canvas, operands);
    }

    cleanUpStrategy.clearChunks();
}

From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpContentOperator.java

License:Open Source License

private void writeOperands(PdfContentByte canvas, List<PdfObject> operands) throws IOException {
    int index = 0;

    for (PdfObject o : operands) {
        toPdf(o, canvas.getPdfWriter(), canvas.getInternalBuffer());
        canvas.getInternalBuffer().append(operands.size() > ++index ? (byte) ' ' : (byte) '\n');
    }/*from   www.  j  av  a2 s.  com*/
}

From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpContentOperator.java

License:Open Source License

/**
 * Renders parts of text which are visible.
 *//*from   w  w w  .  j a  v a 2 s  .c om*/
private void writeTextChunks(Map<Integer, Float> structuredTJoperands, List<PdfCleanUpContentChunk> chunks,
        PdfContentByte canvas, float characterSpacing, float wordSpacing, float fontSize,
        float horizontalScaling) throws IOException {
    canvas.setCharacterSpacing(0);
    canvas.setWordSpacing(0);
    canvas.getInternalBuffer().append((byte) '[');

    float convertedCharacterSpacing = -characterSpacing * 1000f / fontSize;
    float convertedWordSpacing = -wordSpacing * 1000f / fontSize;

    float shift = structuredTJoperands != null ? structuredTJoperands.get(0) : 0;
    PdfCleanUpContentChunk.Text prevChunk = null;

    for (PdfCleanUpContentChunk chunk : chunks) {
        PdfCleanUpContentChunk.Text textChunk = (PdfCleanUpContentChunk.Text) chunk;

        if (prevChunk != null && prevChunk.getNumOfStrTextBelongsTo() != textChunk.getNumOfStrTextBelongsTo()
                && structuredTJoperands != null) {
            shift += structuredTJoperands.get(prevChunk.getNumOfStrTextBelongsTo());
        }

        if (textChunk.isVisible()) {
            if (Float.compare(shift, 0.0f) != 0 && Float.compare(shift, -0.0f) != 0) {
                canvas.getInternalBuffer().append(shift).append(' ');
            }

            textChunk.getText().toPdf(canvas.getPdfWriter(), canvas.getInternalBuffer());
            canvas.getInternalBuffer().append(' ');

            shift = convertedCharacterSpacing + (isSpace(textChunk) ? convertedWordSpacing : 0);
        } else {
            shift += getUnscaledTextChunkWidth(textChunk, characterSpacing, wordSpacing, fontSize,
                    horizontalScaling);
        }

        prevChunk = textChunk;
    }

    if (Float.compare(shift, 0.0f) != 0 && Float.compare(shift, -0.0f) != 0) {
        canvas.getInternalBuffer().append(shift);
    }

    canvas.getInternalBuffer().append(TJ);

    if (Float.compare(characterSpacing, 0.0f) != 0 && Float.compare(characterSpacing, -0.0f) != 0) {
        new PdfNumber(characterSpacing).toPdf(canvas.getPdfWriter(), canvas.getInternalBuffer());
        canvas.getInternalBuffer().append(Tc);
    }

    if (Float.compare(wordSpacing, 0.0f) != 0 && Float.compare(wordSpacing, -0.0f) != 0) {
        new PdfNumber(wordSpacing).toPdf(canvas.getPdfWriter(), canvas.getInternalBuffer());
        canvas.getInternalBuffer().append(Tw);
    }
}

From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpContentOperator.java

License:Open Source License

private void writePath(String operatorStr, PdfContentByte canvas, PdfName strokeColorSpace) throws IOException {
    if (nwFillOperators.contains(operatorStr)) {
        writePath(cleanUpStrategy.getCurrentFillPath(), f, canvas);
    } else if (eoFillOperators.contains(operatorStr)) {
        writePath(cleanUpStrategy.getCurrentFillPath(), eoF, canvas);
    }//ww w  . ja va  2s  .  co m

    if (strokeOperators.contains(operatorStr)) {
        writeStroke(canvas, cleanUpStrategy.getCurrentStrokePath(), strokeColorSpace);
    }

    if (cleanUpStrategy.isClipped()) {
        if (!cleanUpStrategy.getNewClipPath().isEmpty()) {
            byte[] clippingOperator = (cleanUpStrategy
                    .getClippingRule() == PathPaintingRenderInfo.NONZERO_WINDING_RULE) ? W : eoW;
            writePath(cleanUpStrategy.getNewClipPath(), clippingOperator, canvas);
        } else {
            // If the clipping path from the source document is cleaned (it happens when reduction
            // area covers the path completely), then you should treat it as an empty set (no points
            // are included in the path). Then the current clipping path (which is the intersection
            // between previous clipping path and the new one) is also empty set, which means that
            // there is no visible content at all. But at the same time as we removed the clipping
            // path, the invisible content would become visible. So, to emulate the correct result,
            // we would simply put a degenerate clipping path which consists of a single point at (0, 0).
            Path degeneratePath = new Path();
            degeneratePath.moveTo(0, 0);
            writePath(degeneratePath, W, canvas);
        }
        canvas.getInternalBuffer().append(n);
        cleanUpStrategy.setClipped(false);
    }
}

From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpContentOperator.java

License:Open Source License

private void writePath(Path path, byte[] pathPaintingOperator, PdfContentByte canvas) throws IOException {
    if (path.isEmpty()) {
        return;/*from  ww  w  . j  av  a  2  s  .  c o  m*/
    }

    for (Subpath subpath : path.getSubpaths()) {
        writeMoveTo(subpath.getStartPoint(), canvas);

        for (Shape segment : subpath.getSegments()) {
            if (segment instanceof BezierCurve) {
                writeBezierCurve((BezierCurve) segment, canvas);
            } else {
                writeLine((Line) segment, canvas);
            }
        }

        if (subpath.isClosed()) {
            canvas.getInternalBuffer().append(h);
        }
    }

    if (pathPaintingOperator != null) {
        canvas.getInternalBuffer().append(pathPaintingOperator);
    }
}

From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpContentOperator.java

License:Open Source License

private void writeMoveTo(Point2D destinationPoint, PdfContentByte canvas) throws IOException {
    new PdfNumber(destinationPoint.getX()).toPdf(canvas.getPdfWriter(), canvas.getInternalBuffer());
    canvas.getInternalBuffer().append(' ');
    new PdfNumber(destinationPoint.getY()).toPdf(canvas.getPdfWriter(), canvas.getInternalBuffer());
    canvas.getInternalBuffer().append(m);
}

From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpContentOperator.java

License:Open Source License

private void writeBezierCurve(BezierCurve curve, PdfContentByte canvas) throws IOException {
    List<Point2D> basePoints = curve.getBasePoints();
    Point2D p2 = basePoints.get(1);
    Point2D p3 = basePoints.get(2);
    Point2D p4 = basePoints.get(3);

    new PdfNumber(p2.getX()).toPdf(canvas.getPdfWriter(), canvas.getInternalBuffer());
    canvas.getInternalBuffer().append(' ');

    new PdfNumber(p2.getY()).toPdf(canvas.getPdfWriter(), canvas.getInternalBuffer());
    canvas.getInternalBuffer().append(' ');

    new PdfNumber(p3.getX()).toPdf(canvas.getPdfWriter(), canvas.getInternalBuffer());
    canvas.getInternalBuffer().append(' ');

    new PdfNumber(p3.getY()).toPdf(canvas.getPdfWriter(), canvas.getInternalBuffer());
    canvas.getInternalBuffer().append(' ');

    new PdfNumber(p4.getX()).toPdf(canvas.getPdfWriter(), canvas.getInternalBuffer());
    canvas.getInternalBuffer().append(' ');

    new PdfNumber(p4.getY()).toPdf(canvas.getPdfWriter(), canvas.getInternalBuffer());
    canvas.getInternalBuffer().append(c);
}

From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpContentOperator.java

License:Open Source License

private void writeLine(Line line, PdfContentByte canvas) throws IOException {
    Point2D destination = line.getBasePoints().get(1);

    new PdfNumber(destination.getX()).toPdf(canvas.getPdfWriter(), canvas.getInternalBuffer());
    canvas.getInternalBuffer().append(' ');

    new PdfNumber(destination.getY()).toPdf(canvas.getPdfWriter(), canvas.getInternalBuffer());
    canvas.getInternalBuffer().append(l);
}

From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpContentOperator.java

License:Open Source License

private void writeStroke(PdfContentByte canvas, Path path, PdfName strokeColorSpace) throws IOException {
    canvas.getInternalBuffer().append(q);

    if (strokeColorSpace != null) {
        strokeColorSpace.toPdf(canvas.getPdfWriter(), canvas.getInternalBuffer());
        canvas.getInternalBuffer().append(' ').append(cs);
    }/*www . j a  v a  2 s .  c  om*/

    List<PdfObject> strokeColorOperands = cleanUpStrategy.getContext().peekStrokeColor();
    String strokeOperatorStr = strokeColorOperands.get(strokeColorOperands.size() - 1).toString();
    // Below expression converts stroke color operator to its fill analogue.
    strokeColorOperands.set(strokeColorOperands.size() - 1, new PdfLiteral(strokeOperatorStr.toLowerCase()));
    writeOperands(canvas, strokeColorOperands);

    writePath(path, f, canvas);

    canvas.getInternalBuffer().append(Q);
}