com.itextpdf.text.Image类的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(12.1k)|赞(0)|评价(0)|浏览(889)

本文整理了Java中com.itextpdf.text.Image类的一些代码示例,展示了Image类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Image类的具体详情如下:
包路径:com.itextpdf.text.Image
类名称:Image

Image介绍

[英]An Image is the representation of a graphic element (JPEG, PNG or GIF) that has to be inserted into the document
[中]Image是必须插入文档的图形元素(JPEG、PNG或GIF)的表示形式

代码示例

代码示例来源:origin: fr.opensagres.xdocreport/fr.opensagres.xdocreport.itext5.extension

public ExtendedImage( Image image, float offsetY )
{
  super( image );
  this.image = Image.getInstance( image );
  this.offsetY = offsetY;
}

代码示例来源:origin: youseries/ureport

private com.itextpdf.text.Image buildPdfImage(String base64Data, int width,int height) throws Exception{
    com.itextpdf.text.Image pdfImg=null;
    InputStream input=ImageUtils.base64DataToInputStream(base64Data);
    try{
      byte[] bytes=IOUtils.toByteArray(input);
      pdfImg=com.itextpdf.text.Image.getInstance(bytes);
      float imgWidth=pdfImg.getWidth();
      float imgHeight=pdfImg.getHeight();
      if(width==0){
        width=Float.valueOf(imgWidth).intValue();
      }
      if(height==0){
        height=Float.valueOf(imgHeight).intValue();
      }
      width=UnitUtils.pixelToPoint(width-2);
      height=UnitUtils.pixelToPoint(height-2);
      pdfImg.scaleToFit(width,height);
    }finally{
      IOUtils.closeQuietly(input);
    }
    return pdfImg;
  }
}

代码示例来源:origin: org.xhtmlrenderer/flying-saucer-pdf-itext5

private void scaleToOutputResolution(Image image) {
  float factor = _sharedContext.getDotsPerPixel();
  if (factor != 1.0f) {
    image.scaleAbsolute(image.getPlainWidth() * factor, image.getPlainHeight() * factor);
  }
}

代码示例来源:origin: com.itextpdf/itextpdf

/**
 * Scales the image so that it fits a certain width and height.
 *
 * @param fitWidth
 *            the width to fit
 * @param fitHeight
 *            the height to fit
 */
public void scaleToFit(final float fitWidth, final float fitHeight) {
  scalePercent(100);
  float percentX = fitWidth * 100 / getScaledWidth();
  float percentY = fitHeight * 100 / getScaledHeight();
  scalePercent(percentX < percentY ? percentX : percentY);
  setWidthPercentage(0);
}

代码示例来源:origin: com.itextpdf/itextpdf

/**
 * Scale the width and height of an image to a certain percentage.
 *
 * @param percentX
 *            the scaling percentage of the width
 * @param percentY
 *            the scaling percentage of the height
 */
public void scalePercent(final float percentX, final float percentY) {
  plainWidth = getWidth() * percentX / 100f;
  plainHeight = getHeight() * percentY / 100f;
  float[] matrix = matrix();
  scaledWidth = matrix[DX] - matrix[CX];
  scaledHeight = matrix[DY] - matrix[CY];
  setWidthPercentage(0);
}

代码示例来源:origin: qaprosoft/carina

