Example usage for com.itextpdf.text.pdf PdfDictionary getDirectObject

List of usage examples for com.itextpdf.text.pdf PdfDictionary getDirectObject

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfDictionary getDirectObject.

Prototype

public PdfObject getDirectObject(final PdfName key) 

Source Link

Document

Returns the PdfObject associated to the specified key, resolving a possible indirect reference to a direct object.

Usage

From source file:de.gbv.marginalia.Annotation.java

License:Open Source License

/**
 * Get a named member of a PdfDictionary as PdfDictionary.
 * This method should better be included in iText.
 * @param dict Which PdfDictionary to get from.
 * @param name Name of the included PdfDictionary.
 *///from w ww  . j a  v a 2s .com
public static PdfDictionary getAsDictionary(PdfDictionary dict, PdfName name) {
    PdfObject obj = dict.getDirectObject(name);
    if (obj == null || !obj.isDictionary())
        return null;
    return (PdfDictionary) obj;
}

From source file:org.gmdev.pdftrick.utils.CustomExtraImgReader.java

License:Open Source License

/**
 * Read a png image with if all other method fails
 * @param ref//  w w  w  .  ja  v a 2  s .  c om
 * @param resultFile
 * @return The BufferedImage obj
 * @throws IOException
 * @throws ImageReadException
 */
public static BufferedImage readIndexedPNG(int ref, String resultFile) throws IOException, ImageReadException {

    PdfReader reader = new PdfReader(resultFile);
    PRStream stream = (PRStream) reader.getPdfObject(ref);
    PdfDictionary dic = stream;
    byte[] content = PdfReader.getStreamBytesRaw(stream);

    int width = dic.getAsNumber(PdfName.WIDTH).intValue();
    int height = dic.getAsNumber(PdfName.HEIGHT).intValue();
    int pngBitDepth = dic.getAsNumber(PdfName.BITSPERCOMPONENT).intValue();

    PdfObject colorspace = dic.getDirectObject(PdfName.COLORSPACE);
    PdfArray decode = dic.getAsArray(PdfName.DECODE);
    PdfArray carray = (PdfArray) colorspace;
    PdfObject id2 = carray.getDirectObject(3);

    byte[] palette = null;
    if (id2 instanceof PdfString) {
        palette = ((PdfString) id2).getBytes();
    } else if (id2 instanceof PRStream) {
        palette = PdfReader.getStreamBytes(((PRStream) id2));
    }

    Map<PdfName, FilterHandlers.FilterHandler> handlers = new HashMap<PdfName, FilterHandlers.FilterHandler>(
            FilterHandlers.getDefaultFilterHandlers());
    byte[] imageBytes = PdfReader.decodeBytes(content, dic, handlers);

    int stride = (width * pngBitDepth + 7) / 8;
    ByteArrayOutputStream ms = new ByteArrayOutputStream();
    PngWriter png = new PngWriter(ms);

    if (decode != null) {
        if (pngBitDepth == 1) {
            // if the decode array is 1,0, then we need to invert the image
            if (decode.getAsNumber(0).intValue() == 1 && decode.getAsNumber(1).intValue() == 0) {
                int len = imageBytes.length;
                for (int t = 0; t < len; ++t) {
                    imageBytes[t] ^= 0xff;
                }
            } else {
                // if the decode array is 0,1, do nothing.  It's possible that the array could be 0,0 or 1,1 - but that would be silly, so we'll just ignore that case
            }
        } else {
            // todo: add decode transformation for other depths
        }
    }
    int pngColorType = 0;
    png.writeHeader(width, height, pngBitDepth, pngColorType);

    if (palette != null) {
        png.writePalette(palette);
    }
    png.writeData(imageBytes, stride);
    png.writeEnd();

    imageBytes = ms.toByteArray();

    InputStream in = new ByteArrayInputStream(imageBytes);
    ImageInputStream ima_stream = ImageIO.createImageInputStream(in);

    BufferedImage buffImg = null;
    BufferedImage buffPic = ImageIO.read(ima_stream);

    // check if image contains a mask image ... experimental for this type of image
    BufferedImage buffMask = null;
    PRStream maskStream = (PRStream) dic.getAsStream(PdfName.SMASK);
    if (maskStream != null) {
        PdfImageObject maskImage = new PdfImageObject(maskStream);
        buffMask = maskImage.getBufferedImage();

        Image img = PdfTrickUtils.TransformGrayToTransparency(buffMask);
        buffImg = PdfTrickUtils.ApplyTransparency(buffPic, img);
    } else {
        buffImg = buffPic;
    }

    reader.close();
    ms.close();
    in.close();
    return buffImg;
}