List of usage examples for org.apache.poi.xwpf.usermodel XWPFDocument close
@Override public void close() throws IOException
Once this has been called, no further operations, updates or reads should be performed on the document.
From source file:org.knime.ext.textprocessing.nodes.source.parser.word.WordDocumentParser.java
License:Open Source License
private Document parseInternal(final InputStream is) throws Exception { m_currentDoc = new DocumentBuilder(m_tokenizerName); m_currentDoc.setDocumentFile(new File(m_docPath)); m_currentDoc.setDocumentType(m_type); m_currentDoc.addDocumentCategory(m_category); m_currentDoc.addDocumentSource(m_source); POIFSFileSystem poifs = null;/*from w w w . ja v a 2 s .c o m*/ HWPFDocument hdoc = null; XWPFDocument hdoc2 = null; WordExtractor extractor = null; try { // doc files if (m_docPath.endsWith(".doc")) { // copy content of input stream into byte array since content have to be red twice unfortunately. final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final byte[] buf = new byte[1024]; int i = 0; while ((i = is.read(buf)) >= 0) { baos.write(buf, 0, i); } final byte[] content = baos.toByteArray(); // open stream with copied content to read text InputStream copiedInput = new ByteArrayInputStream(content); hdoc = new HWPFDocument(copiedInput); extractor = new WordExtractor(hdoc); for (String p : extractor.getParagraphText()) { p = p.trim(); if (!onlyWhitepscaes(p)) { m_currentDoc.addParagraph(p); } } // open stream again with copied content to read meta info copiedInput = new ByteArrayInputStream(content); poifs = new POIFSFileSystem(copiedInput); final DirectoryEntry dir = poifs.getRoot(); final DocumentEntry siEntry = (DocumentEntry) dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME); final PropertySet ps = new PropertySet(new DocumentInputStream(siEntry)); final SummaryInformation si = new SummaryInformation(ps); setAuthor(si.getAuthor()); setPublicationDate(si.getCreateDateTime()); // docx files } else if (m_docPath.endsWith(".docx") || m_docPath.endsWith(".docm")) { hdoc2 = new XWPFDocument(is); final List<XWPFParagraph> paragraphs = hdoc2.getParagraphs(); for (final XWPFParagraph paragraph : paragraphs) { final String text = paragraph.getText(); if (!onlyWhitepscaes(text)) { m_currentDoc.addParagraph(text); } } setAuthor(hdoc2.getProperties().getCoreProperties().getCreator()); setPublicationDate(hdoc2.getProperties().getCoreProperties().getCreated()); } m_currentDoc.createNewSection(SectionAnnotation.CHAPTER); // find title String title = null; if (m_filenameAsTitle) { title = m_docPath.trim(); } else { final List<Section> sections = m_currentDoc.getSections(); if (sections.size() > 0) { try { title = sections.get(0).getParagraphs().get(0).getSentences().get(0).getText().trim(); } catch (IndexOutOfBoundsException e) { LOGGER.debug("Parsed word document " + m_docPath + " is empty."); title = ""; } } } if (!checkTitle(title)) { title = m_docPath.toString(); } m_currentDoc.addTitle(title); return m_currentDoc.createDocument(); } finally { is.close(); if (poifs != null) { poifs.close(); } if (hdoc != null) { hdoc.close(); } if (hdoc2 != null) { hdoc2.close(); } if (extractor != null) { extractor.close(); } } }
From source file:org.obeonetwork.m2doc.generator.test.VariousTest.java
License:Open Source License
@Test public void testStaticFragmentWithFieldProcessing() throws InvalidFormatException, IOException, DocumentParserException, DocumentGenerationException { FileInputStream is = new FileInputStream("templates/test2.docx"); OPCPackage oPackage = OPCPackage.open(is); XWPFDocument document = new XWPFDocument(oPackage); // insert some static content. document.createParagraph().createRun().setText("static part that will not contain any link"); CTBookmark bookmark = document.getDocument().getBody().addNewBookmarkStart(); bookmark.setName("bookmark1"); bookmark.setId(new BigInteger("66")); document.createParagraph().createRun().setText("bookmarked part"); CTMarkupRange range = document.getDocument().getBody().addNewBookmarkEnd(); range.setId(new BigInteger("66")); document.createParagraph().createRun().setText("another static part that will not contain any link"); // save the document in another file FileOutputStream fos = new FileOutputStream("results/bookmarkTest.docx"); document.write(fos);// w ww. ja v a2s. c o m document.close(); fos.close(); }
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/*from w ww . j a va 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"); }