Java Code Examples for com.lowagie.text.Image#getDirectReference()

The following examples show how to use com.lowagie.text.Image#getDirectReference() . 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: PdfWriter.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Adds an image to the document but not to the page resources. It is used with templates and
 * <CODE>Document.add(Image)</CODE>. Use this method only if you know what you're doing!
 *
 * @param image the <CODE>Image</CODE> to add
 * @param fixedRef the reference to used. It may be <CODE>null</CODE>, a
 *            <CODE>PdfIndirectReference</CODE> or a <CODE>PRIndirectReference</CODE>.
 * @return the name of the image added
 * @throws PdfException on error
 * @throws DocumentException on error
 */
public PdfName addDirectImageSimple(Image image, PdfIndirectReference fixedRef) throws PdfException, DocumentException {
	PdfName name;
	// if the images is already added, just retrieve the name
	if (images.containsKey(image.getMySerialId())) {
		name = (PdfName) images.get(image.getMySerialId());
	}
	// if it's a new image, add it to the document
	else {
		if (image.isImgTemplate()) {
			name = new PdfName("img" + images.size());
			if (image instanceof ImgWMF) {
				try {
					ImgWMF wmf = (ImgWMF) image;
					wmf.readWMF(PdfTemplate.createTemplate(this, 0, 0));
				} catch (Exception e) {
					throw new DocumentException(e);
				}
			}
		} else {
			PdfIndirectReference dref = image.getDirectReference();
			if (dref != null) {
				PdfName rname = new PdfName("img" + images.size());
				images.put(image.getMySerialId(), rname);
				imageDictionary.put(rname, dref);
				return rname;
			}
			Image maskImage = image.getImageMask();
			PdfIndirectReference maskRef = null;
			if (maskImage != null) {
				PdfName mname = (PdfName) images.get(maskImage.getMySerialId());
				maskRef = getImageReference(mname);
			}
			PdfImage i = new PdfImage(image, "img" + images.size(), maskRef);
			if (image instanceof ImgJBIG2) {
				byte[] globals = ((ImgJBIG2) image).getGlobalBytes();
				if (globals != null) {
					PdfDictionary decodeparms = new PdfDictionary();
					decodeparms.put(PdfName.JBIG2GLOBALS, getReferenceJBIG2Globals(globals));
					i.put(PdfName.DECODEPARMS, decodeparms);
				}
			}
			if (image.hasICCProfile()) {
				PdfICCBased icc = new PdfICCBased(image.getICCProfile(), image.getCompressionLevel());
				PdfIndirectReference iccRef = add(icc);
				PdfArray iccArray = new PdfArray();
				iccArray.add(PdfName.ICCBASED);
				iccArray.add(iccRef);
				PdfArray colorspace = i.getAsArray(PdfName.COLORSPACE);
				if (colorspace != null) {
					if (colorspace.size() > 1 && PdfName.INDEXED.equals(colorspace.getPdfObject(0))) {
						colorspace.set(1, iccArray);
					} else {
						i.put(PdfName.COLORSPACE, iccArray);
					}
				} else {
					i.put(PdfName.COLORSPACE, iccArray);
				}
			}
			add(i, fixedRef);
			name = i.name();
		}
		images.put(image.getMySerialId(), name);
	}
	return name;
}
 
Example 2
Source File: PdfWriter.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Adds an image to the document but not to the page resources.
 * It is used with templates and <CODE>Document.add(Image)</CODE>.
 * Use this method only if you know what you're doing!
 * @param image the <CODE>Image</CODE> to add
 * @param fixedRef the reference to used. It may be <CODE>null</CODE>,
 * a <CODE>PdfIndirectReference</CODE> or a <CODE>PRIndirectReference</CODE>.
 * @return the name of the image added
 * @throws PdfException on error
 * @throws DocumentException on error
 */
public PdfName addDirectImageSimple(Image image, PdfIndirectReference fixedRef) throws PdfException, DocumentException {
    PdfName name;
    // if the images is already added, just retrieve the name
    if (images.containsKey(image.getMySerialId())) {
        name = (PdfName) images.get(image.getMySerialId());
    }
    // if it's a new image, add it to the document
    else {
        if (image.isImgTemplate()) {
            name = new PdfName("img" + images.size());
            if(image instanceof ImgWMF){
                try {
                    ImgWMF wmf = (ImgWMF)image;
                    wmf.readWMF(PdfTemplate.createTemplate(this, 0, 0));
                }
                catch (Exception e) {
                    throw new DocumentException(e);
                }
            }
        }
        else {
            PdfIndirectReference dref = image.getDirectReference();
            if (dref != null) {
                PdfName rname = new PdfName("img" + images.size());
                images.put(image.getMySerialId(), rname);
                imageDictionary.put(rname, dref);
                return rname;
            }
            Image maskImage = image.getImageMask();
            PdfIndirectReference maskRef = null;
            if (maskImage != null) {
                PdfName mname = (PdfName)images.get(maskImage.getMySerialId());
                maskRef = getImageReference(mname);
            }
            PdfImage i = new PdfImage(image, "img" + images.size(), maskRef);
            if (image instanceof ImgJBIG2) {
                byte[] globals = ((ImgJBIG2) image).getGlobalBytes();
                if (globals != null) {
                    PdfDictionary decodeparms = new PdfDictionary();
                    decodeparms.put(PdfName.JBIG2GLOBALS, getReferenceJBIG2Globals(globals));
                    i.put(PdfName.DECODEPARMS, decodeparms);
                }
            }
            if (image.hasICCProfile()) {
                PdfICCBased icc = new PdfICCBased(image.getICCProfile(), image.getCompressionLevel());
                PdfIndirectReference iccRef = add(icc);
                PdfArray iccArray = new PdfArray();
                iccArray.add(PdfName.ICCBASED);
                iccArray.add(iccRef);
                PdfArray colorspace = i.getAsArray(PdfName.COLORSPACE);
                if (colorspace != null) {
                    if (colorspace.size() > 1 && PdfName.INDEXED.equals(colorspace.getPdfObject(0)))
                        colorspace.set(1, iccArray);
                    else
                        i.put(PdfName.COLORSPACE, iccArray);
                }
                else
                    i.put(PdfName.COLORSPACE, iccArray);
            }
            add(i, fixedRef);
            name = i.name();
        }
        images.put(image.getMySerialId(), name);
    }
    return name;
}