com.lowagie.text.pdf.PdfNumber Java Examples

The following examples show how to use com.lowagie.text.pdf.PdfNumber. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Image.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #2
Source File: PdfContentStreamProcessor.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	PdfNumber aw = (PdfNumber) operands.get(0);
	PdfNumber ac = (PdfNumber) operands.get(1);
	PdfString string = (PdfString) operands.get(2);

	ArrayList twOperands = new ArrayList(1);
	twOperands.add(0, aw);
	processor.invokeOperator(new PdfLiteral("Tw"), twOperands);

	ArrayList tcOperands = new ArrayList(1);
	tcOperands.add(0, ac);
	processor.invokeOperator(new PdfLiteral("Tc"), tcOperands);

	ArrayList tickOperands = new ArrayList(1);
	tickOperands.add(0, string);
	processor.invokeOperator(new PdfLiteral("'"), tickOperands);
}
 
Example #3
Source File: Image.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #4
Source File: PdfContentStreamProcessor.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	PdfNumber aw = (PdfNumber) operands.get(0);
	PdfNumber ac = (PdfNumber) operands.get(1);
	PdfString string = (PdfString) operands.get(2);

	ArrayList twOperands = new ArrayList(1);
	twOperands.add(0, aw);
	processor.invokeOperator(new PdfLiteral("Tw"), twOperands);

	ArrayList tcOperands = new ArrayList(1);
	tcOperands.add(0, ac);
	processor.invokeOperator(new PdfLiteral("Tc"), tcOperands);

	ArrayList tickOperands = new ArrayList(1);
	tickOperands.add(0, string);
	processor.invokeOperator(new PdfLiteral("'"), tickOperands);
}
 
Example #5
Source File: PdfCollectionField.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Returns a PdfObject that can be used as the value of a Collection Item.
 * @param v	value	the value that has to be changed into a PdfObject (PdfString, PdfDate or PdfNumber)	
 */
public PdfObject getValue(String v) {
	switch(fieldType) {
	case TEXT:
		return new PdfString(v, PdfObject.TEXT_UNICODE);
	case DATE:
		return new PdfDate(PdfDate.decode(v));
	case NUMBER:
		return new PdfNumber(v);
	}
	throw new IllegalArgumentException(v + " is not an acceptable value for the field " + get(PdfName.N).toString());
}
 
Example #6
Source File: PdfContentStreamProcessor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	PdfArray array = (PdfArray) operands.get(0);
	float tj = 0;
	for (Iterator i = array.listIterator(); i.hasNext();) {
		Object entryObj = i.next();
		if (entryObj instanceof PdfString) {
			processor.displayPdfString((PdfString) entryObj, tj);
			tj = 0;
		} else {
			tj = ((PdfNumber) entryObj).floatValue();
		}
	}

}
 
Example #7
Source File: PdfCollectionItem.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Sets the value of the collection item.
 * @param n
 */
public void addItem(String key, PdfNumber n) {
	PdfName fieldname = new PdfName(key);
	PdfCollectionField field = (PdfCollectionField)schema.get(fieldname);
	if (field.fieldType == PdfCollectionField.NUMBER) {
		put(fieldname, n);
	}
}
 
Example #8
Source File: PdfContentStreamProcessor.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	PdfArray array = (PdfArray) operands.get(0);
	float tj = 0;
	for (Iterator i = array.listIterator(); i.hasNext();) {
		Object entryObj = i.next();
		if (entryObj instanceof PdfString) {
			processor.displayPdfString((PdfString) entryObj, tj);
			tj = 0;
		} else {
			tj = ((PdfNumber) entryObj).floatValue();
		}
	}

}
 
Example #9
Source File: PdfContentStreamProcessor.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	float a = ((PdfNumber) operands.get(0)).floatValue();
	float b = ((PdfNumber) operands.get(1)).floatValue();
	float c = ((PdfNumber) operands.get(2)).floatValue();
	float d = ((PdfNumber) operands.get(3)).floatValue();
	float e = ((PdfNumber) operands.get(4)).floatValue();
	float f = ((PdfNumber) operands.get(5)).floatValue();

	processor.textLineMatrix = new Matrix(a, b, c, d, e, f);
	processor.textMatrix = processor.textLineMatrix;
}
 
