List of usage examples for org.apache.poi.xwpf.usermodel XWPFRun addPicture
public XWPFPicture addPicture(InputStream pictureData, int pictureType, String filename, int width, int height) throws InvalidFormatException, IOException
From source file:Modelo.EscribirWord.java
public void cambiarLogo() throws FileNotFoundException, IOException, InvalidFormatException, Exception { //ASPOSE/*from w w w .jav 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.obeonetwork.m2doc.generator.M2DocEvaluator.java
License:Open Source License
/** * Inserts the given {@link MImage}.//from w ww.j a v a 2 s. c o m * * @param run * the {@link XWPFRun} to insert to * @param image * the {@link MImage} to insert */ private void insertMImage(XWPFRun run, MImage image) { try { int heigth = Units.toEMU(image.getHeight()); int width = Units.toEMU(image.getWidth()); try (InputStream imageStream = image.getInputStream()) { run.addPicture(imageStream, image.getType().getPoiType(), image.getURI().toString(), width, heigth); } } catch (InvalidFormatException e) { insertMessage(currentGeneratedParagraph, ValidationMessageLevel.ERROR, String.format(PICTURE_INVALID_FORMAT, image.getURI().toString(), e.getMessage())); } catch (IOException e) { insertMessage(currentGeneratedParagraph, ValidationMessageLevel.ERROR, String.format(AN_I_O_PROBLEM_OCCURED_WHILE_READING, image.getURI().toString(), e.getMessage())); } }
From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java
License:Open Source License
@Override public IConstruct caseImage(Image image) { XWPFRun imageRun = insertRun(image.getStyleRun()); imageRun.setText(""); imageRun.getCTR().getInstrTextList().clear(); if (image.getFileName() == null) { insertQuerySyntaxMessages(image, ""); } else {/* w ww .j a v a2 s.co m*/ URI imageURI = URI.createFileURI(image.getFileName()); imageURI = imageURI.resolve(image.eResource().getURI()); try { int heigth = Units.toEMU(image.getHeight()); int width = Units.toEMU(image.getWidth()); try (InputStream imageStream = uriConverter.createInputStream(imageURI)) { imageRun.addPicture(imageStream, PictureType.toType(imageURI).getPoiType(), image.getFileName(), width, heigth); } } catch (InvalidFormatException e) { insertMessage(currentGeneratedParagraph, ValidationMessageLevel.ERROR, String.format(PICTURE_INVALID_FORMAT, imageURI.toString(), e.getMessage())); } catch (IOException e) { insertMessage(currentGeneratedParagraph, ValidationMessageLevel.ERROR, String.format(AN_I_O_PROBLEM_OCCURED_WHILE_READING, imageURI.toString(), e.getMessage())); } } return image; }
From source file:org.obeonetwork.m2doc.generator.TemplateProcessor.java
License:Open Source License
@Override public AbstractConstruct caseImage(Image object) { XWPFRun imageRun = insertRun(object.getStyleRun()); imageRun.setText(""); imageRun.getCTR().getInstrTextList().clear(); String filePath;//from w w w . j av a 2 s . c o m if ("".equals(this.rootProjectPath) || this.rootProjectPath == null) { filePath = object.getFileName(); } else { filePath = this.rootProjectPath + "/" + object.getFileName(); } try { int heigth = Units.toEMU(object.getHeight()); int width = Units.toEMU(object.getWidth()); imageRun.addPicture(new FileInputStream(filePath), getPictureType(object.getFileName()), object.getFileName(), width, heigth); } catch (InvalidFormatException e) { imageRun.setText("Picture in " + object.getFileName() + " has an invalid format."); imageRun.setBold(true); imageRun.setColor(ERROR_COLOR); } catch (FileNotFoundException e) { imageRun.setText("File " + filePath + " cannot be found."); imageRun.setBold(true); imageRun.setColor(ERROR_COLOR); } catch (IOException e) { imageRun.setText("An I/O Problem occured while reading file "); imageRun.setBold(true); imageRun.setColor(ERROR_COLOR); } return super.caseImage(object); }
From source file:Utils.FileHandler.java
private void fullWidthPicture(XWPFDocument doc, File imgFile) throws InvalidFormatException, IOException { XWPFParagraph p2 = doc.createParagraph(); p2.setAlignment(ParagraphAlignment.LEFT); XWPFRun imageRun = p2.createRun(); imageRun.setTextPosition(10);// w w w . jav a 2 s . c o m imageRun.setText(" "); imageRun.setFontSize(1); imageRun.addPicture(new FileInputStream(imgFile), XWPFDocument.PICTURE_TYPE_PNG, "image", Units.toEMU(469), Units.toEMU(274)); p2.setBorderBottom(Borders.SINGLE); p2.setBorderTop(Borders.SINGLE); p2.setBorderRight(Borders.SINGLE); p2.setBorderLeft(Borders.SINGLE); }