List of usage examples for com.itextpdf.text.pdf PdfArray getAsNumber
public PdfNumber getAsNumber(final int idx)
PdfObject as a PdfNumber, resolving indirect references. From source file:de.gbv.marginalia.Annotation.java
License:Open Source License
/** * Serialize the annotation in XML format. * The annotation is emitted as stream of SAX events to a ContentHandler. * The XML is XFDF with additional Marginalia elements in its own namespace. *//*from ww w . j a v a 2 s . c o m*/ public void serializeXML(ContentHandler handler) throws SAXException { SimpleXMLCreator xml = new SimpleXMLCreator(handler, namespaces); Set<PdfName> allkeys = this.dict.getKeys(); allkeys.remove(PdfName.TYPE); allkeys.remove(PdfName.SUBTYPE); allkeys.remove(PdfName.PARENT); allkeys.remove(PdfName.CONTENTS); allkeys.remove(PdfName.POPUP); Map<String, String> attrs = new HashMap<String, String>(); for (String aName : this.FIELDS.keySet()) { Field f = this.FIELDS.get(aName); String value = f.getFrom(this.dict); if (value != null) { // TODO: encoding & exception attrs.put(aName, value); // allkeys.remove( f.name ); } } PdfDictionary pg = getAsDictionary(this.dict, PdfName.P); allkeys.remove(PdfName.P); //CropBox=[0, 0, 595, 842] //Rotate //MediaBox=[0, 0, 595, 842] // TODO: find out where page number is stored if (attrs.get("page") == null) attrs.put("page", "" + this.pageNum); String element = subtypes.get(this.subtype); if (element == null) { // TODO element = this.subtype.toString(); } xml.startElement(element, attrs); if (element.equals("ink")) { PdfArray inklist = this.dict.getAsArray(new PdfName("InkList")); if (inklist != null) { xml.startElement("inklist"); for (int i = 0; i < inklist.size(); i++) { PdfArray pathArray = inklist.getAsArray(i); String s = ""; for (int j = 0; j < pathArray.size(); j += 2) { if (j > 0) s += ";"; s += "" + pathArray.getAsNumber(j).floatValue() + ","; s += "" + pathArray.getAsNumber(j + 1).floatValue(); } xml.contentElement("gesture", s); } xml.endElement(); } } if (attrs.get("rect") != null) { Map<String, String> a = new HashMap<String, String>(); RectField rf = (RectField) this.FIELDS.get("rect"); PdfRectangle r = null; if (rf != null) r = (PdfRectangle) rf.getObjectFrom(this.dict); if (r != null) { a.put("left", "" + r.left()); a.put("bottom", "" + r.bottom()); a.put("right", "" + r.right()); a.put("top", "" + r.top()); xml.emptyElement("m", "rect", a); } } if (this.content != null && !this.content.equals("")) { // TODO: encode content if not UTF-8 ? xml.contentElement("content", content.toString()); } // TODO: contents-richtext // TODO: popup /* if ( this.popup != null ) { out.println("<!--popup>"); for ( PdfName n : this.popup.getKeys() ) { out.println( n + "=" + this.popup.getDirectObject(n) ); } out.println("</popup-->"); } */ // remaining dictionary elements /* for ( PdfName name : allkeys ) { Map<String,String> a = new HashMap<String,String>(); a.put("name",name.toString()); a.put("value",this.dict.getDirectObject(name).toString()); xml.emptyElement( "m","unknown", a ); } */ xml.endElement(); }
From source file:mkl.testarea.itext5.pdfcleanup.StrictPdfCleanUpProcessor.java
License:Open Source License
/** * Extracts locations from the concrete annotation. * Note: annotation can consist not only of one area specified by the RECT entry, but also of multiple areas specified * by the QuadPoints entry in the annotation dictionary. *//*from w ww . j a v a 2 s. c o m*/ private List<PdfCleanUpLocation> extractLocationsFromRedactAnnot(int page, int annotIndex, PdfDictionary annotDict) { List<PdfCleanUpLocation> locations = new ArrayList<PdfCleanUpLocation>(); List<Rectangle> markedRectangles = new ArrayList<Rectangle>(); PdfArray quadPoints = annotDict.getAsArray(PdfName.QUADPOINTS); if (quadPoints.size() != 0) { markedRectangles.addAll(translateQuadPointsToRectangles(quadPoints)); } else { PdfArray annotRect = annotDict.getAsArray(PdfName.RECT); markedRectangles .add(new Rectangle(annotRect.getAsNumber(0).floatValue(), annotRect.getAsNumber(1).floatValue(), annotRect.getAsNumber(2).floatValue(), annotRect.getAsNumber(3).floatValue())); } clippingRects.put(annotIndex, markedRectangles); BaseColor cleanUpColor = null; PdfArray ic = annotDict.getAsArray(PdfName.IC); if (ic != null) { cleanUpColor = new BaseColor(ic.getAsNumber(0).floatValue(), ic.getAsNumber(1).floatValue(), ic.getAsNumber(2).floatValue()); } PdfStream ro = annotDict.getAsStream(PdfName.RO); if (ro != null) { cleanUpColor = null; } for (Rectangle rect : markedRectangles) { locations.add(new PdfCleanUpLocation(page, rect, cleanUpColor)); } return locations; }
From source file:mkl.testarea.itext5.pdfcleanup.StrictPdfCleanUpProcessor.java
License:Open Source License
private List<Rectangle> translateQuadPointsToRectangles(PdfArray quadPoints) { List<Rectangle> rectangles = new ArrayList<Rectangle>(); for (int i = 0; i < quadPoints.size(); i += 8) { rectangles.add(new Rectangle(quadPoints.getAsNumber(i + 4).floatValue(), // QuadPoints have "Z" order quadPoints.getAsNumber(i + 5).floatValue(), quadPoints.getAsNumber(i + 2).floatValue(), quadPoints.getAsNumber(i + 3).floatValue())); }/*from www . jav a 2 s .c om*/ return rectangles; }
From source file:mkl.testarea.itext5.pdfcleanup.StrictPdfCleanUpProcessor.java
License:Open Source License
/** * Deletes redact annotations from the page and substitutes them with either OverlayText or RO object if it's needed. *///from w ww.j a v a 2s. co m private void deleteRedactAnnots(int pageNum) throws IOException, DocumentException { Set<String> indirRefs = redactAnnotIndirRefs.get(pageNum); if (indirRefs == null || indirRefs.isEmpty()) { return; } PdfReader reader = pdfStamper.getReader(); PdfContentByte canvas = pdfStamper.getOverContent(pageNum); PdfDictionary pageDict = reader.getPageN(pageNum); PdfArray annotsArray = pageDict.getAsArray(PdfName.ANNOTS); // j is for access annotRect (i can be decreased, so we need to store additional index, // indicating current position in ANNOTS array in case if we don't remove anything for (int i = 0, j = 0; i < annotsArray.size(); ++i, ++j) { PdfIndirectReference annotIndRef = annotsArray.getAsIndirectObject(i); PdfDictionary annotDict = annotsArray.getAsDict(i); if (indirRefs.contains(annotIndRef.toString()) || indirRefs.contains(getParentIndRefStr(annotDict))) { PdfStream formXObj = annotDict.getAsStream(PdfName.RO); PdfString overlayText = annotDict.getAsString(PdfName.OVERLAYTEXT); if (fillCleanedArea && formXObj != null) { PdfArray rectArray = annotDict.getAsArray(PdfName.RECT); Rectangle annotRect = new Rectangle(rectArray.getAsNumber(0).floatValue(), rectArray.getAsNumber(1).floatValue(), rectArray.getAsNumber(2).floatValue(), rectArray.getAsNumber(3).floatValue()); insertFormXObj(canvas, pageDict, formXObj, clippingRects.get(j), annotRect); } else if (fillCleanedArea && overlayText != null && overlayText.toUnicodeString().length() > 0) { drawOverlayText(canvas, clippingRects.get(j), overlayText, annotDict.getAsString(PdfName.DA), annotDict.getAsNumber(PdfName.Q), annotDict.getAsBoolean(PdfName.REPEAT)); } annotsArray.remove(i--); // array size is changed, so we need to decrease i } } if (annotsArray.size() == 0) { pageDict.remove(PdfName.ANNOTS); } }
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 .j a v a 2 s . co m*/ * @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; }