List of usage examples for org.apache.poi.xwpf.usermodel XWPFParagraph getRuns
public List<XWPFRun> getRuns()
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 w ww.ja v a2s.c o m*/ for (int i = 1; i < split.length; i++) { run.addBreak(); run.setText(split[i]); } } } }
From source file:org.joeffice.wordprocessor.DocumentTreeTopComponent.java
License:Apache License
public void buildParagraph(DefaultMutableTreeNode root, XWPFParagraph paragraph) { for (XWPFRun run : paragraph.getRuns()) { String nodeText = paragraph.getClass().getSimpleName() + ":" + currentOffset; DefaultMutableTreeNode node = new DefaultMutableTreeNode(nodeText); root.add(node);//from w w w.j a v a2 s .co m buildRun(node, run); } }
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 av a 2 s. com 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.generator.M2DocEvaluator.java
License:Open Source License
/** * Creates a new paragraph and replaces the currentParagrap variable. * // w w w. j a v a 2s.com * @param srcParagraph * the origin paragraph to copy the style from. */ private void createNewParagraph(XWPFParagraph srcParagraph) { // create a new paragraph. XWPFParagraph newParagraph; if (generatedDocument instanceof XWPFTableCell) { XWPFTableCell cell = (XWPFTableCell) generatedDocument; newParagraph = cell.addParagraph(); } else if (generatedDocument instanceof XWPFDocument) { newParagraph = ((XWPFDocument) generatedDocument).createParagraph(); } else if (generatedDocument instanceof XWPFHeaderFooter) { newParagraph = ((XWPFHeaderFooter) generatedDocument).createParagraph(); } else { throw new UnsupportedOperationException("unkown IBody type :" + generatedDocument.getClass()); } CTP ctp = (CTP) srcParagraph.getCTP().copy(); ctp.getRList().clear(); ctp.getFldSimpleList().clear(); ctp.getHyperlinkList().clear(); newParagraph.getCTP().set(ctp); int runNb = newParagraph.getRuns().size(); for (int i = 0; i < runNb; i++) { newParagraph.removeRun(i); } currentTemplateParagraph = srcParagraph; currentGeneratedParagraph = newParagraph; }
From source file:org.obeonetwork.m2doc.generator.TemplateValidationGenerator.java
License:Open Source License
/** * Inserts the given {@link TemplateValidationMessage} in a run next to the one of the given construct subject to the message. * /*www . j av a 2 s. com*/ * @param message * the {@link TemplateValidationMessage} to insert * the error message to insert in a run * @param offset * the offset in number of {@link XWPFRun} to insert after {@link TemplateValidationMessage#getLocation() message location} * @return the number of inserted {@link XWPFRun} */ private int addRunError(TemplateValidationMessage message, int offset) { int res = 0; if (message.getLocation().getParent() instanceof XWPFParagraph) { XWPFParagraph paragraph = (XWPFParagraph) message.getLocation().getParent(); final int basePosition = paragraph.getRuns().indexOf(message.getLocation()) + offset; // separation run. res++; final String color = M2DocUtils.getColor(message.getLevel()); XWPFRun newBlankRun = paragraph.insertNewRun(basePosition + res); newBlankRun.setText(BLANK_SEPARATOR); res++; final XWPFRun separationRun = paragraph.insertNewRun(basePosition + res); separationRun.setText(LOCATION_SEPARATOR); separationRun.setColor(color); separationRun.setFontSize(ERROR_MESSAGE_FONT_SIZE); final CTHighlight separationHighlight = separationRun.getCTR().getRPr().addNewHighlight(); separationHighlight.setVal(STHighlightColor.LIGHT_GRAY); res++; final XWPFRun messageRun = paragraph.insertNewRun(basePosition + res); messageRun.setText(message.getMessage()); messageRun.setColor(color); messageRun.setFontSize(ERROR_MESSAGE_FONT_SIZE); final CTHighlight messageHighlight = messageRun.getCTR().getRPr().addNewHighlight(); messageHighlight.setVal(STHighlightColor.LIGHT_GRAY); } return res; }
From source file:org.obeonetwork.m2doc.generator.test.TableClientProcessorTest.java
License:Open Source License
protected void checkParagraph(XWPFParagraph paragraph, String expectedTitle) { assertEquals(expectedTitle, paragraph.getText()); assertEquals(0, paragraph.getSpacingAfter()); List<XWPFRun> runs = paragraph.getRuns(); assertEquals(1, runs.size());/* ww w . java2 s .c o m*/ }
From source file:org.obeonetwork.m2doc.generator.test.TableClientProcessorWithStyleTest.java
License:Open Source License
@Test @Override//from w w w . j a va2s . c o m public void testWithoutAnyParameter() throws ProviderException { super.testWithoutAnyParameter(); // Check styles List<XWPFTable> tables = doc.getTables(); XWPFTable table = tables.get(0); // First Row (header) XWPFTableRow row = table.getRow(0); // Header => no style XWPFTableCell cell = row.getCell(1); XWPFParagraph cellParagraph = cell.getParagraphs().get(0); XWPFRun cellRun = cellParagraph.getRuns().get(0); assertNull(cell.getColor()); assertNull(cellRun.getColor()); assertFalse(cellRun.isBold()); assertFalse(cellRun.isItalic()); assertFalse(cellRun.isStrikeThrough()); assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline()); // Header => no style cell = row.getCell(2); cellParagraph = cell.getParagraphs().get(0); cellRun = cellParagraph.getRuns().get(0); assertNull(cell.getColor()); assertNull(cellRun.getColor()); assertFalse(cellRun.isBold()); assertFalse(cellRun.isItalic()); assertFalse(cellRun.isStrikeThrough()); assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline()); // First row row = table.getRow(1); // first row, row header => no style cell = row.getCell(0); cellParagraph = cell.getParagraphs().get(0); cellRun = cellParagraph.getRuns().get(0); assertNull(cell.getColor()); assertNull(cellRun.getColor()); assertFalse(cellRun.isBold()); assertFalse(cellRun.isItalic()); assertFalse(cellRun.isStrikeThrough()); assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline()); // first row, first cell => cell style cell = row.getCell(1); cellParagraph = cell.getParagraphs().get(0); cellRun = cellParagraph.getRuns().get(0); assertEquals(CELL_BG_COLOR, cell.getColor().toUpperCase()); assertEquals(CELL_FG_COLOR, cellRun.getColor().toUpperCase()); assertEquals(CELL_FONT_SIZE, cellRun.getFontSize()); assertTrue(cellRun.isBold()); assertFalse(cellRun.isItalic()); assertFalse(cellRun.isStrikeThrough()); assertEquals(UnderlinePatterns.SINGLE, cellRun.getUnderline()); // first row, second cell => row style cell = row.getCell(2); cellParagraph = cell.getParagraphs().get(0); assertEquals(ROW_BG_COLOR, cell.getColor().toUpperCase()); assertTrue(cellParagraph.getRuns().isEmpty()); // Second row row = table.getRow(2); // second row, row header => no style cell = row.getCell(0); cellParagraph = cell.getParagraphs().get(0); cellRun = cellParagraph.getRuns().get(0); assertNull(cell.getColor()); assertNull(cellRun.getColor()); assertFalse(cellRun.isBold()); assertFalse(cellRun.isItalic()); assertFalse(cellRun.isStrikeThrough()); assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline()); // second row, first cell => column style cell = row.getCell(1); cellParagraph = cell.getParagraphs().get(0); assertEquals(COL_BG_COLOR, cell.getColor().toUpperCase()); assertTrue(cellParagraph.getRuns().isEmpty()); // second row, second cell => cell style cell = row.getCell(2); cellParagraph = cell.getParagraphs().get(0); cellRun = cellParagraph.getRuns().get(0); assertEquals(CELL_BG_COLOR, cell.getColor().toUpperCase()); assertEquals(CELL_FG_COLOR, cellRun.getColor().toUpperCase()); assertEquals(CELL_FONT_SIZE, cellRun.getFontSize()); assertTrue(cellRun.isBold()); assertFalse(cellRun.isItalic()); assertFalse(cellRun.isStrikeThrough()); assertEquals(UnderlinePatterns.SINGLE, cellRun.getUnderline()); }
From source file:org.obeonetwork.m2doc.generator.test.TemplateProcessorTest.java
License:Open Source License
@Test public void testVarRefStyledProcessing() throws InvalidFormatException, IOException, DocumentParserException { FileInputStream is = new FileInputStream("templates/testVarStyle.docx"); OPCPackage oPackage = OPCPackage.open(is); XWPFDocument document = new XWPFDocument(oPackage); BodyParser parser = new BodyParser(document, env); Template template = parser.parseTemplate(); Map<String, Object> definitions = new HashMap<String, Object>(); definitions.put("x", "valueofx"); XWPFDocument destinationDoc = createDestinationDocument("templates/testVarStyle.docx"); TemplateProcessor processor = new TemplateProcessor(definitions, "", env, destinationDoc); processor.doSwitch(template);// ww w . ja v a 2s .co m assertEquals("Template de test pour les balises de rfrence une variable\u00a0: valueofx", destinationDoc.getParagraphs().get(0).getText()); XWPFParagraph paragraph = destinationDoc.getParagraphs().get(0); XWPFRun run = paragraph.getRuns().get(paragraph.getRuns().size() - 1); assertEquals("E36C0A", run.getColor()); assertNotNull(run.getCTR().getRPr().getI()); assertNotNull(run.getCTR().getRPr().getB()); }
From source file:org.obeonetwork.m2doc.generator.test.TemplateProcessorTest.java
License:Open Source License
@Test public void testVarQueryStyledProcessing() throws InvalidFormatException, IOException, DocumentParserException { FileInputStream is = new FileInputStream("templates/testVarStyle.docx"); OPCPackage oPackage = OPCPackage.open(is); XWPFDocument document = new XWPFDocument(oPackage); BodyParser parser = new BodyParser(document, env); Template template = parser.parseTemplate(); Map<String, Object> definitions = new HashMap<String, Object>(); definitions.put("x", "valueofx"); XWPFDocument destinationDoc = createDestinationDocument("templates/testVarStyle.docx"); TemplateProcessor processor = new TemplateProcessor(definitions, "", env, destinationDoc); processor.doSwitch(template);/*w w w . j a v a2s . co m*/ assertEquals("Template de test pour les balises de rfrence une variable\u00a0: valueofx", destinationDoc.getParagraphs().get(0).getText()); XWPFParagraph paragraph = destinationDoc.getParagraphs().get(0); XWPFRun run = paragraph.getRuns().get(paragraph.getRuns().size() - 1); assertEquals("E36C0A", run.getColor()); assertNotNull(run.getCTR().getRPr().getI()); assertNotNull(run.getCTR().getRPr().getB()); }
From source file:org.obeonetwork.m2doc.generator.UserContentRawCopy.java
License:Open Source License
/** * Collect Relation Id in paragraph.//w w w .jav a2 s . c om * * @param paragraph * paragraph * @param outputDoc * outputDoc * @throws InvalidFormatException * InvalidFormatException */ private void collectRelationId(XWPFParagraph paragraph, XWPFDocument outputDoc) throws InvalidFormatException { for (XWPFRun run : paragraph.getRuns()) { createPictures(run, outputDoc); } }