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();
}
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();
}
}
}
Aggregations