Search in sources :

Example 11 with COSName

use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.

the class PDStream method createInputStream.

/**
 * This will get a stream with some filters applied but not others. This is
 * useful when doing images, ie filters = [flate,dct], we want to remove
 * flate but leave dct
 *
 * @param stopFilters  A list of filters to stop decoding at.
 * @return A stream with decoded data.
 * @throws IOException If there is an error processing the stream.
 */
public InputStream createInputStream(List<String> stopFilters) throws IOException {
    InputStream is = stream.createRawInputStream();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    List<COSName> filters = getFilters();
    if (filters != null) {
        for (int i = 0; i < filters.size(); i++) {
            COSName nextFilter = filters.get(i);
            if ((stopFilters != null) && stopFilters.contains(nextFilter.getName())) {
                break;
            } else {
                Filter filter = FilterFactory.INSTANCE.getFilter(nextFilter);
                filter.decode(is, os, stream, i);
                IOUtils.closeQuietly(is);
                is = new ByteArrayInputStream(os.toByteArray());
                os.reset();
            }
        }
    }
    return is;
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) Filter(com.tom_roush.pdfbox.filter.Filter) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) COSInputStream(com.tom_roush.pdfbox.cos.COSInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 12 with COSName

use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.

the class PDStream method addCompression.

/**
 * If there are not compression filters on the current stream then this will
 * add a compression filter, flate compression for example.
 *
 * @deprecated This method is inefficient. To copying an existing InputStream, use
 *             {@link #PDStream(PDDocument, InputStream, COSName)} instead, with
 *             COSName.FLATE_DECODE as the final argument.
 *
 *             Otherwise, to write new compressed data, use {@link #createOutputStream(COSName)},
 *             with COSName.FLATE_DECODE as the argument.
 */
@Deprecated
public void addCompression() {
    List<COSName> filters = getFilters();
    if (filters == null) {
        if (stream.getLength() > 0) {
            OutputStream out = null;
            try {
                byte[] bytes = IOUtils.toByteArray(stream.createInputStream());
                out = stream.createOutputStream(COSName.FLATE_DECODE);
                out.write(bytes);
            } catch (IOException e) {
                // not much else we can do here without breaking the existing API, sorry.
                throw new RuntimeException(e);
            } finally {
                IOUtils.closeQuietly(out);
            }
        } else {
            filters = new ArrayList<COSName>();
            filters.add(COSName.FLATE_DECODE);
            setFilters(filters);
        }
    }
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 13 with COSName

use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.

the class PDStream method getFileFilters.

/**
 * This will get the list of filters that are associated with this stream.
 * Or null if there are none.
 *
 * @return A list of all encoding filters to apply to this stream.
 */
public List<String> getFileFilters() {
    List<String> retval = null;
    COSBase filters = stream.getDictionaryObject(COSName.F_FILTER);
    if (filters instanceof COSName) {
        COSName name = (COSName) filters;
        retval = new COSArrayList<String>(name.getName(), name, stream, COSName.F_FILTER);
    } else if (filters instanceof COSArray) {
        retval = COSArrayList.convertCOSNameCOSArrayToList((COSArray) filters);
    }
    return retval;
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 14 with COSName

use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.

the class PDResources method add.

/**
 * Adds the given resource if it does not already exist.
 */
private COSName add(COSName kind, String prefix, COSObjectable object) {
    // return the existing key if the item exists already
    COSDictionary dict = (COSDictionary) resources.getDictionaryObject(kind);
    if (dict != null && dict.containsValue(object.getCOSObject())) {
        return dict.getKeyForValue(object.getCOSObject());
    }
    // AcroForm default resources of a loaded PDF.
    if (dict != null && COSName.FONT.equals(kind)) {
        for (Map.Entry<COSName, COSBase> entry : dict.entrySet()) {
            if (entry.getValue() instanceof COSObject && object.getCOSObject() == ((COSObject) entry.getValue()).getObject()) {
                return entry.getKey();
            }
        }
    }
    // add the item with a new key
    COSName name = createKey(kind, prefix);
    put(kind, name, object);
    return name;
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSName(com.tom_roush.pdfbox.cos.COSName) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase) HashMap(java.util.HashMap) Map(java.util.Map)

Example 15 with COSName

use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.

the class PDFontDescriptor method setFontStretch.

/**
 * This will set the font stretch.
 *
 * @param fontStretch The new stretch for the font.
 */
public void setFontStretch(String fontStretch) {
    COSName name = null;
    if (fontStretch != null) {
        name = COSName.getPDFName(fontStretch);
    }
    dic.setItem(COSName.FONT_STRETCH, name);
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName)

Aggregations

COSName (com.tom_roush.pdfbox.cos.COSName)83 COSBase (com.tom_roush.pdfbox.cos.COSBase)50 COSArray (com.tom_roush.pdfbox.cos.COSArray)27 IOException (java.io.IOException)24 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)23 COSObject (com.tom_roush.pdfbox.cos.COSObject)11 COSString (com.tom_roush.pdfbox.cos.COSString)11 HashMap (java.util.HashMap)10 Map (java.util.Map)10 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)9 COSStream (com.tom_roush.pdfbox.cos.COSStream)7 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)7 MissingOperandException (com.tom_roush.pdfbox.contentstream.operator.MissingOperandException)5 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)5 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)5 PDXObject (com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)5 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)4 PDTransparencyGroup (com.tom_roush.pdfbox.pdmodel.graphics.form.PDTransparencyGroup)4 OutputStream (java.io.OutputStream)4 Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)3