Example usage for org.apache.pdfbox.cos COSName LENGTH

List of usage examples for org.apache.pdfbox.cos COSName LENGTH

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSName LENGTH.

Prototype

COSName LENGTH

To view the source code for org.apache.pdfbox.cos COSName LENGTH.

Click Source Link

Usage

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox.PDFAsVisualSignatureBuilder.java

License:EUPL

public void injectAppearanceStreams(PDStream holderFormStream, PDStream innterFormStream,
        PDStream imageFormStream, String imageObjectName, String imageName, String innerFormName,
        PDFAsVisualSignatureDesigner properties) throws IOException {

    double m00 = getStructure().getAffineTransform().getScaleX();
    double m10 = getStructure().getAffineTransform().getShearY();
    double m01 = getStructure().getAffineTransform().getShearX();
    double m11 = getStructure().getAffineTransform().getScaleY();
    double m02 = getStructure().getAffineTransform().getTranslateX();
    double m12 = getStructure().getAffineTransform().getTranslateY();

    String holderFormComment = "q " + m00 + " " + m10 + " " + m01 + " " + m11 + " " + m02 + " " + m12 + " cm /"
            + innerFormName + " Do Q";

    logger.debug("Holder Form Stream: " + holderFormComment);

    String innerFormComment = getStructure().getInnterFormStream().getInputStreamAsString();

    // PDFBOX-3321 avoid length being written as an indirect object,
    // to prevent call of heuristic readUntilEndStream()
    getStructure().getHolderFormStream().getStream().setInt(COSName.LENGTH, 0);
    getStructure().getInnterFormStream().getStream().setInt(COSName.LENGTH, 0);
    //      getStructure().getImageFormStream().getStream().setInt(COSName.LENGTH, 0);

    appendRawCommands(getStructure().getHolderFormStream().createOutputStream(), holderFormComment);
    appendRawCommands(getStructure().getInnterFormStream().createOutputStream(), innerFormComment);
    logger.debug("Injected appearance stream to pdf");

}

From source file:com.aaasec.sigserv.csspsupport.pdfbox.modifications.CsCOSWriter.java

License:Apache License

/**
 * visitFromStream method comment.//w  w w .  j a v a 2  s  .c om
 *
 * @param obj The object that is being visited.
 *
 * @throws COSVisitorException If there is an exception while visiting this
 * object.
 *
 * @return null
 */
public Object visitFromStream(COSStream obj) throws COSVisitorException {
    InputStream input = null;
    try {
        if (willEncrypt) {
            document.getSecurityHandler().encryptStream(obj, currentObjectKey.getNumber(),
                    currentObjectKey.getGeneration());
        }

        COSObject lengthObject = null;
        // check if the length object is required to be direct, like in
        // a cross reference stream dictionary
        COSBase lengthEntry = obj.getDictionaryObject(COSName.LENGTH);
        String type = obj.getNameAsString(COSName.TYPE);
        if (lengthEntry != null && lengthEntry.isDirect() || "XRef".equals(type)) {
            // the length might be the non encoded length,
            // set the real one as direct object
            COSInteger cosInteger = COSInteger.get(obj.getFilteredLength());
            cosInteger.setDirect(true);
            obj.setItem(COSName.LENGTH, cosInteger);

        } else {
            // make the length an implicit indirect object
            // set the length of the stream and write stream dictionary
            lengthObject = new COSObject(null);

            obj.setItem(COSName.LENGTH, lengthObject);
        }
        input = obj.getFilteredStream();
        //obj.accept(this);
        // write the stream content
        visitFromDictionary(obj);
        getStandardOutput().write(STREAM);
        getStandardOutput().writeCRLF();
        byte[] buffer = new byte[1024];
        int amountRead = 0;
        int totalAmountWritten = 0;
        while ((amountRead = input.read(buffer, 0, 1024)) != -1) {
            getStandardOutput().write(buffer, 0, amountRead);
            totalAmountWritten += amountRead;
        }
        // set the length as an indirect object
        if (lengthObject != null) {
            lengthObject.setObject(COSInteger.get(totalAmountWritten));
        }
        getStandardOutput().writeCRLF();
        getStandardOutput().write(ENDSTREAM);
        getStandardOutput().writeEOL();
        return null;
    } catch (Exception e) {
        throw new COSVisitorException(e);
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                throw new COSVisitorException(e);
            }
        }
    }
}

