Example usage for com.itextpdf.text.pdf PdfName FORM

List of usage examples for com.itextpdf.text.pdf PdfName FORM

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfName FORM.

Prototype

PdfName FORM

To view the source code for com.itextpdf.text.pdf PdfName FORM.

Click Source Link

Document

A name

Usage

From source file:cz.muni.pdfjbim.PdfImageExtractor.java

License:Apache License

private List<Image> getImagesFromPdfDict(PdfDictionary dict, PdfReader doc) throws IOException {
    List<Image> images = new ArrayList<Image>();
    PdfDictionary res = (PdfDictionary) (PdfReader.getPdfObject(dict.get(PdfName.RESOURCES)));
    PdfDictionary xobj = (PdfDictionary) (PdfReader.getPdfObject(res.get(PdfName.XOBJECT)));

    if (xobj != null) {
        for (PdfName name : xobj.getKeys()) {
            PdfObject obj = xobj.get(name);
            if (obj.isIndirect()) {
                PdfDictionary tg = (PdfDictionary) (PdfReader.getPdfObject(obj));
                PdfName subtype = (PdfName) (PdfReader.getPdfObject(tg.get(PdfName.SUBTYPE)));
                if (PdfName.IMAGE.equals(subtype)) {
                    int xrefIdx = ((PRIndirectReference) obj).getNumber();
                    PdfObject pdfObj = doc.getPdfObject(xrefIdx);
                    PdfStream str = (PdfStream) (pdfObj);
                    byte[] bytes = PdfReader.getStreamBytesRaw((PRStream) str);

                    String filter = tg.get(PdfName.FILTER).toString();
                    String width = tg.get(PdfName.WIDTH).toString();
                    String height = tg.get(PdfName.HEIGHT).toString();
                    String bpp = tg.get(PdfName.BITSPERCOMPONENT).toString();

                    if ("/FlateDecode".equals(filter)) {
                        bytes = PdfReader.FlateDecode(bytes, true);
                        try {
                            images.add(Image.getInstance(bytes));
                        } catch (BadElementException ex) {
                            log.warn("problem to process FlatDecoded Image", ex);
                        }//from w  w w .j  a  v  a 2s. c  o m
                    } else if (PdfName.FORM.equals(subtype) || PdfName.GROUP.equals(subtype)) {
                        images.addAll(getImagesFromPdfDict(tg, doc));
                    }
                }
            }
        }
    }
    return images;
}

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;/*  ww w . ja  v  a 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();
}