Guest User

XWPF add image to header/footer problem

a guest
Dec 9th, 2016
455
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package test;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import org.apache.poi.util.Units;
  7. import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
  8. import org.apache.poi.xwpf.usermodel.Document;
  9. import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
  10. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  11. import org.apache.poi.xwpf.usermodel.XWPFFooter;
  12. import org.apache.poi.xwpf.usermodel.XWPFHeader;
  13. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  14. import org.apache.poi.xwpf.usermodel.XWPFRun;
  15. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
  16. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
  17. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
  18. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
  19. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
  20.  
  21. public class PoiTest {
  22.  
  23.     private static final String IMAGE = "C:\\image.jpg";
  24.  
  25.     private static final String DEST = "C:\\test.docx";
  26.  
  27.     public static void main(String[] args) {
  28.  
  29.         try (InputStream imageStream = new FileInputStream(IMAGE);
  30.                 InputStream imageStream2 = new FileInputStream(IMAGE);
  31.                 InputStream imageStream3 = new FileInputStream(IMAGE)) {
  32.             XWPFDocument docx = new XWPFDocument();
  33.             CTBody body = docx.getDocument().getBody();
  34.             CTSectPr sectPr = body.getSectPr();
  35.             if (sectPr == null) {
  36.                 sectPr = body.addNewSectPr();
  37.             }
  38.  
  39.             XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(docx, sectPr);
  40.  
  41.             //write header content
  42.             XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
  43.             XWPFParagraph p = header.createParagraph();
  44.             XWPFRun headerRun = p.createRun();
  45.             headerRun.setText("This is header");
  46.             headerRun.addPicture(imageStream, Document.PICTURE_TYPE_JPEG, "testHeader", Units.toEMU(100), Units.toEMU(100));
  47.  
  48.             //write footer content
  49.             CTP ctpFooter = CTP.Factory.newInstance();
  50.             CTR ctrFooter = ctpFooter.addNewR();
  51.             CTText ctFooter = ctrFooter.addNewT();
  52.             String footerText = "This is footer";
  53.             ctFooter.setStringValue(footerText);
  54.             XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, docx);
  55.             XWPFParagraph[] parsFooter = new XWPFParagraph[1];
  56.             parsFooter[0] = footerParagraph;
  57.             XWPFFooter footer = policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);
  58.             XWPFParagraph fp = footer.getParagraphArray(0);
  59.             XWPFRun footerRun = fp.createRun();
  60.             footerRun.addPicture(imageStream2, Document.PICTURE_TYPE_JPEG, "testFooter", Units.toEMU(100), Units.toEMU(100));
  61.  
  62.             //write body content
  63.             XWPFParagraph bodyParagraph = docx.createParagraph();
  64.             bodyParagraph.setAlignment(ParagraphAlignment.CENTER);
  65.             XWPFRun r = bodyParagraph.createRun();
  66.             r.setBold(true);
  67.             r.setText("This is body content.");
  68.             r.addPicture(imageStream3, Document.PICTURE_TYPE_JPEG, "testBody", Units.toEMU(100), Units.toEMU(100));
  69.  
  70.             try (FileOutputStream out = new FileOutputStream(DEST)) {
  71.                 docx.write(out);
  72.             }
  73.         } catch (Exception ex) {
  74.             ex.printStackTrace();
  75.         }
  76.     }
  77. }
RAW Paste Data