Search in sources :

Example 1 with PRIndirectReference

use of com.lowagie.text.pdf.PRIndirectReference in project OpenPDF by LibrePDF.

the class RemoveLaunchApplication method execute.

/**
 * @see com.lowagie.toolbox.AbstractTool#execute()
 */
public void execute() {
    try {
        if (getValue("srcfile") == null) {
            throw new InstantiationException("You need to choose a sourcefile");
        }
        File src = (File) getValue("srcfile");
        if (getValue("destfile") == null) {
            throw new InstantiationException("You need to choose a destination file");
        }
        File dest = (File) getValue("destfile");
        // we create a reader for a certain document
        PdfReader reader = new PdfReader(src.getAbsolutePath());
        PdfObject o;
        PdfDictionary d;
        PdfDictionary l;
        PdfName n;
        for (int i = 1; i < reader.getXrefSize(); i++) {
            o = reader.getPdfObject(i);
            if (o instanceof PdfDictionary) {
                d = (PdfDictionary) o;
                o = d.get(PdfName.A);
                if (o == null)
                    continue;
                if (o instanceof PdfDictionary) {
                    l = (PdfDictionary) o;
                } else {
                    PRIndirectReference r = (PRIndirectReference) o;
                    l = (PdfDictionary) reader.getPdfObject(r.getNumber());
                }
                n = (PdfName) l.get(PdfName.S);
                if (PdfName.LAUNCH.equals(n)) {
                    if (l.get(PdfName.F) != null) {
                        System.out.println("Removed: " + l.get(PdfName.F));
                        l.remove(PdfName.F);
                    }
                    if (l.get(PdfName.WIN) != null) {
                        System.out.println("Removed: " + l.get(PdfName.WIN));
                        l.remove(PdfName.WIN);
                    }
                    l.put(PdfName.S, PdfName.JAVASCRIPT);
                    l.put(PdfName.JS, new PdfString("app.alert('Launch Application Action removed by iText');\r"));
                }
            }
        }
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        stamper.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : PRIndirectReference(com.lowagie.text.pdf.PRIndirectReference) PdfName(com.lowagie.text.pdf.PdfName) PdfDictionary(com.lowagie.text.pdf.PdfDictionary) PdfStamper(com.lowagie.text.pdf.PdfStamper) FileOutputStream(java.io.FileOutputStream) PdfObject(com.lowagie.text.pdf.PdfObject) PdfReader(com.lowagie.text.pdf.PdfReader) File(java.io.File) PdfString(com.lowagie.text.pdf.PdfString)

Example 2 with PRIndirectReference

use of com.lowagie.text.pdf.PRIndirectReference in project OpenPDF by LibrePDF.

the class PdfTextExtractor method getContentBytesFromContentObject.

/**
 * Gets the content bytes from a content object, which may be a reference a
 * stream or an array.
 *
 * @param contentObject the object to read bytes from
 * @return the content bytes
 * @throws IOException
 */
private byte[] getContentBytesFromContentObject(PdfObject contentObject) throws IOException {
    final byte[] result;
    switch(contentObject.type()) {
        case PdfObject.INDIRECT:
            PRIndirectReference ref = (PRIndirectReference) contentObject;
            PdfObject directObject = PdfReader.getPdfObject(ref);
            result = getContentBytesFromContentObject(directObject);
            break;
        case PdfObject.STREAM:
            PRStream stream = (PRStream) PdfReader.getPdfObject(contentObject);
            result = PdfReader.getStreamBytes(stream);
            break;
        case PdfObject.ARRAY:
            // Stitch together all content before calling processContent(),
            // because
            // processContent() resets state.
            ByteArrayOutputStream allBytes = new ByteArrayOutputStream();
            PdfArray contentArray = (PdfArray) contentObject;
            for (PdfObject pdfObject : contentArray.getElements()) {
                allBytes.write(getContentBytesFromContentObject(pdfObject));
            }
            result = allBytes.toByteArray();
            break;
        default:
            throw new IllegalStateException("Unable to handle Content of type " + contentObject.getClass());
    }
    return result;
}
Also used : PRIndirectReference(com.lowagie.text.pdf.PRIndirectReference) PdfArray(com.lowagie.text.pdf.PdfArray) PRStream(com.lowagie.text.pdf.PRStream) PdfObject(com.lowagie.text.pdf.PdfObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with PRIndirectReference

use of com.lowagie.text.pdf.PRIndirectReference in project OpenPDF by LibrePDF.

the class Image method getInstance.

/**
 * Reuses an existing image.
 * @param ref the reference to the image dictionary
 * @throws BadElementException on error
 * @return the image
 */
public static Image getInstance(PRIndirectReference ref) throws BadElementException {
    PdfDictionary dic = (PdfDictionary) PdfReader.getPdfObjectRelease(ref);
    int width = ((PdfNumber) PdfReader.getPdfObjectRelease(dic.get(PdfName.WIDTH))).intValue();
    int height = ((PdfNumber) PdfReader.getPdfObjectRelease(dic.get(PdfName.HEIGHT))).intValue();
    Image imask = null;
    PdfObject obj = dic.get(PdfName.SMASK);
    if (obj != null && obj.isIndirect()) {
        imask = getInstance((PRIndirectReference) obj);
    } else {
        obj = dic.get(PdfName.MASK);
        if (obj != null && obj.isIndirect()) {
            PdfObject obj2 = PdfReader.getPdfObjectRelease(obj);
            if (obj2 instanceof PdfDictionary)
                imask = getInstance((PRIndirectReference) obj);
        }
    }
    Image img = new ImgRaw(width, height, 1, 1, null);
    img.imageMask = imask;
    img.directReference = ref;
    return img;
}
Also used : PRIndirectReference(com.lowagie.text.pdf.PRIndirectReference) PdfDictionary(com.lowagie.text.pdf.PdfDictionary) PdfObject(com.lowagie.text.pdf.PdfObject) PdfNumber(com.lowagie.text.pdf.PdfNumber) BufferedImage(java.awt.image.BufferedImage)

Example 4 with PRIndirectReference

use of com.lowagie.text.pdf.PRIndirectReference in project itext2 by albfernandez.

the class Image method getInstance.

/**
 * Reuses an existing image.
 * @param ref the reference to the image dictionary
 * @throws BadElementException on error
 * @return the image
 */
public static Image getInstance(PRIndirectReference ref) throws BadElementException {
    PdfDictionary dic = (PdfDictionary) PdfReader.getPdfObjectRelease(ref);
    int width = ((PdfNumber) PdfReader.getPdfObjectRelease(dic.get(PdfName.WIDTH))).intValue();
    int height = ((PdfNumber) PdfReader.getPdfObjectRelease(dic.get(PdfName.HEIGHT))).intValue();
    Image imask = null;
    PdfObject obj = dic.get(PdfName.SMASK);
    if (obj != null && obj.isIndirect()) {
        imask = getInstance((PRIndirectReference) obj);
    } else {
        obj = dic.get(PdfName.MASK);
        if (obj != null && obj.isIndirect()) {
            PdfObject obj2 = PdfReader.getPdfObjectRelease(obj);
            if (obj2 instanceof PdfDictionary)
                imask = getInstance((PRIndirectReference) obj);
        }
    }
    Image img = new ImgRaw(width, height, 1, 1, null);
    img.imageMask = imask;
    img.directReference = ref;
    return img;
}
Also used : PRIndirectReference(com.lowagie.text.pdf.PRIndirectReference) PdfDictionary(com.lowagie.text.pdf.PdfDictionary) PdfObject(com.lowagie.text.pdf.PdfObject) PdfNumber(com.lowagie.text.pdf.PdfNumber) TiffImage(com.lowagie.text.pdf.codec.TiffImage) JBIG2Image(com.lowagie.text.pdf.codec.JBIG2Image) GifImage(com.lowagie.text.pdf.codec.GifImage) BufferedImage(java.awt.image.BufferedImage) PngImage(com.lowagie.text.pdf.codec.PngImage) BmpImage(com.lowagie.text.pdf.codec.BmpImage)

Example 5 with PRIndirectReference

use of com.lowagie.text.pdf.PRIndirectReference in project itext2 by albfernandez.

the class PdfContentStreamProcessorTest method readContentBytes.

private byte[] readContentBytes(final PdfObject contentObject) throws IOException {
    final byte[] result;
    switch(contentObject.type()) {
        case PdfObject.INDIRECT:
            final PRIndirectReference ref = (PRIndirectReference) contentObject;
            final PdfObject directObject = PdfReader.getPdfObject(ref);
            result = readContentBytes(directObject);
            break;
        case PdfObject.STREAM:
            final PRStream stream = (PRStream) PdfReader.getPdfObject(contentObject);
            result = PdfReader.getStreamBytes(stream);
            break;
        case PdfObject.ARRAY:
            // Stitch together all content before calling processContent(),
            // because
            // processContent() resets state.
            final ByteArrayOutputStream allBytes = new ByteArrayOutputStream();
            final PdfArray contentArray = (PdfArray) contentObject;
            final ListIterator<?> iter = contentArray.listIterator();
            while (iter.hasNext()) {
                final PdfObject element = (PdfObject) iter.next();
                allBytes.write(readContentBytes(element));
            }
            result = allBytes.toByteArray();
            break;
        default:
            final String msg = "Unable to handle Content of type " + contentObject.getClass();
            throw new IllegalStateException(msg);
    }
    return result;
}
Also used : PRIndirectReference(com.lowagie.text.pdf.PRIndirectReference) PdfArray(com.lowagie.text.pdf.PdfArray) PRStream(com.lowagie.text.pdf.PRStream) PdfObject(com.lowagie.text.pdf.PdfObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

PRIndirectReference (com.lowagie.text.pdf.PRIndirectReference)6 PdfObject (com.lowagie.text.pdf.PdfObject)6 PdfDictionary (com.lowagie.text.pdf.PdfDictionary)4 PdfArray (com.lowagie.text.pdf.PdfArray)3 PRStream (com.lowagie.text.pdf.PRStream)2 PdfName (com.lowagie.text.pdf.PdfName)2 PdfNumber (com.lowagie.text.pdf.PdfNumber)2 BufferedImage (java.awt.image.BufferedImage)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PdfReader (com.lowagie.text.pdf.PdfReader)1 PdfStamper (com.lowagie.text.pdf.PdfStamper)1 PdfString (com.lowagie.text.pdf.PdfString)1 BmpImage (com.lowagie.text.pdf.codec.BmpImage)1 GifImage (com.lowagie.text.pdf.codec.GifImage)1 JBIG2Image (com.lowagie.text.pdf.codec.JBIG2Image)1 PngImage (com.lowagie.text.pdf.codec.PngImage)1 TiffImage (com.lowagie.text.pdf.codec.TiffImage)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1