use of com.lowagie.text.ImgJBIG2 in project OpenPDF by LibrePDF.
the class PdfContentByte method addImage.
/**
* Adds an <CODE>Image</CODE> to the page. The positioning of the <CODE>Image</CODE>
* is done with the transformation matrix. To position an <CODE>image</CODE> at (x,y)
* use addImage(image, image_width, 0, 0, image_height, x, y). The image can be placed inline.
* @param image the <CODE>Image</CODE> object
* @param a an element of the transformation matrix
* @param b an element of the transformation matrix
* @param c an element of the transformation matrix
* @param d an element of the transformation matrix
* @param e an element of the transformation matrix
* @param f an element of the transformation matrix
* @param inlineImage <CODE>true</CODE> to place this image inline, <CODE>false</CODE> otherwise
* @throws DocumentException on error
*/
public void addImage(Image image, float a, float b, float c, float d, float e, float f, boolean inlineImage) throws DocumentException {
try {
if (image.getLayer() != null)
beginLayer(image.getLayer());
if (image.isImgTemplate()) {
writer.addDirectImageSimple(image);
PdfTemplate template = image.getTemplateData();
float w = template.getWidth();
float h = template.getHeight();
addTemplate(template, a / w, b / w, c / h, d / h, e, f);
} else {
content.append("q ");
content.append(a).append(' ');
content.append(b).append(' ');
content.append(c).append(' ');
content.append(d).append(' ');
content.append(e).append(' ');
content.append(f).append(" cm");
if (inlineImage) {
content.append("\nBI\n");
PdfImage pimage = new PdfImage(image, "", null);
if (image instanceof ImgJBIG2) {
byte[] globals = ((ImgJBIG2) image).getGlobalBytes();
if (globals != null) {
PdfDictionary decodeparms = new PdfDictionary();
decodeparms.put(PdfName.JBIG2GLOBALS, writer.getReferenceJBIG2Globals(globals));
pimage.put(PdfName.DECODEPARMS, decodeparms);
}
}
for (PdfName key : pimage.getKeys()) {
PdfObject value = pimage.get(key);
String s = abrev.get(key);
if (s == null)
continue;
content.append(s);
boolean check = true;
if (key.equals(PdfName.COLORSPACE) && value.isArray()) {
PdfArray ar = (PdfArray) value;
if (ar.size() == 4 && PdfName.INDEXED.equals(ar.getAsName(0)) && ar.getPdfObject(1).isName() && ar.getPdfObject(2).isNumber() && ar.getPdfObject(3).isString()) {
check = false;
}
}
if (check && key.equals(PdfName.COLORSPACE) && !value.isName()) {
PdfName cs = writer.getColorspaceName();
PageResources prs = getPageResources();
prs.addColor(cs, writer.addToBody(value).getIndirectReference());
value = cs;
}
value.toPdf(null, content);
content.append('\n');
}
content.append("ID\n");
pimage.writeContent(content);
content.append("\nEI\nQ").append_i(separator);
} else {
PdfName name;
PageResources prs = getPageResources();
Image maskImage = image.getImageMask();
if (maskImage != null) {
name = writer.addDirectImageSimple(maskImage);
prs.addXObject(name, writer.getImageReference(name));
}
name = writer.addDirectImageSimple(image);
name = prs.addXObject(name, writer.getImageReference(name));
content.append(' ').append(name.getBytes()).append(" Do Q").append_i(separator);
}
}
if (image.hasBorders()) {
saveState();
float w = image.getWidth();
float h = image.getHeight();
concatCTM(a / w, b / w, c / h, d / h, e, f);
rectangle(image);
restoreState();
}
if (image.getLayer() != null)
endLayer();
Annotation annot = image.getAnnotation();
if (annot == null)
return;
float[] r = new float[unitRect.length];
for (int k = 0; k < unitRect.length; k += 2) {
r[k] = a * unitRect[k] + c * unitRect[k + 1] + e;
r[k + 1] = b * unitRect[k] + d * unitRect[k + 1] + f;
}
float llx = r[0];
float lly = r[1];
float urx = llx;
float ury = lly;
for (int k = 2; k < r.length; k += 2) {
llx = Math.min(llx, r[k]);
lly = Math.min(lly, r[k + 1]);
urx = Math.max(urx, r[k]);
ury = Math.max(ury, r[k + 1]);
}
annot = new Annotation(annot);
annot.setDimensions(llx, lly, urx, ury);
PdfAnnotation an = PdfAnnotationsImp.convertAnnotation(writer, annot, new Rectangle(llx, lly, urx, ury));
if (an == null)
return;
addAnnotation(an);
} catch (Exception ee) {
throw new DocumentException(ee);
}
}
use of com.lowagie.text.ImgJBIG2 in project OpenPDF by LibrePDF.
the class PdfWriter method addDirectImageSimple.
/**
* 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 DocumentException {
PdfName name;
// if the images is already added, just retrieve the name
if (images.containsKey(image.getMySerialId())) {
name = images.get(image.getMySerialId());
} else // if it's a new image, add it to the document
{
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 = 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;
}
use of com.lowagie.text.ImgJBIG2 in project itext2 by albfernandez.
the class PdfWriter method addDirectImageSimple.
/**
* 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());
} else // if it's a new image, add it to the document
{
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;
}
use of com.lowagie.text.ImgJBIG2 in project itext2 by albfernandez.
the class PdfContentByte method addImage.
/**
* Adds an <CODE>Image</CODE> to the page. The positioning of the <CODE>Image</CODE>
* is done with the transformation matrix. To position an <CODE>image</CODE> at (x,y)
* use addImage(image, image_width, 0, 0, image_height, x, y). The image can be placed inline.
* @param image the <CODE>Image</CODE> object
* @param a an element of the transformation matrix
* @param b an element of the transformation matrix
* @param c an element of the transformation matrix
* @param d an element of the transformation matrix
* @param e an element of the transformation matrix
* @param f an element of the transformation matrix
* @param inlineImage <CODE>true</CODE> to place this image inline, <CODE>false</CODE> otherwise
* @throws DocumentException on error
*/
public void addImage(Image image, float a, float b, float c, float d, float e, float f, boolean inlineImage) throws DocumentException {
try {
if (image.getLayer() != null)
beginLayer(image.getLayer());
if (image.isImgTemplate()) {
writer.addDirectImageSimple(image);
PdfTemplate template = image.getTemplateData();
float w = template.getWidth();
float h = template.getHeight();
addTemplate(template, a / w, b / w, c / h, d / h, e, f);
} else {
content.append("q ");
content.append(a).append(' ');
content.append(b).append(' ');
content.append(c).append(' ');
content.append(d).append(' ');
content.append(e).append(' ');
content.append(f).append(" cm");
if (inlineImage) {
content.append("\nBI\n");
PdfImage pimage = new PdfImage(image, "", null);
if (image instanceof ImgJBIG2) {
byte[] globals = ((ImgJBIG2) image).getGlobalBytes();
if (globals != null) {
PdfDictionary decodeparms = new PdfDictionary();
decodeparms.put(PdfName.JBIG2GLOBALS, writer.getReferenceJBIG2Globals(globals));
pimage.put(PdfName.DECODEPARMS, decodeparms);
}
}
for (Iterator it = pimage.getKeys().iterator(); it.hasNext(); ) {
PdfName key = (PdfName) it.next();
PdfObject value = pimage.get(key);
String s = (String) abrev.get(key);
if (s == null)
continue;
content.append(s);
boolean check = true;
if (key.equals(PdfName.COLORSPACE) && value.isArray()) {
PdfArray ar = (PdfArray) value;
if (ar.size() == 4 && PdfName.INDEXED.equals(ar.getAsName(0)) && ar.getPdfObject(1).isName() && ar.getPdfObject(2).isNumber() && ar.getPdfObject(3).isString()) {
check = false;
}
}
if (check && key.equals(PdfName.COLORSPACE) && !value.isName()) {
PdfName cs = writer.getColorspaceName();
PageResources prs = getPageResources();
prs.addColor(cs, writer.addToBody(value).getIndirectReference());
value = cs;
}
value.toPdf(null, content);
content.append('\n');
}
content.append("ID\n");
pimage.writeContent(content);
content.append("\nEI\nQ").append_i(separator);
} else {
PdfName name;
PageResources prs = getPageResources();
Image maskImage = image.getImageMask();
if (maskImage != null) {
name = writer.addDirectImageSimple(maskImage);
prs.addXObject(name, writer.getImageReference(name));
}
name = writer.addDirectImageSimple(image);
name = prs.addXObject(name, writer.getImageReference(name));
content.append(' ').append(name.getBytes()).append(" Do Q").append_i(separator);
}
}
if (image.hasBorders()) {
saveState();
float w = image.getWidth();
float h = image.getHeight();
concatCTM(a / w, b / w, c / h, d / h, e, f);
rectangle(image);
restoreState();
}
if (image.getLayer() != null)
endLayer();
Annotation annot = image.getAnnotation();
if (annot == null)
return;
float[] r = new float[unitRect.length];
for (int k = 0; k < unitRect.length; k += 2) {
r[k] = a * unitRect[k] + c * unitRect[k + 1] + e;
r[k + 1] = b * unitRect[k] + d * unitRect[k + 1] + f;
}
float llx = r[0];
float lly = r[1];
float urx = llx;
float ury = lly;
for (int k = 2; k < r.length; k += 2) {
llx = Math.min(llx, r[k]);
lly = Math.min(lly, r[k + 1]);
urx = Math.max(urx, r[k]);
ury = Math.max(ury, r[k + 1]);
}
annot = new Annotation(annot);
annot.setDimensions(llx, lly, urx, ury);
PdfAnnotation an = PdfAnnotationsImp.convertAnnotation(writer, annot, new Rectangle(llx, lly, urx, ury));
if (an == null)
return;
addAnnotation(an);
} catch (Exception ee) {
throw new DocumentException(ee);
}
}
use of com.lowagie.text.ImgJBIG2 in project itext2 by albfernandez.
the class JBIG2Image method getJbig2Image.
/**
* returns an Image representing the given page.
* @param ra the file or array containing the image
* @param page the page number of the image
* @return an Image object
*/
public static Image getJbig2Image(RandomAccessFileOrArray ra, int page) {
if (page < 1)
throw new IllegalArgumentException("The page number must be >= 1.");
try {
JBIG2SegmentReader sr = new JBIG2SegmentReader(ra);
sr.read();
JBIG2SegmentReader.JBIG2Page p = sr.getPage(page);
Image img = new ImgJBIG2(p.pageBitmapWidth, p.pageBitmapHeight, p.getData(true), sr.getGlobal(true));
return img;
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
Aggregations