Search in sources :

Example 36 with PackagePartName

use of org.apache.poi.openxml4j.opc.PackagePartName in project poi by apache.

the class TestXWPFDocument method testRegisterPackagePictureData.

@Test
public void testRegisterPackagePictureData() throws IOException, InvalidFormatException {
    XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_1.docx");
    /* manually assemble a new image package part*/
    OPCPackage opcPckg = doc.getPackage();
    XWPFRelation jpgRelation = XWPFRelation.IMAGE_JPEG;
    PackagePartName partName = PackagingURIHelper.createPartName(jpgRelation.getDefaultFileName().replace('#', '2'));
    PackagePart newImagePart = opcPckg.createPart(partName, jpgRelation.getContentType());
    byte[] nature1 = XWPFTestDataSamples.getImage("abstract4.jpg");
    OutputStream os = newImagePart.getOutputStream();
    os.write(nature1);
    os.close();
    XWPFHeader xwpfHeader = doc.getHeaderArray(0);
    XWPFPictureData newPicData = new XWPFPictureData(newImagePart);
    /* new part is now ready to rumble */
    assertFalse(xwpfHeader.getAllPictures().contains(newPicData));
    assertFalse(doc.getAllPictures().contains(newPicData));
    assertFalse(doc.getAllPackagePictures().contains(newPicData));
    doc.registerPackagePictureData(newPicData);
    assertFalse(xwpfHeader.getAllPictures().contains(newPicData));
    assertFalse(doc.getAllPictures().contains(newPicData));
    assertTrue(doc.getAllPackagePictures().contains(newPicData));
    doc.getPackage().revert();
    opcPckg.close();
    doc.close();
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) OutputStream(java.io.OutputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 37 with PackagePartName

use of org.apache.poi.openxml4j.opc.PackagePartName in project poi by apache.

the class XSLFMetroShape method parseShape.

/*
     * parses the metro bytes to a XSLF shape
     */
public static Shape<?, ?> parseShape(byte[] metroBytes) throws InvalidFormatException, IOException, XmlException {
    PackagePartName shapePN = PackagingURIHelper.createPartName("/drs/shapexml.xml");
    OPCPackage pkg = null;
    try {
        pkg = OPCPackage.open(new ByteArrayInputStream(metroBytes));
        PackagePart shapePart = pkg.getPart(shapePN);
        CTGroupShape gs = CTGroupShape.Factory.parse(shapePart.getInputStream(), DEFAULT_XML_OPTIONS);
        XSLFGroupShape xgs = new XSLFGroupShape(gs, null);
        return xgs.getShapes().get(0);
    } finally {
        if (pkg != null) {
            pkg.close();
        }
    }
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) ByteArrayInputStream(java.io.ByteArrayInputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) CTGroupShape(org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape)

Aggregations

PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)37 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)19 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)19 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)16 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)13 Test (org.junit.Test)10 IOException (java.io.IOException)9 OutputStream (java.io.OutputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 POIXMLException (org.apache.poi.POIXMLException)5 ArrayList (java.util.ArrayList)4 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)4 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)4 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)4 URI (java.net.URI)3 PartAlreadyExistsException (org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException)3 XmlException (org.apache.xmlbeans.XmlException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 NoSuchElementException (java.util.NoSuchElementException)2