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

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

Introduction

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

Prototype

public int getTextPosition() 

Source Link

Document

This element specifies the amount by which text shall be raised or lowered for this run in relation to the default baseline of the surrounding non-positioned text.

Usage

From source file:apachepoitest.DocumentPropertyEnumerator.java

public static void showParagraphElementProperties(List<XWPFRun> rl) {
    System.out.println("\nELEMENTS: ");
    int counter = 1;
    for (XWPFRun r : rl) {
        if (r.toString().trim().length() > 0) {
            System.out.println("#" + counter++ + ": " + r.toString());
        } else {/*w  ww  . j  av a 2s .  c o  m*/
            //Ignore spaces, uncomment to display spaces and comment out "continue"
            //System.out.println("#" + counter++ + ": <SPACE>");
            continue;
        }
        if (r.getColor() != null) {
            System.out.println("COLOR: " + r.getColor());
        }
        if (r.getFontFamily() != null) {
            System.out.println("FONT: " + r.getFontFamily());
        }
        if (r.getFontSize() > 0) {
            System.out.println("FONT SIZE: " + r.getFontSize());
        }
        if (r.getPictureText().length() > 0) {
            System.out.println("PIC TEXT: " + r.getPictureText());
        }
        if (r.getTextPosition() > 0) {
            System.out.println("TEXT POS: " + r.getTextPosition());
        }
        if (r.isBold()) {
            System.out.println("BOLD: " + r.isBold());
        }
        if (r.isItalic()) {
            System.out.println("ITALIC: " + r.isItalic());
        }
        if (r.isStrike()) {
            System.out.println("STRIKETHROUGH: " + r.isStrike());
        }
        if (!r.getUnderline().toString().equals("NONE")) {
            System.out.println("UNDERLINE: " + r.getUnderline().toString());
        }
        if (!r.getSubscript().toString().equals("BASELINE")) {
            System.out.println("Subscript: " + r.getSubscript().toString());
        }
        System.out.println("");
    }
}

From source file:b01.officeLink.ExtendedWordDocument.java

License:Apache License

public boolean replaceInParagraph(XWPFParagraph para, String toReplace, String replaceWith) {
    boolean didReplace = false;

    if (para != null && toReplace != null && replaceWith != null) {
        //        setOrientation(para);

        List<XWPFRun> runs = para.getRuns();
        TextSegement found = para.searchText(toReplace, new PositionInParagraph());
        if (found != null) {
            if (found.getBeginRun() == found.getEndRun()) {
                // whole search string is in one Run
                XWPFRun run = runs.get(found.getBeginRun());
                String runText = run.getText(run.getTextPosition());

                //Support of Enter to transform it to a line break
                //------------------------------------------------
                String replaced = runText.replace(toReplace, replaceWith);
                replaceInRun(run, replaced);
                //------------------------------------------------
                //            run.setText(replaced, 0);
                //------------------------------------------------

                didReplace = true;/*from w w w.  ja v  a 2s  .c o m*/
            } else {
                // The search string spans over more than one Run
                // Put the Strings together
                StringBuilder b = new StringBuilder();
                for (int runPos = found.getBeginRun(); runPos <= found.getEndRun(); runPos++) {
                    XWPFRun run = runs.get(runPos);
                    b.append(run.getText(run.getTextPosition()));
                }
                String connectedRuns = b.toString();
                String replaced = connectedRuns.replace(toReplace, replaceWith);

                // The first Run receives the replaced String of all connected Runs
                XWPFRun partOne = runs.get(found.getBeginRun());
                //Support of Enter to transform it to a line break
                //------------------------------------------------
                replaceInRun(partOne, replaced);//replaceWith
                //partOne.setText(replaced, 0);
                //------------------------------------------------

                // Removing the text in the other Runs.
                for (int runPos = found.getBeginRun() + 1; runPos <= found.getEndRun(); runPos++) {
                    XWPFRun partNext = runs.get(runPos);
                    partNext.setText("", 0);
                }
                didReplace = true;
            }
        }
    }
    return didReplace;
}

From source file:com.altar.worddocreader.WordDocReaderMain.java

public static XWPFDocument replaceText(XWPFDocument doc, HashMap<String, String> keys) throws Exception {
    String txt = "";
    int txtPosition = 0;
    String key = "";
    String val = "";
    for (XWPFParagraph p : doc.getParagraphs()) { //Dkmandaki her bir paragraf okumas yaplyor.
        for (XWPFRun run : p.getRuns()) { //paragraf iindeki satrlar okunuyor.
            txtPosition = run.getTextPosition();
            txt = run.getText(txtPosition);
            for (Map.Entry<String, String> entry : keys.entrySet()) { //keymap iinde gnderilen alanlar keymap'teki deerleri ile deitiriliyor.
                key = entry.getKey();//from  w ww  .jav  a  2  s . com
                val = entry.getValue();
                if (txt != null && txt.indexOf(key) > -1) {
                    txt = txt.replace(key, val);
                    run.setText(txt, 0);
                }
            }
        }
    }

    return doc;

}

From source file:com.bxf.hradmin.testgen.service.impl.DocxTestGenerator.java

License:Open Source License

private NavigableMap<Integer, XWPFRun> getPosToRuns(XWPFParagraph paragraph) {
    int pos = 0;// ww w  .  j  a v  a2  s. c o  m
    NavigableMap<Integer, XWPFRun> map = new TreeMap<>();
    for (XWPFRun run : paragraph.getRuns()) {
        String runText = run.getText(run.getTextPosition());
        if (runText != null && runText.length() > 0) {
            for (int i = 0; i < runText.length(); i++) {
                map.put(pos + i, run);
            }
            pos += runText.length();
        }
    }
    return map;
}

