List of usage examples for org.apache.poi.xwpf.usermodel XWPFParagraph removeRun
public boolean removeRun(int pos)
From source file:ch.admin.searchreplace.SearchReplaceTerms.java
License:Apache License
/** * Konsolidiert die Runs eines Paragraphen in einen. Ansonsten koennen Texte nicht sauber * ersetzt werden, bzw. werden nicht gefunden, weil ueber mehrere Runs verteilt. * @param para Paragraph//from w w w. java2 s . c om * @return Konsolidierter Run */ private static XWPFRun consolidateRuns(XWPFParagraph para) { StringBuffer text = new StringBuffer(); int count = 0; for (XWPFRun r : para.getRuns()) { text.append(r.getText(0)); count++; } for (int i = count - 1; i >= 1; i--) para.removeRun(i); if (count == 0) return (null); XWPFRun r = para.getRuns().get(0); r.setText(text.toString(), 0); return (r); }
From source file:colina.angel.controller.CreadorController.java
public void changeText(XWPFParagraph paragraph, String newText) { List<XWPFRun> runs = paragraph.getRuns(); for (int i = runs.size() - 1; i > 0; i--) { paragraph.removeRun(i); }/* w w w . ja v a 2s .c om*/ XWPFRun run = runs.get(0); run.setText(newText, 0); }
From source file:com.deepoove.poi.resolver.TemplateResolver.java
License:Apache License
/** * running string Algorithm//from w w w.jav a2 s. c o m * * @param paragraph * @return */ public static List<RunTemplate> parseRun(XWPFParagraph paragraph) { List<XWPFRun> runs = paragraph.getRuns(); if (null == runs || runs.isEmpty()) return null; String text = paragraph.getText(); logger.debug("Paragrah's text is:" + text); List<Pair<RunEdge, RunEdge>> pairs = new ArrayList<Pair<RunEdge, RunEdge>>(); List<String> tags = new ArrayList<String>(); calcTagPosInParagraph(text, pairs, tags); List<RunTemplate> rts = new ArrayList<RunTemplate>(); if (pairs.isEmpty()) return rts; RunTemplate runTemplate; calcRunPosInParagraph(runs, pairs); for (Pair<RunEdge, RunEdge> pai : pairs) { logger.debug(pai.getLeft().toString()); logger.debug(pai.getRight().toString()); } // split and merge Pair<RunEdge, RunEdge> pair2 = pairs.get(0); int length = pairs.size(); int tagIndex = length; for (int n = length - 1; n >= 0; n--) { pair2 = pairs.get(n); RunEdge left2 = pair2.getLeft(); RunEdge right2 = pair2.getRight(); int left_r = left2.getRunPos(); int right_r = right2.getRunPos(); int runEdge = left2.getRunEdge(); int runEdge2 = right2.getRunEdge(); String text1 = runs.get(left_r).getText(0); String text2 = runs.get(right_r).getText(0); if (runEdge2 + 1 >= text2.length()) { if (left_r != right_r) paragraph.removeRun(right_r); } else { String substring = text2.substring(runEdge2 + 1, text2.length()); if (left_r == right_r) { XWPFRun insertNewRun = paragraph.insertNewRun(right_r + 1); styleRun(insertNewRun, runs.get(right_r)); insertNewRun.setText(substring, 0); } else runs.get(right_r).setText(substring, 0); } for (int m = right_r - 1; m > left_r; m--) { paragraph.removeRun(m); } if (runEdge <= 0) { runs.get(left_r).setText(tags.get(--tagIndex), 0); runTemplate = parseRun(runs.get(left_r)); } else { String substring = text1.substring(0, runEdge); XWPFRun xwpfRun = runs.get(left_r); runs.get(left_r).setText(substring, 0); XWPFRun insertNewRun = paragraph.insertNewRun(left_r + 1); styleRun(insertNewRun, xwpfRun); insertNewRun.setText(tags.get(--tagIndex), 0); runTemplate = parseRun(runs.get(left_r + 1)); } if (null != runTemplate) { rts.add(runTemplate); } } return rts; }
From source file:msoffice.ReadWord.java
public static void main(String[] args) { try {/*from w w w.ja va 2 s. co m*/ FileInputStream fis = new FileInputStream("H:\\OFICIOTEMPLATE.docx"); XWPFDocument docx = new XWPFDocument(fis); List<XWPFParagraph> paragraphList = docx.getParagraphs(); int nump = paragraphList.size(); System.out.println(nump); for (int x = 0; x < paragraphList.size(); x++) { } for (XWPFParagraph paragraph : paragraphList) { String text = paragraph.getText(); XWPFRun rh = paragraph.createRun(); if (text.contains("unidad")) { text = text.replace("unidad", "CICTE/W-6.a/02.00"); paragraph.removeRun(8); rh.setText(text); } if (text.contains("receptor")) { text = text.replace("receptor", "Gral Brig Jefe del Servicio de Material de Guerra del Ejrcito"); } if (text.contains("asunto")) { text = text.replace("asunto", "Sobre articulo de MG (Armamento) y apoyo de elemento tecnico."); } if (text.contains("referencia")) { text = text.replace("referencia", "Oficio N289/CICTE del 01 julio de 2015."); } if (text.contains("cuerpo")) { text = text.replace("cuerpo", "Tengo el honor de dirigirme a Ud., para manifestarle que en relacin a la solicitud de prestamo de una (01) ametralladora BROWNING Cal .50 y la participacin del elemento tcnico Tco 2da MAM Pacheco Tejada Henry, para las pruebas del vehculo blindado OTORONGO, las cuales se han suspendido y sern reprogramadas.\n" + "Asimismo, se informar de manera oportuna la fecha de realizacin de las pruebas del vehculo blindado OTORONGO, para poder contar con artculo de MG (Armamento) y apoyo de elemento tcnico solicitado.\n" + "Hago propicia la oportunidad para expresarle a Ud. los sentimientos de mi especial consideracin y estima personal."); } System.out.println(text); docx.write(new FileOutputStream("OFICIO.docx")); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
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 va 2 s.co m * @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:service.GenerationLettres.CreerPiecesManquantes.java
/** * * @param filename - Nom du fichier modle de demande des pices manquantes. * @param idDossier - Identifiant du dossier pour lequel l est cr * @throws InvalidFormatException/* w ww .ja v a 2 s. co m*/ * @throws IOException */ public void replacePiecesManquantes(String filename, Formation formation, String sexe, String nom, String prenom, String adresse, Adresse adresseEntite, List<Justificatif> justificatifsOk) throws InvalidFormatException, IOException { List<Justificatif> lesJustificatifs = formation.getLesJustificatifs(); Date dateActuelle = new Date(); DateFormat dateForm = new SimpleDateFormat("dd MMMM yyyy", Locale.FRANCE); String date = dateForm.format(dateActuelle); String codePostal = adresseEntite.getCodePostal(); String ville = adresseEntite.getVille(); String civilite = ""; if (sexe.equals("Masculin")) civilite = "Monsieur"; if (sexe.equals("Feminin")) civilite = "Madame"; String intitule = formation.getIntitule(); if (justificatifsOk != null) { for (Justificatif just : justificatifsOk) { lesJustificatifs.remove(just); } } String newFileName = nom + prenom + " Lettre piecesManquantes.docx"; File file = new File(PATH_MODELS + "/" + filename); FileInputStream fis = new FileInputStream(file.getAbsolutePath()); XWPFDocument doc = new XWPFDocument(fis); doc.write(new FileOutputStream(PATH_TARGET + "/" + newFileName)); doc.close(); doc = new XWPFDocument(OPCPackage.open(PATH_TARGET + "/" + newFileName)); for (XWPFParagraph p : doc.getParagraphs()) { int numberOfRuns = p.getRuns().size(); StringBuilder sb = new StringBuilder(); for (XWPFRun r : p.getRuns()) { int pos = r.getTextPosition(); if (r.getText(pos) != null) { sb.append(r.getText(pos)); } } if (sb.length() > 0 && sb.toString().contains("$formation")) { for (int i = numberOfRuns - 1; i > 0; i--) { p.removeRun(i); } String text = sb.toString().replace("$formation", intitule); XWPFRun run = p.getRuns().get(0); run.setText(text, 0); System.out.println("Changement de la formation effectue"); } } for (XWPFParagraph p : doc.getParagraphs()) { int numberOfRuns = p.getRuns().size(); StringBuilder sb = new StringBuilder(); for (XWPFRun r : p.getRuns()) { int pos = r.getTextPosition(); if (r.getText(pos) != null) { sb.append(r.getText(pos)); } } if (sb.length() > 0 && sb.toString().contains("$date")) { for (int i = numberOfRuns - 1; i > 0; i--) { p.removeRun(i); } String text = sb.toString().replace("$date", date); XWPFRun run = p.getRuns().get(0); run.setText(text, 0); System.out.println("Changement de la date effectue"); } } for (XWPFParagraph p : doc.getParagraphs()) { int numberOfRuns = p.getRuns().size(); StringBuilder sb = new StringBuilder(); for (XWPFRun r : p.getRuns()) { int pos = r.getTextPosition(); if (r.getText(pos) != null) { sb.append(r.getText(pos)); } } if (sb.length() > 0 && sb.toString().contains("$civilite")) { for (int i = numberOfRuns - 1; i > 0; i--) { p.removeRun(i); } String text = sb.toString().replace("$civilite", civilite); XWPFRun run = p.getRuns().get(0); run.setText(text, 0); System.out.println("Changement de la civilite effectue"); } } for (XWPFParagraph p : doc.getParagraphs()) { int numberOfRuns = p.getRuns().size(); StringBuilder sb = new StringBuilder(); for (XWPFRun r : p.getRuns()) { int pos = r.getTextPosition(); if (r.getText(pos) != null) { sb.append(r.getText(pos)); } } if (sb.length() > 0 && sb.toString().contains("$prenom")) { for (int i = numberOfRuns - 1; i > 0; i--) { p.removeRun(i); } String text = sb.toString().replace("$prenom", prenom); XWPFRun run = p.getRuns().get(0); run.setText(text, 0); System.out.println("Changement du prenom effectue"); } } for (XWPFParagraph p : doc.getParagraphs()) { int numberOfRuns = p.getRuns().size(); StringBuilder sb = new StringBuilder(); for (XWPFRun r : p.getRuns()) { int pos = r.getTextPosition(); if (r.getText(pos) != null) { sb.append(r.getText(pos)); } } if (sb.length() > 0 && sb.toString().contains("$nom")) { for (int i = numberOfRuns - 1; i > 0; i--) { p.removeRun(i); } String text = sb.toString().replace("$nom", nom); XWPFRun run = p.getRuns().get(0); run.setText(text, 0); System.out.println("Changement du nom effectue"); } } for (XWPFParagraph p : doc.getParagraphs()) { int numberOfRuns = p.getRuns().size(); StringBuilder sb = new StringBuilder(); for (XWPFRun r : p.getRuns()) { int pos = r.getTextPosition(); if (r.getText(pos) != null) { sb.append(r.getText(pos)); } } if (sb.length() > 0 && sb.toString().contains("$adresse")) { for (int i = numberOfRuns - 1; i > 0; i--) { p.removeRun(i); } String text = sb.toString().replace("$adresse", adresse); XWPFRun run = p.getRuns().get(0); run.setText(text, 0); System.out.println("Changement de l'adresse effectue"); } } for (XWPFParagraph p : doc.getParagraphs()) { int numberOfRuns = p.getRuns().size(); StringBuilder sb = new StringBuilder(); for (XWPFRun r : p.getRuns()) { int pos = r.getTextPosition(); if (r.getText(pos) != null) { sb.append(r.getText(pos)); } } if (sb.length() > 0 && sb.toString().contains("$codePostal")) { for (int i = numberOfRuns - 1; i > 0; i--) { p.removeRun(i); } String text = sb.toString().replace("$codePostal", codePostal); XWPFRun run = p.getRuns().get(0); run.setText(text, 0); System.out.println("Changement du code postal effectue"); } } for (XWPFParagraph p : doc.getParagraphs()) { int numberOfRuns = p.getRuns().size(); StringBuilder sb = new StringBuilder(); for (XWPFRun r : p.getRuns()) { int pos = r.getTextPosition(); if (r.getText(pos) != null) { sb.append(r.getText(pos)); } } if (sb.length() > 0 && sb.toString().contains("$ville")) { for (int i = numberOfRuns - 1; i > 0; i--) { p.removeRun(i); } String text = sb.toString().replace("$ville", ville); XWPFRun run = p.getRuns().get(0); run.setText(text, 0); System.out.println("Changement de la ville effectue"); } } XWPFTable table = doc.createTable(lesJustificatifs.size(), 2); table.setCellMargins(200, 250, 0, 250); int i = 0; for (XWPFTableRow r : table.getRows()) { XWPFTableCell cell = r.getCell(0); cell.setText(lesJustificatifs.get(i).getTitre()); cell = r.getCell(1); cell.setText(lesJustificatifs.get(i).getDescription()); i++; } doc.write(new FileOutputStream(PATH_TARGET + "/temp.docx")); new File(PATH_TARGET + "/temp.docx").delete(); doc.close(); //copyTempToFile(filename); System.out.println("replaceLettrePiecesManquantes DONE"); }