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();
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations