Example usage for com.itextpdf.text.pdf PdfStream getAsDict

List of usage examples for com.itextpdf.text.pdf PdfStream getAsDict

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfStream getAsDict.

Prototype

public PdfDictionary getAsDict(final PdfName key) 

Source Link

Document

Returns a PdfObject as a PdfDictionary, resolving indirect references.

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  . ja  va  2 s.  c o  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();
}