Search in sources :

Example 56 with COSName

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

the class PDInlineImage method getFilters.

/**
 * Returns a list of filters applied to this stream, or null if there are none.
 *
 * @return a list of filters applied to this stream
 */
// TODO return an empty list if there are none?
public List<String> getFilters() {
    List<String> names = null;
    COSBase filters = parameters.getDictionaryObject(COSName.F, COSName.FILTER);
    if (filters instanceof COSName) {
        COSName name = (COSName) filters;
        names = new COSArrayList<String>(name.getName(), name, parameters, COSName.FILTER);
    } else if (filters instanceof COSArray) {
        names = COSArrayList.convertCOSNameCOSArrayToList((COSArray) filters);
    }
    return names;
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 57 with COSName

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

the class PDOptionalContentProperties method getBaseState.

/**
 * Returns the base state for optional content groups.
 * @return the base state
 */
public BaseState getBaseState() {
    COSDictionary d = getD();
    COSName name = (COSName) d.getItem(COSName.BASE_STATE);
    return BaseState.valueOf(name);
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSName(com.tom_roush.pdfbox.cos.COSName)

Example 58 with COSName

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

the class PDColorSpace method create.

/**
 * Creates a color space given a name or array. Abbreviated device color names are not supported
 * here, please replace them first. This method is for PDFBox internal use only, others should
 * use {@link #create(COSBase, PDResources)}.
 *
 * @param colorSpace the color space COS object
 * @param resources the current resources.
 * @param wasDefault if current color space was used by a default color space.
 * @return a new color space.
 * @throws MissingResourceException if the color space is missing in the resources dictionary
 * @throws IOException if the color space is unknown or cannot be created.
 */
public static PDColorSpace create(COSBase colorSpace, PDResources resources, boolean wasDefault) throws IOException {
    if (colorSpace instanceof COSObject) {
        return createFromCOSObject((COSObject) colorSpace, resources);
    } else if (colorSpace instanceof COSName) {
        COSName name = (COSName) colorSpace;
        // default color spaces
        if (resources != null) {
            COSName defaultName = null;
            if (name.equals(COSName.DEVICECMYK) && resources.hasColorSpace(COSName.DEFAULT_CMYK)) {
                defaultName = COSName.DEFAULT_CMYK;
            } else if (name.equals(COSName.DEVICERGB) && resources.hasColorSpace(COSName.DEFAULT_RGB)) {
                defaultName = COSName.DEFAULT_RGB;
            } else if (name.equals(COSName.DEVICEGRAY) && resources.hasColorSpace(COSName.DEFAULT_GRAY)) {
                defaultName = COSName.DEFAULT_GRAY;
            }
            if (resources.hasColorSpace(defaultName) && !wasDefault) {
                return resources.getColorSpace(defaultName, true);
            }
        }
        // built-in color spaces
        if (name == COSName.DEVICECMYK) {
            // return PDDeviceCMYK.INSTANCE;
            Log.e("PdfBox-Android", "Unsupported color space kind: " + name + ". Will try DeviceRGB instead");
            return PDDeviceRGB.INSTANCE;
        } else if (name == COSName.DEVICERGB) {
            return PDDeviceRGB.INSTANCE;
        } else if (name == COSName.DEVICEGRAY) {
            return PDDeviceGray.INSTANCE;
        } else if (name == COSName.PATTERN) {
            // return new PDPattern(resources);
            Log.e("PdfBox-Android", "Unsupported color space kind: " + name + ". Will try DeviceRGB instead");
            return PDDeviceRGB.INSTANCE;
        } else if (resources != null) {
            if (!resources.hasColorSpace(name)) {
                throw new MissingResourceException("Missing color space: " + name.getName());
            }
            return resources.getColorSpace(name);
        } else {
            throw new MissingResourceException("Unknown color space: " + name.getName());
        }
    } else if (colorSpace instanceof COSArray) {
        COSArray array = (COSArray) colorSpace;
        if (array.size() == 0) {
            throw new IOException("Colorspace array is empty");
        }
        COSBase base = array.getObject(0);
        if (!(base instanceof COSName)) {
            throw new IOException("First element in colorspace array must be a name");
        }
        COSName name = (COSName) base;
        if (name == COSName.CALGRAY) {
            // return new PDCalGray(array); TODO: PdfBox-Android
            Log.e("PdfBox-Android", "Unsupported color space kind: " + name + ". Will try DeviceRGB instead");
            return PDDeviceRGB.INSTANCE;
        } else if (name == COSName.CALRGB) {
            // return new PDCalRGB(array);
            Log.e("PdfBox-Android", "Unsupported color space kind: " + name + ". Will try DeviceRGB instead");
            return PDDeviceRGB.INSTANCE;
        } else if (name == COSName.DEVICEN) {
            // return new PDDeviceN(array);
            Log.e("PdfBox-Android", "Unsupported color space kind: " + name + ". Will try DeviceRGB instead");
            return PDDeviceRGB.INSTANCE;
        } else if (name == COSName.INDEXED) {
            // return new PDIndexed(array);
            Log.e("PdfBox-Android", "Unsupported color space kind: " + name + ". Will try DeviceRGB instead");
            return PDDeviceRGB.INSTANCE;
        } else if (name == COSName.SEPARATION) {
            // return new PDSeparation(array);
            Log.e("PdfBox-Android", "Unsupported color space kind: " + name + ". Will try DeviceRGB instead");
            return PDDeviceRGB.INSTANCE;
        } else if (name == COSName.ICCBASED) {
            // return PDICCBased.create(array, resources);
            Log.e("PdfBox-Android", "Unsupported color space kind: " + name + ". Will try DeviceRGB instead");
            return PDDeviceRGB.INSTANCE;
        } else if (name == COSName.LAB) {
            // return new PDLab(array);
            Log.e("PdfBox-Android", "Unsupported color space kind: " + name + ". Will try DeviceRGB instead");
            return PDDeviceRGB.INSTANCE;
        } else if (name == COSName.PATTERN) {
            if (array.size() == 1) {
            // return new PDPattern(resources);
            } else {
            // return new PDPattern(resources, PDColorSpace.create(array.get(1)));
            }
            Log.e("PdfBox-Android", "Unsupported color space kind: " + name + ". Will try DeviceRGB instead");
            return PDDeviceRGB.INSTANCE;
        } else if (name == COSName.DEVICECMYK || name == COSName.DEVICERGB || name == COSName.DEVICEGRAY) {
            // not allowed in an array, but we sometimes encounter these regardless
            return create(name, resources, wasDefault);
        } else {
            throw new IOException("Invalid color space kind: " + name);
        }
    } else {
        throw new IOException("Expected a name or array but got: " + colorSpace);
    }
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSObject(com.tom_roush.pdfbox.cos.COSObject) MissingResourceException(com.tom_roush.pdfbox.pdmodel.MissingResourceException) COSBase(com.tom_roush.pdfbox.cos.COSBase) IOException(java.io.IOException)

Example 59 with COSName

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

the class FDFFieldTest method testCOSNameValue.

@Test
public void testCOSNameValue() throws IOException {
    String testString = "Yes";
    COSName testCOSSName = COSName.getPDFName(testString);
    FDFField field = new FDFField();
    field.setValue(testCOSSName);
    assertEquals(testCOSSName, (COSName) field.getCOSValue());
    assertEquals(testString, field.getValue());
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) COSString(com.tom_roush.pdfbox.cos.COSString) Test(org.junit.Test)

Example 60 with COSName

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

the class SetFontAndSize method process.

@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException {
    if (arguments.size() < 2) {
        throw new MissingOperandException(operator, arguments);
    }
    COSBase base0 = arguments.get(0);
    COSBase base1 = arguments.get(1);
    if (!(base0 instanceof COSName)) {
        return;
    }
    if (!(base1 instanceof COSNumber)) {
        return;
    }
    COSName fontName = (COSName) base0;
    float fontSize = ((COSNumber) base1).floatValue();
    context.getGraphicsState().getTextState().setFontSize(fontSize);
    PDFont font = context.getResources().getFont(fontName);
    if (font == null) {
        Log.w("PdfBox-Android", "font '" + fontName.getName() + "' not found in resources");
    }
    context.getGraphicsState().getTextState().setFont(font);
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) COSName(com.tom_roush.pdfbox.cos.COSName) MissingOperandException(com.tom_roush.pdfbox.contentstream.operator.MissingOperandException) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) COSBase(com.tom_roush.pdfbox.cos.COSBase)

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