SHARE
TWEET

WordInsertPicture

a guest Jun 20th, 2014 872 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  7. import org.apache.poi.xwpf.usermodel.Document;
  8. import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
  9. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  10. public class WordInsertPicture {
  11.  
  12.         public static void main(String[] args) throws IOException {
  13.                 // Create a document file
  14.                 CustomXWPFDocument document = new CustomXWPFDocument();
  15.                
  16.                 // Adding a file
  17.                 try {
  18.                         // Working addPicture Code below...
  19.                         XWPFParagraph paragraphX = document.createParagraph();
  20.                         paragraphX.setAlignment(ParagraphAlignment.CENTER);
  21.                         String blipId = paragraphX.getDocument().addPictureData(
  22.                         new FileInputStream(new File("/tmp/Simple_tux.png")),
  23.                         Document.PICTURE_TYPE_PNG);
  24.                         document.createPicture(blipId,
  25.                         document.getNextPicNameNumber(Document.PICTURE_TYPE_PNG), 200, 75);
  26.  
  27.                 } catch (InvalidFormatException e1) {
  28.                         e1.printStackTrace();
  29.                 }
  30.                
  31.                 FileOutputStream outStream = null;
  32.                
  33.                 try {
  34.                         String fileName = "/tmp/tempDoc.docx";
  35.                         outStream = new FileOutputStream(fileName);
  36.                 } catch (FileNotFoundException e) {
  37.                         e.printStackTrace();
  38.                 }
  39.                 try {
  40.                         document.write(outStream);
  41.                         outStream.close();
  42.                 } catch (FileNotFoundException e) {
  43.                         e.printStackTrace();
  44.                 } catch (IOException e) {
  45.                         e.printStackTrace();
  46.                 }
  47.         }
  48. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top