Example #10
Source File: PdfContentStreamProcessor.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	float ty = ((PdfNumber) operands.get(1)).floatValue();

	ArrayList tlOperands = new ArrayList(1);
	tlOperands.add(0, new PdfNumber(-ty));
	processor.invokeOperator(new PdfLiteral("TL"), tlOperands);
	processor.invokeOperator(new PdfLiteral("Td"), operands);
}
 
Example #11
Source File: PdfContentStreamProcessor.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	float tx = ((PdfNumber) operands.get(0)).floatValue();
	float ty = ((PdfNumber) operands.get(1)).floatValue();

	Matrix translationMatrix = new Matrix(tx, ty);
	processor.textMatrix = translationMatrix.multiply(processor.textLineMatrix);
	processor.textLineMatrix = processor.textMatrix;
}
 
Example #12
Source File: PdfContentStreamProcessor.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	PdfName fontResourceName = (PdfName) operands.get(0);
	float size = ((PdfNumber) operands.get(1)).floatValue();

	PdfDictionary fontsDictionary = processor.resources.getAsDict(PdfName.FONT);
	CMapAwareDocumentFont font = new CMapAwareDocumentFont(
			(PRIndirectReference) fontsDictionary.get(fontResourceName));

	processor.gs().font = font;
	processor.gs().fontSize = size;

}
 
Example #13
Source File: PdfContentStreamProcessor.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	float a = ((PdfNumber) operands.get(0)).floatValue();
	float b = ((PdfNumber) operands.get(1)).floatValue();
	float c = ((PdfNumber) operands.get(2)).floatValue();
	float d = ((PdfNumber) operands.get(3)).floatValue();
	float e = ((PdfNumber) operands.get(4)).floatValue();
	float f = ((PdfNumber) operands.get(5)).floatValue();
	Matrix matrix = new Matrix(a, b, c, d, e, f);
	GraphicsState gs = (GraphicsState) processor.gsStack.peek();
	gs.ctm = gs.ctm.multiply(matrix);
}
 
Example #14
Source File: BmpImage.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Image indexedModel(byte bdata[], int bpc, int paletteEntries) throws BadElementException {
    Image img = new ImgRaw(width, height, 1, bpc, bdata);
    PdfArray colorspace = new PdfArray();
    colorspace.add(PdfName.INDEXED);
    colorspace.add(PdfName.DEVICERGB);
    byte np[] = getPalette(paletteEntries);
    int len = np.length;
    colorspace.add(new PdfNumber(len / 3 - 1));
    colorspace.add(new PdfString(np));
    PdfDictionary ad = new PdfDictionary();
    ad.put(PdfName.COLORSPACE, colorspace);
    img.setAdditional(ad);
    return img;
}
 
Example #15
Source File: PdfCollectionField.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns a PdfObject that can be used as the value of a Collection Item.
 * @param v	value	the value that has to be changed into a PdfObject (PdfString, PdfDate or PdfNumber)	
 */
public PdfObject getValue(String v) {
	switch(fieldType) {
	case TEXT:
		return new PdfString(v, PdfObject.TEXT_UNICODE);
	case DATE:
		return new PdfDate(PdfDate.decode(v));
	case NUMBER:
		return new PdfNumber(v);
	}
	throw new IllegalArgumentException(v + " is not an acceptable value for the field " + get(PdfName.N).toString());
}
 
Example #16
Source File: PdfCollectionItem.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Sets the value of the collection item.
 * @param n
 */
public void addItem(String key, PdfNumber n) {
	PdfName fieldname = new PdfName(key);
	PdfCollectionField field = (PdfCollectionField)schema.get(fieldname);
	if (field.fieldType == PdfCollectionField.NUMBER) {
		put(fieldname, n);
	}
}
 
Example #17
Source File: ITextPdfArray.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public int getInt(int i) throws IOException {
	PdfNumber number = wrapped.getAsNumber(i);
	if (number != null) {
		return number.intValue();
	}
	return 0;
}
 
