Example usage for org.apache.poi.xwpf.usermodel XWPFRun getEmbeddedPictures

List of usage examples for org.apache.poi.xwpf.usermodel XWPFRun getEmbeddedPictures

Introduction

In this page you can find the example usage for org.apache.poi.xwpf.usermodel XWPFRun getEmbeddedPictures.

Prototype

public List<XWPFPicture> getEmbeddedPictures() 

Source Link

Document

Returns the embedded pictures of the run.

Usage

From source file:mj.ocraptor.extraction.tika.parser.microsoft.ooxml.XWPFWordExtractorDecorator.java

License:Apache License

private TmpFormatting processRun(XWPFRun run, XWPFParagraph paragraph, XHTMLContentHandler xhtml,
        TmpFormatting tfmtg) throws SAXException, XmlException, IOException {
    // True if we are currently in the named style tag:
    if (run.isBold() != tfmtg.isBold()) {
        if (tfmtg.isItalic()) {
            xhtml.endElement("i");
            tfmtg.setItalic(false);//from ww  w.j a  v  a  2 s .c  o m
        }
        if (run.isBold()) {
            xhtml.startElement("b");
        } else {
            xhtml.endElement("b");
        }
        tfmtg.setBold(run.isBold());
    }

    if (run.isItalic() != tfmtg.isItalic()) {
        if (run.isItalic()) {
            xhtml.startElement("i");
        } else {
            xhtml.endElement("i");
        }
        tfmtg.setItalic(run.isItalic());
    }

    boolean addedHREF = false;
    if (run instanceof XWPFHyperlinkRun) {
        XWPFHyperlinkRun linkRun = (XWPFHyperlinkRun) run;
        XWPFHyperlink link = linkRun.getHyperlink(document);
        if (link != null && link.getURL() != null) {
            xhtml.startElement("a", "href", link.getURL());
            addedHREF = true;
        } else if (linkRun.getAnchor() != null && linkRun.getAnchor().length() > 0) {
            xhtml.startElement("a", "href", "#" + linkRun.getAnchor());
            addedHREF = true;
        }
    }

    xhtml.characters(run.toString());

    // If we have any pictures, output them
    for (XWPFPicture picture : run.getEmbeddedPictures()) {
        if (paragraph.getDocument() != null) {
            XWPFPictureData data = picture.getPictureData();
            if (data != null) {
                AttributesImpl attr = new AttributesImpl();

                attr.addAttribute("", "src", "src", "CDATA", "embedded:" + data.getFileName());
                attr.addAttribute("", "alt", "alt", "CDATA", picture.getDescription());

                xhtml.startElement("img", attr);
                xhtml.endElement("img");
            }
        }
    }

    if (addedHREF) {
        xhtml.endElement("a");
    }

    return tfmtg;
}

From source file:offishell.word.WordHeleper.java

License:MIT License

/**
 * <p>//from w w  w . j  a v  a  2  s . c om
 * Helper method to clone {@link XWPFRun}.
 * </p>
 * 
 * @param in
 * @param out
 * @param model
 */
public static void copy(XWPFRun in, XWPFRun out, UnaryOperator<String> converter) {
    // copy
    out.setBold(in.isBold());
    out.setCapitalized(in.isCapitalized());
    out.setCharacterSpacing(in.getCharacterSpacing());
    out.setColor(in.getColor());
    out.setDoubleStrikethrough(in.isDoubleStrikeThrough());
    out.setEmbossed(in.isEmbossed());
    out.setFontFamily(in.getFontFamily());
    out.setFontSize(in.getFontSize());
    out.setImprinted(in.isImprinted());
    out.setItalic(in.isItalic());
    out.setKerning(in.getKerning());
    out.setShadow(in.isShadowed());
    out.setSmallCaps(in.isSmallCaps());
    out.setStrikeThrough(in.isStrikeThrough());
    out.setVerticalAlignment(out.getVerticalAlignment().toString());
    out.setTextPosition(in.getTextPosition());
    out.setUnderline(in.getUnderline());

    // copy context
    CTR inCTR = in.getCTR();
    CTRPr inPR = inCTR.getRPr();
    CTR outCTR = out.getCTR();
    CTRPr outPR = outCTR.isSetRPr() ? outCTR.getRPr() : outCTR.addNewRPr();
    outPR.set(inCTR.getRPr());
    out.setVerticalAlignment(
            inPR == null || inPR.getVertAlign() == null ? "baseline" : inPR.getVertAlign().toString());

    // // copy tab
    // CTEmpty[] tabs = inCTR.getTabArray();
    //
    // if (tabs.length != 0) {
    // out.addTab();
    // }
    outCTR.setAnnotationRefArray(inCTR.getAnnotationRefList().toArray(CTEmpty[]::new));
    outCTR.setBrArray(inCTR.getBrList().toArray(CTBr[]::new));
    outCTR.setCommentReferenceArray(inCTR.getCommentReferenceList().toArray(CTMarkup[]::new));
    outCTR.setContinuationSeparatorArray(inCTR.getContinuationSeparatorList().toArray(CTEmpty[]::new));
    outCTR.setCrArray(inCTR.getCrList().toArray(CTEmpty[]::new));
    outCTR.setDelInstrTextArray(inCTR.getDelInstrTextList().toArray(CTText[]::new));
    outCTR.setDrawingArray(inCTR.getDrawingList().toArray(CTDrawing[]::new));
    outCTR.setEndnoteRefArray(inCTR.getEndnoteRefList().toArray(CTEmpty[]::new));
    outCTR.setFldCharArray(inCTR.getFldCharList().toArray(CTFldChar[]::new));
    outCTR.setFootnoteRefArray(inCTR.getFootnoteRefList().toArray(CTEmpty[]::new));
    outCTR.setInstrTextArray(inCTR.getInstrTextList().toArray(CTText[]::new));
    outCTR.setLastRenderedPageBreakArray(inCTR.getLastRenderedPageBreakList().toArray(CTEmpty[]::new));
    outCTR.setObjectArray(inCTR.getObjectList().toArray(CTObject[]::new));
    outCTR.setPictArray(inCTR.getPictList().toArray(CTPicture[]::new));
    outCTR.setPtabArray(inCTR.getPtabList().toArray(CTPTab[]::new));
    outCTR.setSymArray(inCTR.getSymList().toArray(CTSym[]::new));
    outCTR.setTabArray(inCTR.getTabList().toArray(CTEmpty[]::new));

    // copy image
    for (XWPFPicture inPicture : in.getEmbeddedPictures()) {
        try {
            XWPFPictureData inData = inPicture.getPictureData();
            String outId = out.getDocument().addPictureData(new ByteArrayInputStream(inData.getData()),
                    inData.getPictureType());

            select(CTBlip.class, outCTR).to(blip -> blip.setEmbed(outId));
        } catch (Exception e) {
            throw I.quiet(e);
        }
    }

    // copy text
    write(out, converter.apply(in.text()));
}

From source file:org.articleEditor.insertContent.DocumentUpdater1.java

License:Apache License

DocumentPosition searchRun(XWPFRun run, int offset) throws BadLocationException {
    for (XWPFPicture picture : run.getEmbeddedPictures()) {
        // currentOffset++;
    }//  w w  w .ja va  2  s.  com
    List<CTText> texts = run.getCTR().getTList();
    int textIndex = 0;
    for (CTText text : texts) {
        String textValue = text.getStringValue();
        int textLength = textValue.length();
        if (currentOffset + textLength >= offset) {// || (currentOffset + textLength == offset && !textValue.endsWith("\n"))) {
            DocumentPosition position = new DocumentPosition();
            position.run = run;
            position.text = text;
            position.offsetInText = offset - currentOffset;
            position.positionInRun = textIndex;
            return position;
        } else {
            currentOffset += textLength;
        }
        textIndex++;
    }
    return null;
}

From source file:org.articleEditor.insertContent.POIDocxReader.java

License:Apache License

protected void processRun(XWPFRun run) throws BadLocationException {
    charAttrs = new SimpleAttributeSet();

    if (run.getFontSize() > 0) {
        int size = run.getFontSize();
        StyleConstants.setFontSize(charAttrs, size);
    }//from  w w  w .  j  a  v  a2  s .  com
    StyleConstants.setBold(charAttrs, run.isBold());
    StyleConstants.setItalic(charAttrs, run.isItalic());
    StyleConstants.setStrikeThrough(charAttrs, run.isStrike());
    boolean underlined = run.getUnderline() != UnderlinePatterns.NONE;
    StyleConstants.setUnderline(charAttrs, underlined);
    VerticalAlign verticalAlignment = run.getSubscript();
    if (verticalAlignment == VerticalAlign.SUBSCRIPT) {
        StyleConstants.setSubscript(parAttrs, true);
    } else if (verticalAlignment == VerticalAlign.SUPERSCRIPT) {
        StyleConstants.setSuperscript(parAttrs, true);
    } else {
        StyleConstants.setSubscript(parAttrs, false);
        StyleConstants.setSuperscript(parAttrs, false);
    }
    if (run.getFontFamily() != null) {
        StyleConstants.setFontFamily(charAttrs, run.getFontFamily());
    }
    if (run.getColor() != null) {
        String name = run.getColor();
        if (!name.toLowerCase().equals("auto")) {
            Color color = Color.decode("#" + name);
            StyleConstants.setForeground(charAttrs, color);
        }
    }

    // Not working
    if (run.getCTR().getRPr() != null && run.getCTR().getRPr().getHighlight() != null) {
        STHighlightColor.Enum colorEnum = run.getCTR().getRPr().getHighlight().getVal();
        Color color = decodeHighlightName(colorEnum);
        StyleConstants.setBackground(charAttrs, color);
    }

    for (XWPFPicture picture : run.getEmbeddedPictures()) {
        processPicture(picture);
    }
    String text = run.toString();
    document.insertString(currentOffset, text, charAttrs);
    currentOffset += text.length();
}

From source file:org.obeonetwork.m2doc.generator.UserContentRawCopy.java

License:Open Source License

/**
 * createPictures in document./* www. j a  v a2s.c om*/
 * 
 * @param inputRun
 *            input Run
 * @param outputDoc
 *            output Document
 * @throws InvalidFormatException
 *             InvalidFormatException
 */
private void createPictures(XWPFRun inputRun, XWPFDocument outputDoc) throws InvalidFormatException {
    // Add picture in document and keep relation id change idRelation reference
    for (XWPFPicture inputPic : inputRun.getEmbeddedPictures()) {
        byte[] img = inputPic.getPictureData().getData();
        // Put image in doc and get idRelation
        String idRelationOutput = outputDoc.addPictureData(img, inputPic.getPictureData().getPictureType());
        String idRelationInput = inputPic.getCTPicture().getBlipFill().getBlip().getEmbed();
        inputPicuteIdToOutputmap.put(idRelationInput, idRelationOutput);
    }
}