public static void displayImageInfo(ImageInfo info) { ImageSize size = info.getSize(); Dimension2D dPt = size.getDimensionPt(); Dimension dPx = size.getDimensionPx(); log.debug(info.getOriginalURI() + " " + info.getMimeType() + " " + Math.round(dPx.getWidth()) + "x" + Math.round(dPx.getHeight())); log.debug("Resolution:" + Math.round(size.getDpiHorizontal()) + "x" + Math.round(size.getDpiVertical())); log.debug("Print size: " + Math.round(dPt.getWidth() / 72) + "\" x" + Math.round(dPt.getHeight() / 72) + "\""); }
/** * Create a <wp:inline> element suitable for this image, * which can be _embedded_ in w:p/w:r/w:drawing. * @param filenameHint Any text, for example the original filename * @param altText Like HTML's alt text * @param id1 An id unique in the document * @param id2 Another id unique in the document * @param cx Image width in twip * @param link true if this is to be linked not embedded * None of these things seem to be exposed in Word 2007's * user interface, but Word won't open the document if * any of the attributes these go in (except @ desc) aren't present! * @throws Exception */ public Inline createImageInline(String filenameHint, String altText, int id1, int id2, long cx, boolean link) throws Exception { ImageSize size = imageInfo.getSize(); Dimension2D dPt = size.getDimensionPt(); double imageWidthTwips = dPt.getWidth() * 20; log.debug("imageWidthTwips: " + imageWidthTwips); long cy; log.debug("Scaling image height to retain aspect ratio"); cy = UnitsOfMeasurement.twipToEMU(dPt.getHeight() * 20 * cx / imageWidthTwips); // Now convert cx to EMU cx = UnitsOfMeasurement.twipToEMU(cx); log.debug("cx=" + cx + "; cy=" + cy); return createImageInline(filenameHint, altText, id1, id2, cx, cy, link); }
ImageSize size = imagePart.getImageInfo().getSize(); double ratio = (double) size.getHeightPx() / (double) size.getWidthPx(); log.debug("fit ratio: " + ratio);
ImageSize size = imageInfo.getSize();
public static CxCy scale(ImageInfo imageInfo, double xEmu, double yEmu) { ImageSize size = imageInfo.getSize(); double iwEmu = toEmu(size.getWidthPx(), size.getDpiHorizontal()); double ihEmu = toEmu(size.getHeightPx(), size.getDpiVertical()); return scaleToFit(iwEmu, ihEmu, xEmu, yEmu); }
ImageSize size = imagePart.getImageInfo().getSize(); double ratio = (double) size.getHeightPx() / (double) size.getWidthPx(); log.debug("fit ratio: " + ratio);
/** {@inheritDoc} */ public ImageSize getSize() { return getInfo().getSize(); }
public static void displayImageInfo(ImageInfo info) { ImageSize size = info.getSize(); Dimension2D dPt = size.getDimensionPt(); Dimension dPx = size.getDimensionPx(); log.debug(info.getOriginalURI() + " " + info.getMimeType() + " " + Math.round(dPx.getWidth()) + "x" + Math.round(dPx.getHeight())); log.debug("Resolution:" + Math.round(size.getDpiHorizontal()) + "x" + Math.round(size.getDpiVertical())); log.debug("Print size: " + Math.round(dPt.getWidth() / 72) + "\" x" + Math.round(dPt.getHeight() / 72) + "\""); }
/** {@inheritDoc} */ public void handleImage(RenderingContext context, Image image, Rectangle pos) throws IOException { Java2DRenderingContext java2dContext = (Java2DRenderingContext)context; ImageInfo info = image.getInfo(); ImageGraphics2D imageG2D = (ImageGraphics2D)image; Dimension dim = info.getSize().getDimensionMpt(); Graphics2D g2d = (Graphics2D)java2dContext.getGraphics2D().create(); g2d.translate(pos.x, pos.y); double sx = pos.width / dim.getWidth(); double sy = pos.height / dim.getHeight(); g2d.scale(sx, sy); Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, dim.getWidth(), dim.getHeight()); imageG2D.getGraphics2DImagePainter().paint(g2d, area); g2d.dispose(); }
/** {@inheritDoc} */ public void handleImage(RenderingContext context, Image image, Rectangle pos) throws IOException { PSRenderingContext psContext = (PSRenderingContext) context; PSGenerator gen = psContext.getGenerator(); ImageRawPNG png = (ImageRawPNG) image; float x = (float) pos.getX() / 1000f; float y = (float) pos.getY() / 1000f; float w = (float) pos.getWidth() / 1000f; float h = (float) pos.getHeight() / 1000f; Rectangle2D targetRect = new Rectangle2D.Float(x, y, w, h); ImageEncoder encoder = new ImageEncoderPNG(png); ImageInfo info = image.getInfo(); Dimension imgDim = info.getSize().getDimensionPx(); String imgDescription = image.getClass().getName(); ColorModel cm = png.getColorModel(); PSImageUtils.writeImage(encoder, imgDim, imgDescription, targetRect, cm, gen); }
/** {@inheritDoc} */ public void handleImage(RenderingContext context, Image image, Rectangle pos) throws IOException { PSRenderingContext psContext = (PSRenderingContext)context; PSGenerator gen = psContext.getGenerator(); ImageRawCCITTFax ccitt = (ImageRawCCITTFax)image; float x = (float)pos.getX() / 1000f; float y = (float)pos.getY() / 1000f; float w = (float)pos.getWidth() / 1000f; float h = (float)pos.getHeight() / 1000f; Rectangle2D targetRect = new Rectangle2D.Float( x, y, w, h); ImageInfo info = image.getInfo(); ImageEncoder encoder = new ImageEncoderCCITTFax(ccitt); PSImageUtils.writeImage(encoder, info.getSize().getDimensionPx(), info.getOriginalURI(), targetRect, ccitt.getColorSpace(), 1, false, gen); }
/** {@inheritDoc} */ public void handleImage(RenderingContext context, Image image, Rectangle pos) throws IOException { PSRenderingContext psContext = (PSRenderingContext)context; PSGenerator gen = psContext.getGenerator(); ImageRawJPEG jpeg = (ImageRawJPEG)image; float x = (float)pos.getX() / 1000f; float y = (float)pos.getY() / 1000f; float w = (float)pos.getWidth() / 1000f; float h = (float)pos.getHeight() / 1000f; Rectangle2D targetRect = new Rectangle2D.Float( x, y, w, h); ImageInfo info = image.getInfo(); ImageEncoder encoder = new ImageEncoderJPEG(jpeg); PSImageUtils.writeImage(encoder, info.getSize().getDimensionPx(), info.getOriginalURI(), targetRect, jpeg.getColorSpace(), 8, jpeg.isInverted(), gen); }
/** {@inheritDoc} */ public void generateForm(RenderingContext context, Image image, PSImageFormResource form) throws IOException { PSRenderingContext psContext = (PSRenderingContext) context; PSGenerator gen = psContext.getGenerator(); ImageRawPNG png = (ImageRawPNG) image; ImageInfo info = image.getInfo(); String imageDescription = info.getMimeType() + " " + info.getOriginalURI(); ImageEncoder encoder = new ImageEncoderPNG(png); FormGenerator formGen = new ImageFormGenerator(form.getName(), imageDescription, info.getSize() .getDimensionPt(), info.getSize().getDimensionPx(), encoder, png.getColorSpace(), false); formGen.generate(gen); }
@Override public void inlineToDocx(WordprocessingMLPackage wordPackage, Text text, Object paramValue, Matcher paramsMatcher) { try { Image image = new Image(paramValue, paramsMatcher); if (image.isValid()) { Part part = resolveTextPartForDOCX(text, wordPackage); BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordPackage, part, image.imageContent); int originalWidth = imagePart.getImageInfo().getSize().getWidthPx(); int originalHeight = imagePart.getImageInfo().getSize().getHeightPx(); double widthScale = (double) image.width / (double) originalWidth; double heightScale = (double) image.height / (double) originalHeight; double actualScale = Math.min(widthScale, heightScale); long targetWidth = Math.round(originalWidth * actualScale); long targetHeight = Math.round(originalHeight * actualScale); Inline inline = imagePart.createImageInline("", "", docxUniqueId1++, docxUniqueId2++, XlsxUtils.convertPxToEmu(targetWidth), XlsxUtils.convertPxToEmu(targetHeight), false); org.docx4j.wml.Drawing drawing = new org.docx4j.wml.ObjectFactory().createDrawing(); R run = (R) text.getParent(); run.getContent().add(drawing); drawing.getAnchorOrInline().add(inline); text.setValue(""); } } catch (Exception e) { throw new ReportFormattingException("An error occurred while inserting bitmap to docx file", e); } }
public static CxCy scale(ImageInfo imageInfo, double xEmu, double yEmu) { ImageSize size = imageInfo.getSize(); double iwEmu = toEmu(size.getWidthPx(), size.getDpiHorizontal()); double ihEmu = toEmu(size.getHeightPx(), size.getDpiVertical()); return scaleToFit(iwEmu, ihEmu, xEmu, yEmu); }
/** {@inheritDoc} */ public void generateForm(RenderingContext context, Image image, PSImageFormResource form) throws IOException { PSRenderingContext psContext = (PSRenderingContext)context; PSGenerator gen = psContext.getGenerator(); ImageRawCCITTFax ccitt = (ImageRawCCITTFax)image; ImageInfo info = image.getInfo(); String imageDescription = info.getMimeType() + " " + info.getOriginalURI(); ImageEncoder encoder = new ImageEncoderCCITTFax(ccitt); FormGenerator formGen = new ImageFormGenerator( form.getName(), imageDescription, info.getSize().getDimensionPt(), info.getSize().getDimensionPx(), encoder, ccitt.getColorSpace(), 1, false); formGen.generate(gen); }
/** {@inheritDoc} */ public void generateForm(RenderingContext context, Image image, PSImageFormResource form) throws IOException { PSRenderingContext psContext = (PSRenderingContext)context; PSGenerator gen = psContext.getGenerator(); ImageRawJPEG jpeg = (ImageRawJPEG)image; ImageInfo info = image.getInfo(); String imageDescription = info.getMimeType() + " " + info.getOriginalURI(); ImageEncoder encoder = new ImageEncoderJPEG(jpeg); FormGenerator formGen = new ImageFormGenerator( form.getName(), imageDescription, info.getSize().getDimensionPt(), info.getSize().getDimensionPx(), encoder, jpeg.getColorSpace(), jpeg.isInverted()); formGen.generate(gen); }
/** {@inheritDoc} */ public void generateForm(RenderingContext context, Image image, PSImageFormResource form) throws IOException { PSRenderingContext psContext = (PSRenderingContext)context; PSGenerator gen = psContext.getGenerator(); ImageRendered imageRend = (ImageRendered)image; ImageInfo info = image.getInfo(); String imageDescription = info.getMimeType() + " " + info.getOriginalURI(); RenderedImage ri = imageRend.getRenderedImage(); FormGenerator formGen = new ImageFormGenerator( form.getName(), imageDescription, info.getSize().getDimensionPt(), ri, false); formGen.generate(gen); }
@Override public void inlineToDocx(WordprocessingMLPackage wordPackage, Text text, Object paramValue, Matcher paramsMatcher) { try { Image image = new Image(paramValue, paramsMatcher); if (image.isValid()) { BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordPackage, resolveTextPartForDOCX(text, wordPackage), image.imageContent); Inline inline = imagePart.createImageInline("", "", docxUniqueId1++, docxUniqueId2++, false); ImageSize oldSize = imagePart.getImageInfo().getSize(); double widthExtent = (double) image.width / oldSize.getWidthPx(); double heightExtent = (double) image.height / oldSize.getHeightPx(); inline.getExtent().setCx((long) (inline.getExtent().getCx() * widthExtent)); inline.getExtent().setCy((long) (inline.getExtent().getCy() * heightExtent)); org.docx4j.wml.Drawing drawing = new org.docx4j.wml.ObjectFactory().createDrawing(); R run = (R) text.getParent(); run.getContent().add(drawing); drawing.getAnchorOrInline().add(inline); text.setValue(""); } } catch (Exception e) { throw new ReportFormattingException("An error occurred while inserting bitmap to docx file", e); } }