From source file:pdfpicmangler.PDPng.java

License:Open Source License

/**
 * Construct from a stream./*from  ww  w.ja v  a  2 s .co  m*/
 * 
 * @param doc The document to create the image as part of.
 * @param is The stream that contains the png data.
 * @throws IOException If there is an error reading the png data.
 */
public PDPng(PDDocument doc, InputStream is) throws IOException {
    super(doc, "png");

    System.out.println("reading in png");

    COSDictionary dic = getCOSStream();

    dic.setItem(COSName.SUBTYPE, COSName.IMAGE);
    //dic.setItem(COSName.TYPE, COSName.XOBJECT);

    data = getCOSStream().createFilteredStream();

    readPng(is);

    setWidth(imageWidth);
    setHeight(imageHeight);
    dic.setInt(COSName.BITS_PER_COMPONENT, bitDepth);

    if ((colorType & PNG_TYPE_PALETTE) != 0) {
        getCOSStream().setItem(COSName.COLORSPACE, paldata);
    } else if ((colorType & PNG_TYPE_COLOR) != 0) {
        setColorSpace(PDDeviceRGB.INSTANCE);
    } else {
        setColorSpace(new PDDeviceGray());
    }

    COSDictionary filterParams = new COSDictionary();
    filterParams.setInt(COSName.PREDICTOR, 15); // png adaptive predictor
    filterParams.setInt(COSName.COLORS,
            ((colorType & PNG_TYPE_COLOR) == 0 || (colorType & PNG_TYPE_PALETTE) != 0) ? 1 : 3);
    filterParams.setInt(COSName.BITS_PER_COMPONENT, bitDepth);
    filterParams.setInt(COSName.COLUMNS, imageWidth);
    filterParams.setDirect(true);

    dic.setItem(COSName.DECODE_PARMS, filterParams);
    dic.setItem(COSName.FILTER, COSName.FLATE_DECODE);

    dic.setInt(COSName.LENGTH, dataLen);
    dic.getDictionaryObject(COSName.LENGTH).setDirect(true);
}

From source file:se.streamsource.streamflow.web.application.pdf.Underlay.java

License:Apache License

private COSStream makeUniqObjectNames(Map objectNameMap, COSStream stream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(10240);

    byte[] buf = new byte[10240];
    int read;/*from w w w .  j av  a  2 s  . c  o m*/
    InputStream is = stream.getUnfilteredStream();
    while ((read = is.read(buf)) > -1) {
        baos.write(buf, 0, read);
    }

    buf = baos.toByteArray();
    baos = new ByteArrayOutputStream(buf.length + 100);
    StringBuffer sbObjectName = new StringBuffer(10);
    boolean bInObjectIdent = false;
    boolean bInText = false;
    boolean bInEscape = false;
    for (int i = 0; i < buf.length; i++) {
        byte b = buf[i];

        if (!bInEscape) {
            if (!bInText && b == '(') {
                bInText = true;
            }
            if (bInText && b == ')') {
                bInText = false;
            }
            if (b == '\\') {
                bInEscape = true;
            }

            if (!bInText && !bInEscape) {
                if (b == '/') {
                    bInObjectIdent = true;
                } else if (bInObjectIdent && Character.isWhitespace((char) b)) {
                    bInObjectIdent = false;

                    String objectName = sbObjectName.toString().substring(1);
                    String newObjectName = objectName + "overlay";
                    baos.write('/');
                    baos.write(newObjectName.getBytes());

                    objectNameMap.put(objectName, COSName.getPDFName(newObjectName));

                    sbObjectName.delete(0, sbObjectName.length());
                }
            }

            if (bInObjectIdent) {
                sbObjectName.append((char) b);
                continue;
            }
        } else {
            bInEscape = false;
        }

        baos.write(b);
    }

    COSDictionary streamDict = new COSDictionary();
    streamDict.setInt(COSName.LENGTH, baos.size());
    COSStream output = new COSStream(streamDict, pdfDocument.getDocument().getScratchFile());
    output.setFilters(stream.getFilters());
    OutputStream os = output.createUnfilteredStream();
    baos.writeTo(os);
    os.close();

    return output;
}