Image image = Image.getInstance(testRootDir.getAbsolutePath() + "/" + Screenshot.captureFailure(driver, ""));
Document document = null;
if (scaled) {
  document = new Document(PageSize.A4, 10, 10, 10, 10);
  if (image.getHeight() > (document.getPageSize().getHeight() - 20)
      || image.getScaledWidth() > (document.getPageSize().getWidth() - 20)) {
    image.scaleToFit(document.getPageSize().getWidth() - 20, document.getPageSize().getHeight() - 20);
  document = new Document(new RectangleReadOnly(image.getScaledWidth(), image.getScaledHeight()));

代码示例来源:origin: com.itextpdf/itextpdf

img = Image.getInstance(img);
  img.setRotation(img.getImageRotation() + (float) (cell.getRotation() * Math.PI / 180.0));
  if (!img.isScaleToFitHeight()) {
    continue;
  img.scalePercent(100);
  float scale = (currentMaxHeight - cell.getEffectivePaddingTop() - cell
      .getEffectivePaddingBottom())
      / img.getScaledHeight();
  img.scalePercent(scale * 100);
  vf = true;
          + cell.getRight()
          - cell.getEffectivePaddingRight() - img
          .getScaledWidth()) / 2;
      break;
    case Element.ALIGN_RIGHT:
      left = xPos + cell.getRight()
          - cell.getEffectivePaddingRight()
          - img.getScaledWidth();
      break;
    default:
img.setAbsolutePosition(left, tly - img.getScaledHeight());
try {
  if (isTagged(canvases[PdfPTable.TEXTCANVAS])) {

代码示例来源:origin: Swati4star/Images-to-PDF

quality = Integer.parseInt(mQualityString);
Image image = Image.getInstance(mImagesUri.get(i));
image.setCompressionLevel((int) qualtyMod);
image.setBorder(Rectangle.BOX);
image.setBorderWidth(mBorderWidth);
float pageHeight = document.getPageSize().getHeight() - (mMarginBottom + mMarginTop);
if (mImagescaleType.equals(IMAGE_SCALE_TYPE_ASPECT_RATIO))
  image.scaleToFit(pageWidth, pageHeight);
else
  image.scaleAbsolute(pageWidth, pageHeight);
image.setAbsolutePosition(
    (documentRect.getWidth() - image.getScaledWidth()) / 2,
    (documentRect.getHeight() - image.getScaledHeight()) / 2);

代码示例来源:origin: com.itextpdf/itextpdf

Image tim = img_store.get(src);
  if (tim != null)
    img = Image.getInstance(tim);
  src = new File(path, src).getPath();
img = Image.getInstance(src);
if (img == null)
  return null;
float heightInPoints = HtmlUtilities.parseLength(height, actualFontSize);
if (widthInPoints > 0 && heightInPoints > 0) {
  img.scaleAbsolute(widthInPoints, heightInPoints);
} else if (widthInPoints > 0) {
  heightInPoints = img.getHeight() * widthInPoints
      / img.getWidth();
  img.scaleAbsolute(widthInPoints, heightInPoints);
} else if (heightInPoints > 0) {
  widthInPoints = img.getWidth() * heightInPoints
      / img.getHeight();
  img.scaleAbsolute(widthInPoints, heightInPoints);
  img.setSpacingBefore(Float.parseFloat(before));
String after = chain.getProperty(HtmlTags.AFTER);
if (after != null)
  img.setSpacingAfter(Float.parseFloat(after));
img.setWidthPercentage(0);
return img;

代码示例来源:origin: com.itextpdf/itextpdf

put(PdfName.TYPE, PdfName.XOBJECT);
put(PdfName.SUBTYPE, PdfName.IMAGE);
put(PdfName.WIDTH, new PdfNumber(image.getWidth()));
put(PdfName.HEIGHT, new PdfNumber(image.getHeight()));
if (image.getLayer() != null)
  put(PdfName.OC, image.getLayer().getRef());
if (image.isMask() && (image.getBpc() == 1 || image.getBpc() > 0xff))
  put(PdfName.IMAGEMASK, PdfBoolean.PDFTRUE);
if (maskRef != null) {
  if (image.isSmask())
    put(PdfName.SMASK, maskRef);
  else
    put(PdfName.MASK, maskRef);
if (image.isMask() && image.isInverted())
  put(PdfName.DECODE, new PdfLiteral("[1 0]"));
if (image.isInterpolation())
  put(PdfName.INTERPOLATE, PdfBoolean.PDFTRUE);
InputStream is = null;
try {
  int transparency[] = image.getTransparency();
  if (transparency != null && !image.isMask() && maskRef == null) {
    StringBuilder s = new StringBuilder("[");
    for (int k = 0; k < transparency.length; ++k)
  if (image.isImgRaw()) {
    int colorspace = image.getColorspace();

代码示例来源:origin: Swati4star/Images-to-PDF

Image image = Image.getInstance(imagesUri.get(i));
image.setBorder(0);
float pageWidth = document.getPageSize().getWidth(); // - (mMarginLeft + mMarginRight);
float pageHeight = document.getPageSize().getHeight(); // - (mMarginBottom + mMarginTop);
image.scaleToFit(pageWidth, pageHeight);
image.setAbsolutePosition(
    (documentRect.getWidth() - image.getScaledWidth()) / 2,
    (documentRect.getHeight() - image.getScaledHeight()) / 2);
document.add(image);

代码示例来源:origin: com.itextpdf/itextg

if (image.getLayer() != null)
  beginLayer(image.getLayer());
if (isTagged()) {
  if (inText)
  image.setAccessibleAttribute(PdfName.BBOX, new PdfArray(new float[] {left, bottom, right, top}));
if (writer != null && image.isImgTemplate()) {
  writer.addDirectImageSimple(image);
  PdfTemplate template = image.getTemplateData();
  if (image.getAccessibleAttributes() != null) {
    for (PdfName key : image.getAccessibleAttributes().keySet()) {
      template.setAccessibleAttribute(key, image.getAccessibleAttribute(key));
    PdfName name;
    PageResources prs = getPageResources();
    Image maskImage = image.getImageMask();
    if (maskImage != null) {
      name = writer.addDirectImageSimple(maskImage);
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)

代码示例来源:origin: com.itextpdf/itextpdf

cb.clip();
cb.newPath();
bmp.scaleAbsolute(destWidth * bmp.getWidth() / srcWidth, -destHeight * bmp.getHeight() / srcHeight);
bmp.setAbsolutePosition(xDest - destWidth * xSrc / srcWidth, yDest + destHeight * ySrc / srcHeight - bmp.getScaledHeight());
cb.addImage(bmp);
cb.restoreState();

代码示例来源:origin: com.liumapp.signature/sign-helper

public static void main(String[] args) {
  PdfStamper stamper = null;
  try {
    PdfReader reader = new PdfReader(dataPath + "/test.pdf");
    stamper = new PdfStamper(reader , new FileOutputStream(dataPath + "/output_onlyImage.pdf"));
    PdfContentByte over = stamper.getOverContent(1);
    Image image = Image.getInstance(dataPath +"/sign.png");
    image.setAbsolutePosition(0 , 0);
    image.scaleToFit(300 , 109);
    over.addImage(image);
  } catch (IOException e) {
    e.printStackTrace();
  } catch (DocumentException e) {
    e.printStackTrace();
  } finally {
    try {
      stamper.close();
    } catch (DocumentException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

代码示例来源:origin: com.itextpdf/itextpdf

/**
 * Constructs a chunk containing an <CODE>Image</CODE>.
 *
 * @param image
 *            the image
 * @param offsetX
 *            the image offset in the x direction
 * @param offsetY
 *            the image offset in the y direction
 */
public Chunk(final Image image, final float offsetX, final float offsetY) {
  this(OBJECT_REPLACEMENT_CHARACTER, new Font());
  Image copyImage = Image.getInstance(image);
  copyImage.setAbsolutePosition(Float.NaN, Float.NaN);
  setAttribute(IMAGE, new Object[] { copyImage, new Float(offsetX),
      new Float(offsetY), Boolean.FALSE });
  this.role = null;
}

代码示例来源:origin: com.itextpdf/itextpdf

BufferedImage img = tp.getImage();
Rectangle2D rect = tp.getAnchorRect();
com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(img, null);
PdfPatternPainter pattern = cb.createPattern(image.getWidth(), image.getHeight());
AffineTransform inverse = this.normalizeMatrix();
inverse.translate(rect.getX(), rect.getY());
inverse.scale(rect.getWidth() / image.getWidth(), -rect.getHeight() / image.getHeight());
double[] mx = new double[6];
inverse.getMatrix(mx);
pattern.setPatternMatrix((float)mx[0], (float)mx[1], (float)mx[2], (float)mx[3], (float)mx[4], (float)mx[5]) ;
image.setAbsolutePosition(0,0);
pattern.addImage(image);
if (fill)
com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(img, null);
PdfPatternPainter pattern = cb.createPattern(width, height);
image.setAbsolutePosition(0,0);
pattern.addImage(image);
if (fill)

代码示例来源:origin: yeokm1/docs-to-pdf-converter

Image image = Image.getInstance(bufImg, null);
document.setPageSize(new Rectangle(image.getScaledWidth(), image.getScaledHeight()));
document.newPage();
image.setAbsolutePosition(0, 0);
document.add(image);

代码示例来源:origin: wxynihao/book118-downloader

/**
 * 使用图片创建PDF文件
 *
 * @param srcPahOfImg  图片文件夹路径
 * @param desPathOfPdf PDF存储路径
 * @throws DocumentException pdf相关错误
 * @throws IOException       图片相关错误
 */
public static void creatPDF(String srcPahOfImg, String desPathOfPdf) throws DocumentException, IOException {
  File file = new File(srcPahOfImg);
  File[] picFiles = file.listFiles();
  if (picFiles == null || picFiles.length == 0) {
    return;
  }
  List<File> files = Arrays.asList(picFiles);
  //需要根据第一页创建document的大小
  //如果不根据第一页创建,即使修改document的大小也不会生效,困惑
  Image firstImg = Image.getInstance(files.get(0).getCanonicalPath());
  Document document = new Document(new Rectangle(firstImg.getWidth(), firstImg.getHeight()), 0, 0, 0, 0);
  PdfWriter.getInstance(document, new FileOutputStream(desPathOfPdf));
  document.open();
  document.add(firstImg);
  for (int i = 1; i < files.size(); i++) {
    Image img = Image.getInstance(files.get(i).getCanonicalPath());
    document.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
    document.add(img);
  }
  document.close();
}

代码示例来源:origin: grakic/jfreesteel

ImageIO.write((BufferedImage) photo, "jpeg", bas);
byte[] data = bas.toByteArray();
Image image = Image.getInstance(data);
image.setAbsolutePosition(60, 572);
image.setBorder(Image.BOX);
image.setBorderWidth(1f);
image.scaleAbsolute(119, 158);
writer.getDirectContent().addImage(image);

代码示例来源:origin: com.itextpdf/itextpdf

if (image.hasAbsoluteY()) {
  graphics.addImage(image);
  pageEmpty = false;
if (currentHeight != 0 && indentTop() - currentHeight - image.getScaledHeight() < indentBottom()) {
  if (!strictImageSequence && imageWait == null) {
    imageWait = image;
  if (currentHeight != 0 && indentTop() - currentHeight - image.getScaledHeight() < indentBottom()) {
    imageWait = image;
    return;
boolean textwrap = (image.getAlignment() & Image.TEXTWRAP) == Image.TEXTWRAP
    && !((image.getAlignment() & Image.MIDDLE) == Image.MIDDLE);
boolean underlying = (image.getAlignment() & Image.UNDERLYING) == Image.UNDERLYING;
float diff = leading / 2;
if (textwrap) {
  diff += leading;
float lowerleft = indentTop() - currentHeight - image.getScaledHeight() -diff;
float mt[] = image.matrix();
float startPosition = indentLeft() - mt[4];
if ((image.getAlignment() & Image.RIGHT) == Image.RIGHT) startPosition = indentRight() - image.getScaledWidth() - mt[4];
if ((image.getAlignment() & Image.MIDDLE) == Image.MIDDLE) startPosition = indentLeft() + (indentRight() - indentLeft() - image.getScaledWidth()) / 2 - mt[4];
if (image.hasAbsoluteX()) startPosition = image.getAbsoluteX();
if (textwrap) {
  if (imageEnd < 0 || imageEnd < currentHeight + image.getScaledHeight() + diff) {
    imageEnd = currentHeight + image.getScaledHeight() + diff;

相关文章

微信公众号

最新文章

更多