use of org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture in project poi by apache.
the class TestXWPFRun method testAddPictureInHeader.
/**
* Bugzilla #58237 - Unable to add image to word document header
*/
@Test
public void testAddPictureInHeader() throws IOException, InvalidFormatException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx");
XWPFHeader hdr = doc.createHeader(HeaderFooterType.DEFAULT);
XWPFParagraph p = hdr.createParagraph();
XWPFRun r = p.createRun();
assertEquals(0, hdr.getAllPictures().size());
assertEquals(0, r.getEmbeddedPictures().size());
r.addPicture(new ByteArrayInputStream(new byte[0]), Document.PICTURE_TYPE_JPEG, "test.jpg", 21, 32);
assertEquals(1, hdr.getAllPictures().size());
assertEquals(1, r.getEmbeddedPictures().size());
XWPFPicture pic = r.getEmbeddedPictures().get(0);
CTPicture ctPic = pic.getCTPicture();
CTBlipFillProperties ctBlipFill = ctPic.getBlipFill();
assertNotNull(ctBlipFill);
CTBlip ctBlip = ctBlipFill.getBlip();
assertNotNull(ctBlip);
assertEquals("rId1", ctBlip.getEmbed());
XWPFDocument docBack = XWPFTestDataSamples.writeOutAndReadBack(doc);
XWPFHeader hdrBack = docBack.getHeaderArray(0);
XWPFParagraph pBack = hdrBack.getParagraphArray(0);
XWPFRun rBack = pBack.getRuns().get(0);
assertEquals(1, hdrBack.getAllPictures().size());
assertEquals(1, rBack.getEmbeddedPictures().size());
docBack.close();
doc.close();
}
Aggregations