Search in sources :

Example 6 with CTBlip

use of org.openxmlformats.schemas.drawingml.x2006.main.CTBlip 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

CTBlip (org.openxmlformats.schemas.drawingml.x2006.main.CTBlip)6 CTBlipFillProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties)3 CTNonVisualDrawingProps (org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps)2 CTPresetGeometry2D (org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D)2 CTShapeProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties)2 CTPicture (org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture)2 CTPicture (org.openxmlformats.schemas.presentationml.x2006.main.CTPicture)2 Color (java.awt.Color)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 QName (javax.xml.namespace.QName)1 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)1 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)1 TexturePaint (org.apache.poi.sl.usermodel.PaintStyle.TexturePaint)1 LineCap (org.apache.poi.sl.usermodel.StrokeStyle.LineCap)1 LineDash (org.apache.poi.sl.usermodel.StrokeStyle.LineDash)1 XSLFFillProperties (org.apache.poi.xslf.usermodel.XSLFPropertiesDelegate.XSLFFillProperties)1 XmlCursor (org.apache.xmlbeans.XmlCursor)1 XmlException (org.apache.xmlbeans.XmlException)1