BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wmlPackage, sourcePart, bytes); inline = imagePart.createImageInline( filenameHint, altText, id1, id2, false); ImageSize size = imagePart.getImageInfo().getSize(); double ratio = (double) size.getHeightPx() / (double) size.getWidthPx(); log.debug("fit ratio: " + ratio); cyl = (long)((double) cxl * ratio); inline = imagePart.createImageInline( filenameHint, altText, id1, id2, cxl, cyl, false);
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wmlPackage, sourcePart, bytes); inline = imagePart.createImageInline( filenameHint, altText, id1, id2, false); } else { ImageSize size = imagePart.getImageInfo().getSize(); double ratio = (double) size.getHeightPx() / (double) size.getWidthPx(); log.debug("fit ratio: " + ratio); cyl = (long)((double) cxl * ratio); inline = imagePart.createImageInline( filenameHint, altText, id1, id2, cxl, cyl, false); log.debug("incrementally saving " + imagePart.getPartName().getName()); ((UnzippedPartStore)wmlPackage.getTargetPartStore()).saveBinaryPart(imagePart); imagePart.setBinaryData(bb);//new byte[0]); imagePart.setImageInfo(null); // this might help as well
imagePart = BinaryPartAbstractImage.createImagePart( ddp.getPackage(), ddp, bytes); } catch (Exception e) { "image_broken.gif")); imagePart = BinaryPartAbstractImage.createImagePart( ddp.getPackage(), ddp, bytes); return imagePart.getSourceRelationships().get(0).getId();
public static void main(String[] args) throws Exception { //String uri = System.getProperty("user.dir") + "/sample-docs/metafile-samples/gradient.emf"; String uri = System.getProperty("user.dir") + "/sample-docs/metafile-samples/freehand_picture_saveas.wmf"; System.out.println(uri); //String uri = "/tmp/img4448.img"; ImageInfo ii = getImageInfo(new URL(uri)); displayImageInfo(ii); }
Part sourcePart, byte[] bytes, String mime) throws Exception { String ext = mimeToExt(mime); if (mime==null || ext==null) { log.warn("Null or unknown mime type; image introspection required!"); return createImagePart( opcPackage, sourcePart, bytes); (BinaryPartAbstractImage) ctm.newPartForContentType( mime, createImageName(opcPackage, sourcePart, proposedRelId, ext), null); + " with name " + imagePart.getPartName().toString()); imagePart.setBinaryData(bytes); imagePart.rels.add(sourcePart.addTargetPart(imagePart, proposedRelId));
public R newImage(final DocxRendererContext docx, byte[] bytes, String filenameHint, Attributes attributes, int id1, int id2) { try { BinaryPartAbstractImage imagePart = null; imagePart = BinaryPartAbstractImage.createImagePart(docx.getPackage(), docx.getContainerPart(), bytes); Inline inline = null; String altText = attributes.contains("alt") ? attributes.getValue("alt") : ""; inline = imagePart.createImageInline(filenameHint, altText, id1, id2, cx, cy, false); } else { if (cx > 0) { inline = imagePart.createImageInline(filenameHint, altText, id1, id2, cx, false); } else { if (maxWidth > 0) { inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false, maxWidth); } else { inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false);
/** * Possibility to put directly an image filePath instead of giving an image byte array * @param wordMLPackage * @param imageFile * */ public static BinaryPartAbstractImage createImagePart(WordprocessingMLPackage wordMLPackage, File imageFile) throws Exception { return createImagePart(wordMLPackage, wordMLPackage.getMainDocumentPart(), imageFile); }
/** * Workaround for the fact that Xalan doesn't let us pass an AtomicInteger into an extension * function. Instead, it converts it into an int, which means the object in our * bookmarkIdCounter parameter isn't updated. * * So here we wrap the AtomicInteger in a class, * * @author jharrop * */ public static class BookmarkCounter { protected AtomicInteger bookmarkId; BookmarkCounter(AtomicInteger bookmarkId) { this.bookmarkId = bookmarkId; } }
/** * Create a <wp:inline> element suitable for this image, * which can be _embedded_ in w:p/w:r/w:drawing. * If the image is wider than the page, it will be scaled * automatically. To avoid the deprecated warning, use the * same method, but with an additional argument of false appended. * @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 * 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 */ @Deprecated public Inline createImageInline(String filenameHint, String altText, int id1, int id2) throws Exception { return createImageInline(filenameHint, altText, id1, id2, false); }
/** * @param filenameHint * @param altText * @param id1 * @param id2 * @param link * @param maxWidth * @return * @throws Exception * @since 3.3.0 */ public Inline createImageInline(String filenameHint, String altText, int id1, int id2, boolean link, int maxWidth) throws Exception { // This signature can scale the image to specified maxWidth WordprocessingMLPackage wmlPackage = ((WordprocessingMLPackage) this.getPackage()); List<SectionWrapper> sections = wmlPackage.getDocumentModel().getSections(); PageDimensions page = sections.get(sections.size() - 1).getPageDimensions(); CxCy cxcy = CxCy.scale(imageInfo, page, maxWidth); return createImageInline(filenameHint, altText, id1, id2, cxcy.getCx(), cxcy.getCy(), link); }
ImageInfo info = ensureFormatIsSupported(imageFile, locByte, false); (BinaryPartAbstractImage) ctm.newPartForContentType( info.getMimeType(), createImageName(opcPackage, sourcePart, proposedRelId, ext), null); + " with name " + imagePart.getPartName().toString()); imagePart.setBinaryData(fis); imagePart.setImageInfo(info);
ImageInfo info = ensureFormatIsSupported(url, null, null, false); // final param doesn't matter in this case (BinaryPartAbstractImage) ctm.newPartForContentType( info.getMimeType(), createImageName(opcPackage, sourcePart, proposedRelId, ext), null); + " with name " + imagePart.getPartName().toString()); imagePart.getRelLast().setTargetMode("External"); opcPackage.getExternalResources().put(imagePart.getExternalTarget(), imagePart); imagePart.getRelLast().setTarget(url.toString()); imagePart.setImageInfo(info);
try { try { info = getImageInfo(url); displayImageInfo(info); } catch (Exception e) { convertToPNG(bais, fos, density); ByteArrayInputStream bais = new ByteArrayInputStream(bytes); fos = new FileOutputStream(imageFile); convertToPNG(bais, fos, density); bais.close(); getImageManager().getCache().clearCache(); try { info = getImageInfo(new URL("file:/" + imageFile.getAbsolutePath())); } catch (java.io.FileNotFoundException fnfe) { info = getImageInfo(new URL("file://" + imageFile.getAbsolutePath())); displayImageInfo(info); } else { throw new Docx4JException("Unsupported linked image type.");
(BinaryPartAbstractImage) ctm.newPartForContentType( mime, createImageName(opcPackage, sourcePart, proposedRelId, ext), null); + " with name " + imagePart.getPartName().toString()); imagePart.setBinaryData(bytes); imagePart.rels.add(sourcePart.addTargetPart(imagePart, proposedRelId));
private void putImage(WorksheetPart worksheetPart, SpreadsheetMLPackage pkg, BinaryPartAbstractImage imagePart, CTOneCellAnchor anchor) throws Docx4JException { PartName drawingPart = new PartName(worksheetPart.getPartName().getName().replace("worksheets/sheet", "drawings/drawing")); String imagePartName = imagePart.getPartName().getName(); Drawing drawing = (Drawing) pkg.getParts().get(drawingPart); int currentId = 0;
public static R createRunWithImage(WordprocessingMLPackage wordMLPackage, byte[] bytes, String filenameHint, String altText, Integer maxWidth) throws Exception { BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes); // creating random ids assuming they are unique // id must not be too large, otherwise Word cannot open the document int id1 = random.nextInt(100000); int id2 = random.nextInt(100000); if (filenameHint == null) { filenameHint = "dummyFileName"; } if (altText == null) { altText = "dummyAltText"; } Inline inline; if (maxWidth == null) { inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false); } else { inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false, maxWidth); } // Now add the inline in w:p/w:r/w:drawing org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory(); org.docx4j.wml.R run = factory.createR(); org.docx4j.wml.Drawing drawing = factory.createDrawing(); run.getContent().add(drawing); drawing.getAnchorOrInline().add(inline); return run; }
/** * Create an image part from the provided byte array, attach it to the * docx main document part, and return it. * * @param wordMLPackage * @param sourcePart * @param bytes * @return * @throws Exception */ public static BinaryPartAbstractImage createImagePart(WordprocessingMLPackage wordMLPackage, byte[] bytes) throws Exception { return createImagePart(wordMLPackage, wordMLPackage.getMainDocumentPart(), bytes); }
/** * Workaround for the fact that Xalan doesn't let us pass an AtomicInteger into an extension * function. Instead, it converts it into an int, which means the object in our * bookmarkIdCounter parameter isn't updated. * * So here we wrap the AtomicInteger in a class, * * @author jharrop * */ public static class BookmarkCounter { protected AtomicInteger bookmarkId; BookmarkCounter(AtomicInteger bookmarkId) { this.bookmarkId = bookmarkId; } }
Part sourcePart, byte[] bytes, String mime) throws Exception { String ext = mimeToExt(mime); if (mime==null || ext==null) { log.warn("Null or unknown mime type; image introspection required!"); return createImagePart( opcPackage, sourcePart, bytes); (BinaryPartAbstractImage) ctm.newPartForContentType( mime, createImageName(opcPackage, sourcePart, proposedRelId, ext), null); + " with name " + imagePart.getPartName().toString()); imagePart.setBinaryData(bytes); imagePart.rels.add(sourcePart.addTargetPart(imagePart, proposedRelId));
/** * Create a <wp:inline> element suitable for this image, * which can be _embedded_ in w:p/w:r/w:drawing. To avoid the deprecated warning, use the * same method, but with an additional argument of false appended. * @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 * 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 */ @Deprecated public Inline createImageInline(String filenameHint, String altText, int id1, int id2, long cx) throws Exception { return createImageInline(filenameHint, altText, id1, id2, cx, false); }