use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.
the class PDPageContentStream method drawImage.
/**
* Draw an inline image at the x,y coordinates and a certain width and height.
*
* @param inlineImage The inline image to draw.
* @param x The x-coordinate to draw the inline image.
* @param y The y-coordinate to draw the inline image.
* @param width The width of the inline image to draw.
* @param height The height of the inline image to draw.
*
* @throws IOException If there is an error writing to the stream.
* @throws IllegalStateException If the method was called within a text block.
*/
public void drawImage(PDInlineImage inlineImage, float x, float y, float width, float height) throws IOException {
if (inTextMode) {
throw new IllegalStateException("Error: drawImage is not allowed within a text block.");
}
saveGraphicsState();
transform(new Matrix(width, 0, 0, height, x, y));
// create the image dictionary
StringBuilder sb = new StringBuilder();
sb.append(OperatorName.BEGIN_INLINE_IMAGE);
sb.append("\n /W ");
sb.append(inlineImage.getWidth());
sb.append("\n /H ");
sb.append(inlineImage.getHeight());
sb.append("\n /CS ");
sb.append("/");
sb.append(inlineImage.getColorSpace().getName());
if (inlineImage.getDecode() != null && inlineImage.getDecode().size() > 0) {
sb.append("\n /D ");
sb.append("[");
for (COSBase base : inlineImage.getDecode()) {
sb.append(((COSNumber) base).intValue());
sb.append(" ");
}
sb.append("]");
}
if (inlineImage.isStencil()) {
sb.append("\n /IM true");
}
sb.append("\n /BPC ");
sb.append(inlineImage.getBitsPerComponent());
// image dictionary
write(sb.toString());
writeLine();
// binary data
writeOperator(OperatorName.BEGIN_INLINE_IMAGE_DATA);
writeBytes(inlineImage.getData());
writeLine();
writeOperator(OperatorName.END_INLINE_IMAGE);
restoreGraphicsState();
}
use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.
the class PDPageTree method getKids.
/**
* Helper to get kids from malformed PDFs.
* @param node page tree node
* @return list of kids
*/
private List<COSDictionary> getKids(COSDictionary node) {
List<COSDictionary> result = new ArrayList<COSDictionary>();
COSArray kids = node.getCOSArray(COSName.KIDS);
if (kids == null) {
// probably a malformed PDF
return result;
}
for (int i = 0, size = kids.size(); i < size; i++) {
COSBase base = kids.getObject(i);
if (base instanceof COSDictionary) {
result.add((COSDictionary) base);
} else {
Log.w("PdfBox-Android", "COSDictionary expected, but got " + (base == null ? "null" : base.getClass().getSimpleName()));
}
}
return result;
}
use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.
the class PDResources method getColorSpace.
/**
* Returns the color space resource with the given name, or null if none exists. This method is
* for PDFBox internal use only, others should use {@link #getColorSpace(COSName)}.
*
* @param name Name of the color space resource.
* @param wasDefault if current color space was used by a default color space. This parameter is
* to
* @return a new color space.
* @throws IOException if something went wrong.
*/
public PDColorSpace getColorSpace(COSName name, boolean wasDefault) throws IOException {
COSObject indirect = getIndirect(COSName.COLORSPACE, name);
if (cache != null && indirect != null) {
PDColorSpace cached = cache.getColorSpace(indirect);
if (cached != null) {
return cached;
}
}
// get the instance
PDColorSpace colorSpace;
COSBase object = get(COSName.COLORSPACE, name);
if (object != null) {
colorSpace = PDColorSpace.create(object, this, wasDefault);
} else {
colorSpace = PDColorSpace.create(name, this, wasDefault);
}
// we can't cache PDPattern, because it holds page resources, see PDFBOX-2370
if (// TODO: PdfBox-Android
cache != null) /*&& !(colorSpace instanceof PDPattern)*/
{
cache.put(indirect, colorSpace);
}
return colorSpace;
}
use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.
the class PDResources method getIndirect.
/**
* Returns the resource with the given name and kind as an indirect object, or null.
*/
private COSObject getIndirect(COSName kind, COSName name) {
COSDictionary dict = (COSDictionary) resources.getDictionaryObject(kind);
if (dict == null) {
return null;
}
COSBase base = dict.getItem(name);
if (base instanceof COSObject) {
return (COSObject) base;
}
// not an indirect object. Resource may have been added at runtime.
return null;
}
use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.
the class PDFunction method create.
/**
* Create the correct PD Model function based on the COS base function.
*
* @param function The COS function dictionary.
*
* @return The PDModel Function object.
*
* @throws IOException If we are unable to create the PDFunction object.
*/
public static PDFunction create(COSBase function) throws IOException {
if (function == COSName.IDENTITY) {
return new PDFunctionTypeIdentity(null);
}
COSBase base = function;
if (function instanceof COSObject) {
base = ((COSObject) function).getObject();
}
if (!(base instanceof COSDictionary)) {
throw new IOException("Error: Function must be a Dictionary, but is " + base.getClass().getSimpleName());
}
COSDictionary functionDictionary = (COSDictionary) base;
int functionType = functionDictionary.getInt(COSName.FUNCTION_TYPE);
switch(functionType) {
case 0:
return new PDFunctionType0(functionDictionary);
case 2:
return new PDFunctionType2(functionDictionary);
case 3:
return new PDFunctionType3(functionDictionary);
case 4:
return new PDFunctionType4(functionDictionary);
default:
throw new IOException("Error: Unknown function type " + functionType);
}
}
Aggregations