From source file:com.siemens.sw360.licenseinfo.outputGenerators.DocxUtils.java

License:Open Source License

private static void replaceParagraph(XWPFParagraph paragraph, String placeHolder, String replaceText) {
    for (XWPFRun r : paragraph.getRuns()) {
        String text = r.getText(r.getTextPosition());
        if (text != null && text.contains(placeHolder)) {
            text = text.replace(placeHolder, replaceText);
            r.setText(text, 0);/*w  ww  .j  a va  2s  . c  o  m*/
        }
    }
}

From source file:offishell.word.WordHeleper.java

License:MIT License

/**
 * <p>/*from w w  w  .  j  a v  a  2s  .  c  o m*/
 * 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.eclipse.sw360.licenseinfo.outputGenerators.DocxUtils.java

License:Open Source License

private static void replaceParagraph(XWPFParagraph paragraph, String placeHolder, String replaceText) {
    for (XWPFRun run : paragraph.getRuns()) {
        String text = run.getText(run.getTextPosition());
        if (text != null && text.contains(placeHolder)) {
            text = text.replace(placeHolder, replaceText);
            String[] split = text.split("\n");
            run.setText(split[0], 0);//from   www  .j a va2s. c om
            for (int i = 1; i < split.length; i++) {
                run.addBreak();
                run.setText(split[i]);
            }
        }
    }
}

From source file:org.kino.server.api.contractgenerator.java

static private long replaceInParagraphs(Map<String, String> replacements, List<XWPFParagraph> xwpfParagraphs) {
    long count = 0;
    for (XWPFParagraph paragraph : xwpfParagraphs) {
        List<XWPFRun> runs = paragraph.getRuns();

        for (Map.Entry<String, String> replPair : replacements.entrySet()) {
            String find = replPair.getKey();
            String repl = replPair.getValue();
            TextSegement found = paragraph.searchText(find, new PositionInParagraph());
            if (found != null) {
                count++;//from   w  w w.j ava 2 s .  c om
                if (found.getBeginRun() == found.getEndRun()) {
                    // whole search string is in one Run
                    XWPFRun run = runs.get(found.getBeginRun());
                    String runText = run.getText(run.getTextPosition());
                    String replaced = runText.replace(find, repl);
                    run.setText(replaced, 0);
                } else {
                    // The search string spans over more than one Run
                    // Put the Strings together
                    StringBuilder b = new StringBuilder();
                    for (int runPos = found.getBeginRun(); runPos <= found.getEndRun(); runPos++) {
                        XWPFRun run = runs.get(runPos);
                        b.append(run.getText(run.getTextPosition()));
                    }
                    String connectedRuns = b.toString();
                    String replaced = connectedRuns.replace(find, repl);

                    // The first Run receives the replaced String of all connected Runs
                    XWPFRun partOne = runs.get(found.getBeginRun());
                    partOne.setText(replaced, 0);
                    // Removing the text in the other Runs.
                    for (int runPos = found.getBeginRun() + 1; runPos <= found.getEndRun(); runPos++) {
                        XWPFRun partNext = runs.get(runPos);
                        partNext.setText("", 0);
                    }
                }
            }
        }
    }
    return count;
}

From source file:org.obeonetwork.m2doc.parser.test.RunIteratorTests.java

License:Open Source License

@Test
public void testNonEmptyDoc() throws InvalidFormatException, IOException {
    FileInputStream is = new FileInputStream("templates/RunIteratorTest.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    TokenIterator iterator = new TokenIterator(document);
    XWPFRun run = iterator.next().getRun();
    assertEquals("P1Run1 ", run.getText(run.getTextPosition()));
    run = iterator.next().getRun();/*www  .  ja v a 2  s.c o  m*/
    assertEquals("P1Run2", run.getText(run.getTextPosition()));
    run = iterator.next().getRun();
    assertEquals(" P1Run3", run.getText(run.getTextPosition()));
    run = iterator.next().getRun();
    assertEquals("P2Run1 ", run.getText(run.getTextPosition()));
    run = iterator.next().getRun();
    assertEquals("P2Run2", run.getText(run.getTextPosition()));
    run = iterator.next().getRun();
    assertEquals(" ", run.getText(run.getTextPosition()));
    run = iterator.next().getRun();
    assertEquals("P2Run3", run.getText(run.getTextPosition()));
    run = iterator.next().getRun();
    assertEquals("", run.getText(run.getTextPosition()));
    assertTrue(!iterator.hasNext());
}

From source file:org.obeonetwork.m2doc.parser.test.RunProviderTest.java

License:Open Source License

@Test
public void testNonEmptyDoc() throws InvalidFormatException, IOException {
    FileInputStream is = new FileInputStream("templates/RunIteratorTest.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    TokenProvider iterator = new TokenProvider(document);
    XWPFRun run = iterator.next().getRun();
    assertEquals("P1Run1 ", run.getText(run.getTextPosition()));
    run = iterator.next().getRun();//w  w  w . j  a  va  2  s  .  c o  m
    assertEquals("P1Run2", run.getText(run.getTextPosition()));
    run = iterator.next().getRun();
    assertEquals(" P1Run3", run.getText(run.getTextPosition()));
    run = iterator.next().getRun();
    assertEquals("P2Run1 ", run.getText(run.getTextPosition()));
    run = iterator.next().getRun();
    assertEquals("P2Run2", run.getText(run.getTextPosition()));
    run = iterator.next().getRun();
    assertEquals(" ", run.getText(run.getTextPosition()));
    run = iterator.next().getRun();
    assertEquals("P2Run3", run.getText(run.getTextPosition()));
    run = iterator.next().getRun();
    assertEquals("", run.getText(run.getTextPosition()));
    assertTrue(!iterator.hasNext());
}