List of usage examples for org.apache.poi.xwpf.usermodel XWPFRun getTextPosition
public int getTextPosition()
From source file:org.obeonetwork.m2doc.parser.test.RunProviderTest.java
License:Open Source License
@Test public void testHasElements() 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); assertTrue(iterator.hasElements(7)); XWPFRun run = iterator.next().getRun(); assertTrue(iterator.hasElements(6)); assertEquals("P1Run1 ", run.getText(run.getTextPosition())); run = iterator.next().getRun();/*w w w.j a v a 2 s . c om*/ assertTrue(iterator.hasElements(5)); assertEquals("P1Run2", run.getText(run.getTextPosition())); run = iterator.next().getRun(); assertTrue(iterator.hasElements(4)); assertEquals(" P1Run3", run.getText(run.getTextPosition())); run = iterator.next().getRun(); assertTrue(iterator.hasElements(3)); assertEquals("P2Run1 ", run.getText(run.getTextPosition())); run = iterator.next().getRun(); assertTrue(iterator.hasElements(2)); assertEquals("P2Run2", run.getText(run.getTextPosition())); run = iterator.next().getRun(); assertTrue(iterator.hasElements(1)); assertEquals(" ", run.getText(run.getTextPosition())); run = iterator.next().getRun(); assertTrue(iterator.hasElements(0)); 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 testLookAhead() 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); assertTrue(iterator.hasElements(7)); XWPFRun run; run = iterator.lookAhead(1).getRun(); assertEquals("P1Run1 ", run.getText(run.getTextPosition())); run = iterator.lookAhead(2).getRun(); assertEquals("P1Run2", run.getText(run.getTextPosition())); run = iterator.lookAhead(3).getRun(); assertEquals(" P1Run3", run.getText(run.getTextPosition())); run = iterator.lookAhead(4).getRun(); assertEquals("P2Run1 ", run.getText(run.getTextPosition())); run = iterator.lookAhead(5).getRun(); assertEquals("P2Run2", run.getText(run.getTextPosition())); run = iterator.lookAhead(6).getRun(); assertEquals(" ", run.getText(run.getTextPosition())); run = iterator.lookAhead(7).getRun(); assertEquals("P2Run3", run.getText(run.getTextPosition())); assertTrue(iterator.hasElements(7)); }
From source file:org.obeonetwork.m2doc.parser.test.RunProviderTest.java
License:Open Source License
@Test public void testNextWitLookAhead() 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); assertTrue(iterator.hasElements(7)); XWPFRun run; run = iterator.lookAhead(1).getRun(); assertEquals("P1Run1 ", run.getText(run.getTextPosition())); run = iterator.next().getRun();/*from w w w . j av a 2 s. co m*/ assertEquals("P1Run1 ", run.getText(run.getTextPosition())); run = iterator.lookAhead(1).getRun(); assertEquals("P1Run2", run.getText(run.getTextPosition())); run = iterator.lookAhead(2).getRun(); assertEquals(" P1Run3", run.getText(run.getTextPosition())); run = iterator.next().getRun(); assertEquals("P1Run2", run.getText(run.getTextPosition())); run = iterator.next().getRun(); assertEquals(" P1Run3", run.getText(run.getTextPosition())); assertTrue(iterator.hasElements(4)); }
From source file:org.obeonetwork.m2doc.tests.parser.RunIteratorTests.java
License:Open Source License
@Test public void testNonEmptyDoc() throws InvalidFormatException, IOException { try (FileInputStream is = new FileInputStream("resources/document/notEmpty/notEmpty-template.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();//from w ww . java 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.tests.parser.RunProviderTests.java
License:Open Source License
@Test public void testNonEmptyDoc() throws InvalidFormatException, IOException { try (FileInputStream is = new FileInputStream("resources/document/notEmpty/notEmpty-template.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();//from w ww . j a va2 s . c om 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.tests.parser.RunProviderTests.java
License:Open Source License
@Test public void testHasElements() throws InvalidFormatException, IOException { try (FileInputStream is = new FileInputStream("resources/document/notEmpty/notEmpty-template.docx"); OPCPackage oPackage = OPCPackage.open(is); XWPFDocument document = new XWPFDocument(oPackage);) { TokenProvider iterator = new TokenProvider(document); // CHECKSTYLE:OFF assertTrue(iterator.hasElements(7)); // CHECKSTYLE:ON XWPFRun run = iterator.next().getRun(); assertTrue(iterator.hasElements(6)); assertEquals("P1Run1 ", run.getText(run.getTextPosition())); run = iterator.next().getRun();// w w w .j a va 2 s. c o m assertTrue(iterator.hasElements(5)); assertEquals("P1Run2", run.getText(run.getTextPosition())); run = iterator.next().getRun(); assertTrue(iterator.hasElements(4)); assertEquals(" P1Run3", run.getText(run.getTextPosition())); run = iterator.next().getRun(); assertTrue(iterator.hasElements(3)); assertEquals("P2Run1 ", run.getText(run.getTextPosition())); run = iterator.next().getRun(); assertTrue(iterator.hasElements(2)); assertEquals("P2Run2", run.getText(run.getTextPosition())); run = iterator.next().getRun(); assertTrue(iterator.hasElements(1)); assertEquals(" ", run.getText(run.getTextPosition())); run = iterator.next().getRun(); assertTrue(iterator.hasElements(0)); assertEquals("P2Run3", run.getText(run.getTextPosition())); run = iterator.next().getRun(); assertEquals("", run.getText(run.getTextPosition())); assertTrue(!iterator.hasNext()); } }
From source file:org.obeonetwork.m2doc.tests.parser.RunProviderTests.java
License:Open Source License
@Test public void testLookAhead() throws InvalidFormatException, IOException { try (FileInputStream is = new FileInputStream("resources/document/notEmpty/notEmpty-template.docx"); OPCPackage oPackage = OPCPackage.open(is); XWPFDocument document = new XWPFDocument(oPackage);) { TokenProvider iterator = new TokenProvider(document); // CHECKSTYLE:OFF assertTrue(iterator.hasElements(7)); XWPFRun run; run = iterator.lookAhead(1).getRun(); assertEquals("P1Run1 ", run.getText(run.getTextPosition())); run = iterator.lookAhead(2).getRun(); assertEquals("P1Run2", run.getText(run.getTextPosition())); run = iterator.lookAhead(3).getRun(); assertEquals(" P1Run3", run.getText(run.getTextPosition())); run = iterator.lookAhead(4).getRun(); assertEquals("P2Run1 ", run.getText(run.getTextPosition())); run = iterator.lookAhead(5).getRun(); assertEquals("P2Run2", run.getText(run.getTextPosition())); run = iterator.lookAhead(6).getRun(); assertEquals(" ", run.getText(run.getTextPosition())); run = iterator.lookAhead(7).getRun(); assertEquals("P2Run3", run.getText(run.getTextPosition())); assertTrue(iterator.hasElements(7)); // CHECKSTYLE:ON }//from w w w. j a v a 2 s . co m }
From source file:org.obeonetwork.m2doc.tests.parser.RunProviderTests.java
License:Open Source License
@Test public void testNextWitLookAhead() throws InvalidFormatException, IOException { try (FileInputStream is = new FileInputStream("resources/document/notEmpty/notEmpty-template.docx"); OPCPackage oPackage = OPCPackage.open(is); XWPFDocument document = new XWPFDocument(oPackage);) { TokenProvider iterator = new TokenProvider(document); // CHECKSTYLE:OFF assertTrue(iterator.hasElements(7)); // CHECKSTYLE:ON XWPFRun run; run = iterator.lookAhead(1).getRun(); assertEquals("P1Run1 ", run.getText(run.getTextPosition())); run = iterator.next().getRun();//from ww w . j av a 2 s .c o m assertEquals("P1Run1 ", run.getText(run.getTextPosition())); run = iterator.lookAhead(1).getRun(); assertEquals("P1Run2", run.getText(run.getTextPosition())); run = iterator.lookAhead(2).getRun(); assertEquals(" P1Run3", run.getText(run.getTextPosition())); run = iterator.next().getRun(); assertEquals("P1Run2", run.getText(run.getTextPosition())); run = iterator.next().getRun(); assertEquals(" P1Run3", run.getText(run.getTextPosition())); assertTrue(iterator.hasElements(4)); } }
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 w w . j av a 2 s. c o 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"); }