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; } }
public static void scaleToFit(Image img, Rectangle box) { float scaleWidth = box.getWidth() / img.getWidth(); float scaleHeight = box.getHeight() / img.getHeight(); float scale = Math.min(scaleHeight, scaleWidth); if (scale < 1) img.scalePercent(scale * 100f); }
/** * 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); }
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; } }
/** * 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); }
public static void adjustOrScaleToFit(Image img, Dimension dim, Rectangle box) { if (dim == null) { scaleToFit(img, box); return; } float width = img.getWidth(); switch (dim.unit()) { case Percent: width = box.getWidth() * dim.amount() / 100f; break; case Px: width = dim.amount(); break; } // W --> w // H --> h •••> h = w * H / W float height = width * img.getHeight() / img.getWidth(); img.scaleAbsolute(width, height); }
/** * 使用图片创建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(); }
appearance.setImageScale((float) ((1)/img.getWidth()));
this.setImageScalePercentage(width / image.getWidth()); return null;
this.setImageScalePercentage(width / image.getWidth()); return null;
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);
public boolean setFieldAsImage(String field, Image image) { try { if (Float.isNaN(image.getAbsoluteX())) image.setAbsolutePosition(0, image.getAbsoluteY()); if (Float.isNaN(image.getAbsoluteY())) image.setAbsolutePosition(image.getAbsoluteY(), 0); PdfTemplate tmpl = PdfTemplate.createTemplate(wrt, image.getWidth(), image.getHeight()); tmpl.addImage(image); PdfStream str = tmpl.getFormXObject(PdfStream.NO_COMPRESSION); PdfIndirectReference ref = wrt.addToBody(str).getIndirectReference(); PdfDictionary d = new PdfDictionary(); d.put(PdfName.N, ref); return setField(field, d); } catch (Exception de) { throw new ExceptionConverter(de); } }
writeWord(os, META_SETWINDOWEXT); writeWord(os, (int)image.getHeight()); writeWord(os, (int)image.getWidth()); writeDWord(os, 0x00cc0020); writeWord(os, (int)image.getHeight()); writeWord(os, (int)image.getWidth()); writeWord(os, 0); writeWord(os, 0); writeWord(os, (int)image.getHeight()); writeWord(os, (int)image.getWidth()); writeWord(os, 0); writeWord(os, 0);
public boolean setFieldAsImage(String field, Image image) { try { if (Float.isNaN(image.getAbsoluteX())) image.setAbsolutePosition(0, image.getAbsoluteY()); if (Float.isNaN(image.getAbsoluteY())) image.setAbsolutePosition(image.getAbsoluteY(), 0); PdfTemplate tmpl = PdfTemplate.createTemplate(wrt, image.getWidth(), image.getHeight()); tmpl.addImage(image); PdfStream str = tmpl.getFormXObject(PdfStream.NO_COMPRESSION); PdfIndirectReference ref = wrt.addToBody(str).getIndirectReference(); PdfDictionary d = new PdfDictionary(); d.put(PdfName.N, ref); return setField(field, d); } catch (Exception de) { throw new ExceptionConverter(de); } }
private void writeAttributes(final Image image) { if (image != null) { this.setAttribute(PdfName.O, PdfName.LAYOUT); if (image.getWidth() > 0){ this.setAttribute(PdfName.WIDTH, new PdfNumber(image.getWidth())); } if (image.getHeight() > 0){ this.setAttribute(PdfName.HEIGHT, new PdfNumber(image.getHeight())); } PdfRectangle rect = new PdfRectangle(image, image.getRotation()); this.setAttribute(PdfName.BBOX, rect); if (image.getBackgroundColor() != null){ BaseColor color = image.getBackgroundColor(); this.setAttribute(PdfName.BACKGROUNDCOLOR, new PdfArray(new float[] {color.getRed()/255f, color.getGreen()/255f, color.getBlue()/255f}) ); } } }
writeWord(os, META_SETWINDOWEXT); writeWord(os, (int)image.getHeight()); writeWord(os, (int)image.getWidth()); writeDWord(os, 0x00cc0020); writeWord(os, (int)image.getHeight()); writeWord(os, (int)image.getWidth()); writeWord(os, 0); writeWord(os, 0); writeWord(os, (int)image.getHeight()); writeWord(os, (int)image.getWidth()); writeWord(os, 0); writeWord(os, 0);
private void writeAttributes(final Image image) { if (image != null) { this.setAttribute(PdfName.O, PdfName.LAYOUT); if (image.getWidth() > 0){ this.setAttribute(PdfName.WIDTH, new PdfNumber(image.getWidth())); } if (image.getHeight() > 0){ this.setAttribute(PdfName.HEIGHT, new PdfNumber(image.getHeight())); } PdfRectangle rect = new PdfRectangle(image, image.getRotation()); this.setAttribute(PdfName.BBOX, rect); if (image.getBackgroundColor() != null){ BaseColor color = image.getBackgroundColor(); this.setAttribute(PdfName.BACKGROUNDCOLOR, new PdfArray(new float[] {color.getRed()/255f, color.getGreen()/255f, color.getBlue()/255f}) ); } } }
Paragraph paragraph = new Paragraph(image.getWidth() + " x " + image.getHeight()); paragraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(paragraph);