Example #18
Source File: PaperightPdfConverter.java    From website with GNU Affero General Public License v3.0 5 votes vote down vote up
public String cropPdf(String pdfFilePath) throws DocumentException, IOException, Exception {
	String filename = FilenameUtils.getBaseName(pdfFilePath) + "_cropped." + FilenameUtils.getExtension(pdfFilePath);
	filename = FilenameUtils.concat(System.getProperty("java.io.tmpdir"), filename);
	PdfReader reader = new PdfReader(pdfFilePath);
	try {
		PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
		try {
			for (int i = 1; i <= reader.getNumberOfPages(); i++) {
				PdfDictionary pdfDictionary = reader.getPageN(i);
				PdfArray cropArray = new PdfArray();
				Rectangle box = getSmallestBox(reader, i);
				//Rectangle cropbox = reader.getCropBox(i);
				if (box != null) {
					cropArray.add(new PdfNumber(box.getLeft()));
					cropArray.add(new PdfNumber(box.getBottom()));
					cropArray.add(new PdfNumber(box.getLeft() + box.getWidth()));
					cropArray.add(new PdfNumber(box.getBottom() + box.getHeight()));
					pdfDictionary.put(PdfName.CROPBOX, cropArray);
					pdfDictionary.put(PdfName.MEDIABOX, cropArray);
					pdfDictionary.put(PdfName.TRIMBOX, cropArray);
					pdfDictionary.put(PdfName.BLEEDBOX, cropArray);
				}
			}
			return filename;
		} finally {
			stamper.close();
		}
	} catch (Exception e) {
		logger.error(e.getMessage(), e);
		throw e;
	} finally {
		reader.close();
	}
}
 
Example #19
Source File: BmpImage.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private Image indexedModel(byte bdata[], int bpc, int paletteEntries) throws BadElementException {
	Image img = new ImgRaw(width, height, 1, bpc, bdata);
	PdfArray colorspace = new PdfArray();
	colorspace.add(PdfName.INDEXED);
	colorspace.add(PdfName.DEVICERGB);
	byte np[] = getPalette(paletteEntries);
	int len = np.length;
	colorspace.add(new PdfNumber(len / 3 - 1));
	colorspace.add(new PdfString(np));
	PdfDictionary ad = new PdfDictionary();
	ad.put(PdfName.COLORSPACE, colorspace);
	img.setAdditional(ad);
	return img;
}
 
Example #20
Source File: PdfContentStreamProcessor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	ArrayList tdoperands = new ArrayList(2);
	tdoperands.add(0, new PdfNumber(0));
	tdoperands.add(1, new PdfNumber(processor.gs().leading));
	processor.invokeOperator(new PdfLiteral("Td"), tdoperands);
}
 
Example #21
Source File: PdfContentStreamProcessor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	float a = ((PdfNumber) operands.get(0)).floatValue();
	float b = ((PdfNumber) operands.get(1)).floatValue();
	float c = ((PdfNumber) operands.get(2)).floatValue();
	float d = ((PdfNumber) operands.get(3)).floatValue();
	float e = ((PdfNumber) operands.get(4)).floatValue();
	float f = ((PdfNumber) operands.get(5)).floatValue();

	processor.textLineMatrix = new Matrix(a, b, c, d, e, f);
	processor.textMatrix = processor.textLineMatrix;
}
 
Example #22
Source File: PdfContentStreamProcessor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	float ty = ((PdfNumber) operands.get(1)).floatValue();

	ArrayList tlOperands = new ArrayList(1);
	tlOperands.add(0, new PdfNumber(-ty));
	processor.invokeOperator(new PdfLiteral("TL"), tlOperands);
	processor.invokeOperator(new PdfLiteral("Td"), operands);
}
 
Example #23
Source File: PdfContentStreamProcessor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	float tx = ((PdfNumber) operands.get(0)).floatValue();
	float ty = ((PdfNumber) operands.get(1)).floatValue();

	Matrix translationMatrix = new Matrix(tx, ty);
	processor.textMatrix = translationMatrix.multiply(processor.textLineMatrix);
	processor.textLineMatrix = processor.textMatrix;
}
 
Example #24
Source File: PdfContentStreamProcessor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	PdfName fontResourceName = (PdfName) operands.get(0);
	float size = ((PdfNumber) operands.get(1)).floatValue();

	PdfDictionary fontsDictionary = processor.resources.getAsDict(PdfName.FONT);
	CMapAwareDocumentFont font = new CMapAwareDocumentFont((PRIndirectReference) fontsDictionary.get(fontResourceName));

	processor.gs().font = font;
	processor.gs().fontSize = size;

}
 
