Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
- import org.apache.poi.xwpf.usermodel.Document;
- import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
- import org.apache.poi.xwpf.usermodel.XWPFParagraph;
- public class WordInsertPicture {
- public static void main(String[] args) throws IOException {
- // Create a document file
- CustomXWPFDocument document = new CustomXWPFDocument();
- // Adding a file
- try {
- // Working addPicture Code below...
- XWPFParagraph paragraphX = document.createParagraph();
- paragraphX.setAlignment(ParagraphAlignment.CENTER);
- String blipId = paragraphX.getDocument().addPictureData(
- new FileInputStream(new File("/tmp/Simple_tux.png")),
- Document.PICTURE_TYPE_PNG);
- document.createPicture(blipId,
- document.getNextPicNameNumber(Document.PICTURE_TYPE_PNG), 200, 75);
- } catch (InvalidFormatException e1) {
- e1.printStackTrace();
- }
- FileOutputStream outStream = null;
- try {
- String fileName = "/tmp/tempDoc.docx";
- outStream = new FileOutputStream(fileName);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- try {
- document.write(outStream);
- outStream.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement