List of usage examples for org.apache.poi.xwpf.usermodel XWPFDocument removeBodyElement
public boolean removeBodyElement(int pos)
From source file:biz.webgate.dominoext.poi.component.kernel.DocumentProcessor.java
License:Apache License
public XWPFDocument processDocument(ITemplateSource itsCurrent, List<IDocumentBookmark> bookmarks, List<DocumentTable> tables, FacesContext context, UIDocument uiDocument) throws POIException { Logger logCurrent = LoggerFactory.getLogger(this.getClass().getCanonicalName()); InputStream is = itsCurrent.getFileStream(); XWPFDocument dxDocument = getDocument(is); itsCurrent.cleanUP();/*from ww w . j a va2s . c o m*/ if (bookmarks != null && bookmarks.size() > 0) { processBookmarks2Document(dxDocument, bookmarks); } logCurrent.finest("Start export Tables"); if (tables != null && tables.size() > 0) { for (DocumentTable tblExport : tables) { XWPFTable dxTable = EmbeddedDataSourceExportProcessor.getInstance().processExportTable(tblExport, dxDocument, context, tblExport.getVar(), tblExport.getIndex()); logCurrent.finest("exportTable created"); // logCurrent.finer("Start Processing Cells"); if (tblExport.getDocCellValues() != null && tblExport.getDocCellValues().size() > 0) { for (ICellValue iCV : tblExport.getDocCellValues()) { if (iCV instanceof DocCellValue) { DocCellValue cv = (DocCellValue) iCV; if (cv.getRowNumber() <= tblExport.getMaxRow()) { DocumentProcessor.setDocCellValue(dxTable, cv.getRowNumber(), cv.getColumnNumber(), cv.getValue(), tblExport.getMaxRow(), false); } else { logCurrent.finer("MaxValue < CellValue.getRowNumber()"); } } } } if (dxTable != null) { logCurrent.finest("Set Table Position"); if (tblExport.getTableNr() != 0) { dxDocument.setTable(tblExport.getTableNr(), dxTable); dxDocument.removeBodyElement(dxDocument.getPosOfTable(dxTable)); } } /* * if (dxTable != null) { System.out.println("Insert Table"); * dxDocument.insertTable(2, dxTable); } */ // System.out.println(dxDocument.getPosOfParagraph(p)); // logCurrent.finer("Proccess Export Cells - DONE"); } } if (uiDocument != null) { uiDocument.postGenerationProcess(context, dxDocument); } return dxDocument; }
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); }//from www. j a va2s .co 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); }/*from w w w. j a v a 2 s .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:Modelo.EscribirWord.java
public void cambiarLogo() throws FileNotFoundException, IOException, InvalidFormatException, Exception { //ASPOSE//from www . j av a 2 s . c o m // 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: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;// w w w . j a va2s . co m deleteRuns(startPosition, true); } else if (elem != endPosition.run.getParagraph()) { doc.removeBodyElement(elemIndex); elemIndex--; } else { deleteRuns(endPosition, false); return; } } }
From source file:org.obeonetwork.m2doc.generator.DocumentGenerator.java
License:Open Source License
/** * creates the destination document.// w ww. j a v a 2 s.c om * * @param inputDocumentFileName * the name of the destination document's file. * @return the created document * @throws IOException * if an I/O problem occurs. * @throws InvalidFormatException * if the generated file has a format problem. */ private XWPFDocument createDestinationDocument(String inputDocumentFileName) throws IOException, InvalidFormatException { FileInputStream is = new FileInputStream(inputDocumentFileName); OPCPackage oPackage = OPCPackage.open(is); XWPFDocument document = new XWPFDocument(oPackage); int size = document.getBodyElements().size(); for (int i = 0; i < size; i++) { document.removeBodyElement(0); } return document; }
From source file:org.obeonetwork.m2doc.generator.test.TemplateProcessorTest.java
License:Open Source License
private XWPFDocument createDestinationDocument(String inputDocumentFileName) throws IOException, InvalidFormatException { FileInputStream is = new FileInputStream(inputDocumentFileName); OPCPackage oPackage = OPCPackage.open(is); XWPFDocument document = new XWPFDocument(oPackage); int size = document.getBodyElements().size(); for (int i = 0; i < size; i++) { document.removeBodyElement(0); }//from www .j av a 2 s . c o m return document; }