List of usage examples for org.apache.poi.xwpf.usermodel XWPFDocument getBodyElements
@Override
public List<IBodyElement> getBodyElements()
From source file:com.example.office.DOCDocumentParse.java
@SuppressWarnings("unused") private File doc2docxOld(File docFile) { String docxFilePath = docFile.getPath() + "x"; File docxFile = new File(docxFilePath); if (!docxFile.exists()) { XWPFDocument document = null; try (InputStream ins = new FileInputStream(docxFile); OutputStream out = new FileOutputStream(docxFile);) { Document doc = new Document(docFile.getPath()); doc.save(docxFile.getPath()); document = new XWPFDocument(ins); // document.removeBodyElement(0) List<IBodyElement> elements = document.getBodyElements(); IBodyElement element = elements.get(elements.size() - 1); if (StringUtils.equals(BodyElementType.PARAGRAPH.name(), element.getElementType().name())) { XWPFParagraph xp = ((XWPFParagraph) element); String text = xp.getText(); if (StringUtils.isNotBlank(text)) { if (text.contains("Evaluation") && text.contains("Aspose")) { document.removeBodyElement(elements.size() - 1); }/* w w w . ja v a 2s . c o m*/ } } IBodyElement element0 = elements.get(0); if (StringUtils.equals(BodyElementType.PARAGRAPH.name(), element0.getElementType().name())) { XWPFParagraph xp = ((XWPFParagraph) element0); String text = xp.getText(); if (StringUtils.isNotBlank(text)) { if (text.contains("Evaluation") && text.contains("Aspose")) { document.removeBodyElement(0); } } } document.write(out); } catch (Exception e) { LogUtils.writeWarnExceptionLog(log, e); } finally { try { if (document != null) document.close(); } catch (IOException e) { LogUtils.writeDebugExceptionLog(log, e); } } } return docxFile; }
From source file:com.example.office.DOCDocumentParse.java
private void deleteAsposeInfo(File docxFile) { XWPFDocument document = null; try (InputStream ins = new FileInputStream(docxFile); OutputStream out = new FileOutputStream(docxFile);) { document = new XWPFDocument(ins); List<IBodyElement> elements = document.getBodyElements(); IBodyElement element = elements.get(elements.size() - 1); if (StringUtils.equals(BodyElementType.PARAGRAPH.name(), element.getElementType().name())) { XWPFParagraph xp = ((XWPFParagraph) element); String text = xp.getText(); if (StringUtils.isNotBlank(text)) { if (text.contains("Evaluation") && text.contains("Aspose")) { document.removeBodyElement(elements.size() - 1); }/* ww w . j a va2s .c o m*/ } } IBodyElement element0 = elements.get(0); if (StringUtils.equals(BodyElementType.PARAGRAPH.name(), element0.getElementType().name())) { XWPFParagraph xp = ((XWPFParagraph) element0); String text = xp.getText(); if (StringUtils.isNotBlank(text)) { if (text.contains("Evaluation") && text.contains("Aspose")) { document.removeBodyElement(0); } } } document.write(out); } catch (Exception e) { LogUtils.writeWarnExceptionLog(log, e); } finally { if (document != null) { try { document.close(); } catch (IOException e) { LogUtils.writeDebugExceptionLog(log, e); } } } }
From source file:com.siemens.sw360.licenseinfo.outputGenerators.DocxUtils.java
License:Open Source License
public static void replaceText(XWPFDocument document, String placeHolder, String replaceText) { for (XWPFHeader header : document.getHeaderList()) replaceAllBodyElements(header.getBodyElements(), placeHolder, replaceText); replaceAllBodyElements(document.getBodyElements(), placeHolder, replaceText); }
From source file:fr.opensagres.poi.xwpf.converter.core.styles.A.java
License:Open Source License
@Test public void testParagraphStyles() throws Exception { // 1) Load docx with Apache POI XWPFDocument document = new XWPFDocument(Data.class.getResourceAsStream("DocxStructures.docx")); // Create styles engine XWPFStylesDocument stylesDocument = new XWPFStylesDocument(document); // Loop for each paragraph List<IBodyElement> elements = document.getBodyElements(); for (IBodyElement element : elements) { if (element.getElementType() == BodyElementType.PARAGRAPH) { testParagraph((XWPFParagraph) element, stylesDocument); }// w ww .j a v a 2 s .co m } }
From source file:fr.opensagres.poi.xwpf.converter.core.styles.FontStylesBasedOnTestCase.java
License:Open Source License
@Test public void testParagraphStyles() throws Exception { // 1) Load docx with Apache POI XWPFDocument document = new XWPFDocument(Data.class.getResourceAsStream("TestFontStylesBasedOn.docx")); // Create styles engine XWPFStylesDocument stylesDocument = new XWPFStylesDocument(document); // Loop for each paragraph List<IBodyElement> elements = document.getBodyElements(); for (IBodyElement element : elements) { if (element.getElementType() == BodyElementType.PARAGRAPH) { testParagraph((XWPFParagraph) element, stylesDocument); }/*from w w w . j a v a 2 s. c o m*/ } }
From source file:fr.opensagres.poi.xwpf.converter.core.styles.run.FontSizeDocDefaultsTestCase.java
License:Open Source License
private void internalTest(Float size, String docName) throws Exception { XWPFDocument document = new XWPFDocument(Data.class.getResourceAsStream(docName)); XWPFStylesDocument stylesDocument = new XWPFStylesDocument(document); List<IBodyElement> elements = document.getBodyElements(); boolean ran = false; for (IBodyElement element : elements) { if (element.getElementType() == BodyElementType.PARAGRAPH) { for (XWPFRun docxRun : ((XWPFParagraph) element).getRuns()) { Object sizeFromStyle = stylesDocument.getFontSize(docxRun); ran = true;//w ww . j ava2 s .co m assertEquals(sizeFromStyle, size); } } } assertTrue(ran); }
From source file:fr.opensagres.poi.xwpf.converter.core.styles.TableCellVerticalAlignmentTestCase.java
License:Open Source License
@Test public void testParagraphStyles() throws Exception { // 1) Load docx with Apache POI XWPFDocument document = new XWPFDocument(Data.class.getResourceAsStream("TableCellVerticalAlignment.docx")); // Create styles engine XWPFStylesDocument stylesDocument = new XWPFStylesDocument(document); // Loop for each paragraph List<IBodyElement> elements = document.getBodyElements(); for (IBodyElement element : elements) { if (element.getElementType() == BodyElementType.TABLE) { testTable((XWPFTable) element, stylesDocument); }/* w w w .jav a2s . co m*/ } }
From source file:Modelo.EscribirWord.java
public void cambiarLogo() throws FileNotFoundException, IOException, InvalidFormatException, Exception { //ASPOSE//ww w.java 2 s . com // Document doc = new Document("HojaInventarioObjeto.doc"); //// doc.save("Out.pdf"); // HWPFDocument doc = new HWPFDocument(new FileInputStream("HojaInventarioTemplate.doc")); XWPFDocument document = new XWPFDocument(new FileInputStream("HojaInventarioTemplate.docx")); String imageOldName = "logo"; try { // LOG.info("replaceImage: old=" + "logo" + ", new=" + "imagePathNew"); String imagePathNew = "PlayStation_1_Logo.png"; int newImageWidth = 1; int newImageHeight = 2; int imageParagraphPos = -1; XWPFParagraph imageParagraph = null; List<IBodyElement> documentElements = document.getBodyElements(); for (IBodyElement documentElement : documentElements) { imageParagraphPos++; if (documentElement instanceof XWPFParagraph) { imageParagraph = (XWPFParagraph) documentElement; if (imageParagraph != null && imageParagraph.getCTP() != null && imageParagraph.getCTP().toString().trim().indexOf(imageOldName) != -1) { break; } } } if (imageParagraph == null) { throw new Exception("Unable to replace image data due to the exception:\n" + "'" + imageOldName + "' not found in in document."); } ParagraphAlignment oldImageAlignment = imageParagraph.getAlignment(); // remove old image document.removeBodyElement(imageParagraphPos - 1); // now add new image // BELOW LINE WILL CREATE AN IMAGE // PARAGRAPH AT THE END OF THE DOCUMENT. // REMOVE THIS IMAGE PARAGRAPH AFTER // SETTING THE NEW IMAGE AT THE OLD IMAGE POSITION XWPFParagraph newImageParagraph = document.createParagraph(); XWPFRun newImageRun = newImageParagraph.createRun(); //newImageRun.setText(newImageText); newImageParagraph.setAlignment(oldImageAlignment); try (FileInputStream is = new FileInputStream(imagePathNew)) { newImageRun.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, imagePathNew, Units.toEMU(newImageWidth), Units.toEMU(newImageHeight)); } // set new image at the old image position document.setParagraph(newImageParagraph, imageParagraphPos); // NOW REMOVE REDUNDANT IMAGE FORM THE END OF DOCUMENT document.removeBodyElement(document.getBodyElements().size() - 1); // return document; } catch (Exception e) { throw new Exception("Unable to replace image '" + imageOldName + "' due to the exception:\n" + e); } finally { // cleanup code } }
From source file:offishell.msoffice.WordTestHelper.java
License:MIT License
/** * <p>//from w w w .j a v a 2 s. c om * Assertion helper. * </p> * * @param word * @param expectedWord */ public default void checkBody(Word word, Word expectedWord) { assertin(word, expectedWord, (w, value) -> { XWPFDocument document = w.docment(); List<IBodyElement> elements = document.getBodyElements(); assert elements != null; assert value.of(elements.size()); for (IBodyElement element : elements) { if (element instanceof XWPFParagraph) { assert value.of((XWPFParagraph) element, this::checkParagraph); } } }); }
From source file:org.articleEditor.insertContent.DocumentUpdater1.java
License:Apache License
private void deleteInBetween(DocumentPosition startPosition, DocumentPosition endPosition) { XWPFDocument doc = startPosition.run.getParagraph().getDocument(); boolean deletePart = false; for (int elemIndex = 0; elemIndex < doc.getBodyElements().size(); elemIndex++) { IBodyElement elem = doc.getBodyElements().get(elemIndex); if (!deletePart && startPosition.run.getParagraph() != elem) { // Skip } else if (!deletePart) { deletePart = true;/*from w ww . jav a 2 s.c o m*/ deleteRuns(startPosition, true); } else if (elem != endPosition.run.getParagraph()) { doc.removeBodyElement(elemIndex); elemIndex--; } else { deleteRuns(endPosition, false); return; } } }