Search in sources :

Example 11 with CTPicture

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();
}
Also used : CTBlipFillProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties) ByteArrayInputStream(java.io.ByteArrayInputStream) CTBlip(org.openxmlformats.schemas.drawingml.x2006.main.CTBlip) CTPicture(org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture) Test(org.junit.Test)

Aggregations

CTBlipFillProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties)5 XmlException (org.apache.xmlbeans.XmlException)4 XmlObject (org.apache.xmlbeans.XmlObject)4 CTBlip (org.openxmlformats.schemas.drawingml.x2006.main.CTBlip)4 CTPicture (org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture)4 CTPicture (org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture)4 CTNonVisualDrawingProps (org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps)3 CTPresetGeometry2D (org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D)3 CTShapeProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties)3 CTPicture (org.openxmlformats.schemas.presentationml.x2006.main.CTPicture)3 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)2 XmlCursor (org.apache.xmlbeans.XmlCursor)2 XmlAnyTypeImpl (org.apache.xmlbeans.impl.values.XmlAnyTypeImpl)2 Test (org.junit.Test)2 CTNonVisualPictureProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualPictureProperties)2 CTPoint2D (org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D)2 CTPositiveSize2D (org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D)2 CTTransform2D (org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 StringReader (java.io.StringReader)1