@Override public List<Object> apply(Object o) { if (o instanceof Drawing) { Object o2 = ((Drawing)o).getAnchorOrInline().get(0); if (o2 instanceof Anchor) { extent = ((Anchor)o2).getExtent(); return null; } if (o2 instanceof Inline) { extent = ((Inline)o2).getExtent(); return null; } } return null; }
org.docx4j.wml.Drawing drawing = factory.createDrawing(); run.getContent().add(drawing); drawing.getAnchorOrInline().add(inline);
drawing.getAnchorOrInline().add(inline);
org.docx4j.wml.Drawing drawing = docx.getFactory().createDrawing(); run.getContent().add(drawing); drawing.getAnchorOrInline().add(inline); return run; } catch (Exception e) {
@Override public List<Object> apply(Object o) { if (o instanceof Drawing) { Object o2 = ((Drawing)o).getAnchorOrInline().get(0); if (o2 instanceof Anchor) { extent = ((Anchor)o2).getExtent(); return null; } if (o2 instanceof Inline) { extent = ((Inline)o2).getExtent(); return null; } } return null; }
/** * We create an object factory and use it to create a paragraph and a run. * Then we add the run to the paragraph. Next we create a drawing and * add it to the run. Finally we add the inline object to the drawing and * return the paragraph. * * @param inline The inline object containing the image. * @return the paragraph containing the image */ private static P addInlineImageToParagraph(Inline inline) { // Now add the in-line image to a paragraph org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory(); P paragraph = factory.createP(); R run = factory.createR(); paragraph.getContent().add(run); Drawing drawing = factory.createDrawing(); run.getContent().add(drawing); drawing.getAnchorOrInline().add(inline); return paragraph; }
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; }
Drawing drawing = factory.createDrawing(); run.getRunContent().add(drawing); drawing.getAnchorOrInline().add(inline);
Drawing drawing = factory.createDrawing(); run.getContent().add(drawing); drawing.getAnchorOrInline().add(inline); return paragraph;
org.docx4j.wml.Drawing drawing = factory.createDrawing(); run.getContent().add(drawing); drawing.getAnchorOrInline().add(inline);
org.docx4j.wml.Drawing drawing = Context.getWmlObjectFactory().createDrawing(); run.getContent().add(drawing); drawing.getAnchorOrInline().add(inline);
drawing.getAnchorOrInline().add(inline);
drawing.getAnchorOrInline() .add(inline); run.getContent()
@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); } }
@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); } }