Example usage for org.apache.poi.openxml4j.opc OPCPackage createPart

List of usage examples for org.apache.poi.openxml4j.opc OPCPackage createPart

Introduction

In this page you can find the example usage for org.apache.poi.openxml4j.opc OPCPackage createPart.

Prototype

public PackagePart createPart(PackagePartName partName, String contentType, ByteArrayOutputStream content) 

Source Link

Document

Add a part to the package.

Usage

From source file:com.hp.autonomy.frontend.reports.powerpoint.PowerPointServiceImpl.java

License:MIT License

/**
 * Utility function to write a chart object to a slide based on a template.
 * Creates new copies of referred objects in the chart, e.g. colors1.xml and style1.xml, and writes the Excel
 *   workbook data to new files in the PowerPoint .zip structure.
 * @param pptx the presentation to add to.
 * @param slide the slide to add to.//from  w ww.j  a  v a 2 s .  c o m
 * @param templateChart the original template chart XML reference object from the template.
 * @param modifiedChart the new chart XML object.
 * @param workbook the Excel workbook data corresponding to the chart XML data.
 * @param relId the relation id for the new chart.
 * @throws IOException if there's IO errors working with the chart.
 * @throws InvalidFormatException if there's errors generating new package part names for the new copies of the data.
 */
private static void writeChart(final XMLSlideShow pptx, final XSLFSlide slide, final XSLFChart templateChart,
        final CTChartSpace modifiedChart, final XSSFWorkbook workbook, final String relId)
        throws IOException, InvalidFormatException {
    final OPCPackage opcPackage = pptx.getPackage();
    final PackagePartName chartName = generateNewName(opcPackage,
            templateChart.getPackagePart().getPartName().getURI().getPath());

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
    xmlOptions.setSaveSyntheticDocumentElement(
            new QName(CTChartSpace.type.getName().getNamespaceURI(), "chartSpace", "c"));
    modifiedChart.save(baos, xmlOptions);

    final PackagePart chartPart = opcPackage.createPart(chartName, XSLFRelation.CHART.getContentType(), baos);

    slide.getPackagePart().addRelationship(chartName, TargetMode.INTERNAL, XSLFRelation.CHART.getRelation(),
            relId);

    for (final POIXMLDocumentPart.RelationPart part : templateChart.getRelationParts()) {
        final ByteArrayOutputStream partCopy = new ByteArrayOutputStream();
        final URI targetURI = part.getRelationship().getTargetURI();

        final PackagePartName name = generateNewName(opcPackage, targetURI.getPath());

        final String contentType = part.getDocumentPart().getPackagePart().getContentType();

        if ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equals(contentType)) {
            workbook.write(partCopy);
        } else {
            IOUtils.copy(part.getDocumentPart().getPackagePart().getInputStream(), partCopy);
        }

        opcPackage.createPart(name, contentType, partCopy);
        chartPart.addRelationship(name, TargetMode.INTERNAL, part.getRelationship().getRelationshipType());
    }
}