Example #25
Source File: PdfContentStreamProcessor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	float a = ((PdfNumber) operands.get(0)).floatValue();
	float b = ((PdfNumber) operands.get(1)).floatValue();
	float c = ((PdfNumber) operands.get(2)).floatValue();
	float d = ((PdfNumber) operands.get(3)).floatValue();
	float e = ((PdfNumber) operands.get(4)).floatValue();
	float f = ((PdfNumber) operands.get(5)).floatValue();
	Matrix matrix = new Matrix(a, b, c, d, e, f);
	GraphicsState gs = (GraphicsState) processor.gsStack.peek();
	gs.ctm = gs.ctm.multiply(matrix);
}
 
Example #26
Source File: PdfContentStreamProcessor.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	PdfNumber leading = (PdfNumber) operands.get(0);
	processor.gs().leading = leading.floatValue();
}
 
Example #27
Source File: PdfContentStreamProcessor.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	PdfNumber scale = (PdfNumber) operands.get(0);
	processor.gs().horizontalScaling = scale.floatValue();
}
 
Example #28
Source File: PngImage.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
PdfObject getColorspace() {
    if (icc_profile != null) {
        if ((colorType & 2) == 0)
            return PdfName.DEVICEGRAY;
        else
            return PdfName.DEVICERGB;
    }
    if (gamma == 1f && !hasCHRM) {
        if ((colorType & 2) == 0)
            return PdfName.DEVICEGRAY;
        else
            return PdfName.DEVICERGB;
    }
    else {
        PdfArray array = new PdfArray();
        PdfDictionary dic = new PdfDictionary();
        if ((colorType & 2) == 0) {
            if (gamma == 1f)
                return PdfName.DEVICEGRAY;
            array.add(PdfName.CALGRAY);
            dic.put(PdfName.GAMMA, new PdfNumber(gamma));
            dic.put(PdfName.WHITEPOINT, new PdfLiteral("[1 1 1]"));
            array.add(dic);
        }
        else {
            PdfObject wp = new PdfLiteral("[1 1 1]");
            array.add(PdfName.CALRGB);
            if (gamma != 1f) {
                PdfArray gm = new PdfArray();
                PdfNumber n = new PdfNumber(gamma);
                gm.add(n);
                gm.add(n);
                gm.add(n);
                dic.put(PdfName.GAMMA, gm);
            }
            if (hasCHRM) {
                float z = yW*((xG-xB)*yR-(xR-xB)*yG+(xR-xG)*yB);
                float YA = yR*((xG-xB)*yW-(xW-xB)*yG+(xW-xG)*yB)/z;
                float XA = YA*xR/yR;
                float ZA = YA*((1-xR)/yR-1);
                float YB = -yG*((xR-xB)*yW-(xW-xB)*yR+(xW-xR)*yB)/z;
                float XB = YB*xG/yG;
                float ZB = YB*((1-xG)/yG-1);
                float YC = yB*((xR-xG)*yW-(xW-xG)*yW+(xW-xR)*yG)/z;
                float XC = YC*xB/yB;
                float ZC = YC*((1-xB)/yB-1);
                float XW = XA+XB+XC;
                float YW = 1;//YA+YB+YC;
                float ZW = ZA+ZB+ZC;
                PdfArray wpa = new PdfArray();
                wpa.add(new PdfNumber(XW));
                wpa.add(new PdfNumber(YW));
                wpa.add(new PdfNumber(ZW));
                wp = wpa;
                PdfArray matrix = new PdfArray();
                matrix.add(new PdfNumber(XA));
                matrix.add(new PdfNumber(YA));
                matrix.add(new PdfNumber(ZA));
                matrix.add(new PdfNumber(XB));
                matrix.add(new PdfNumber(YB));
                matrix.add(new PdfNumber(ZB));
                matrix.add(new PdfNumber(XC));
                matrix.add(new PdfNumber(YC));
                matrix.add(new PdfNumber(ZC));
                dic.put(PdfName.MATRIX, matrix);
            }
            dic.put(PdfName.WHITEPOINT, wp);
            array.add(dic);
        }
        return array;
    }
}
 
Example #29
Source File: PdfContentStreamProcessor.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	PdfNumber charSpace = (PdfNumber) operands.get(0);
	processor.gs().characterSpacing = charSpace.floatValue();
}
 
Example #30
Source File: PdfContentStreamProcessor.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList operands) {
	PdfNumber wordSpace = (PdfNumber) operands.get(0);
	processor.gs().wordSpacing = wordSpace.floatValue();
}