Example usage for org.apache.poi.openxml4j.opc PackagePart getOutputStream

List of usage examples for org.apache.poi.openxml4j.opc PackagePart getOutputStream

Introduction

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

Prototype

public OutputStream getOutputStream() 

Source Link

Document

Get the output stream of this part.

Usage

From source file:AddAudioToPptx.java

License:Apache License

public static void main(String[] args) throws Exception {
    URL video = new URL("http://archive.org/download/test-mpeg/test-mpeg.mpg");
    // URL video = new URL("file:test-mpeg.mpg");

    XMLSlideShow pptx = new XMLSlideShow();

    // add video file
    String videoFileName = "lego_edsheeran.mp3";
    PackagePartName partName = PackagingURIHelper.createPartName("/ppt/media/" + videoFileName);
    PackagePart part = pptx.getPackage().createPart(partName, "audio/mpeg");
    OutputStream partOs = part.getOutputStream();
    //InputStream fis = video.openStream();
    FileInputStream fis = new FileInputStream(videoFileName);
    byte buf[] = new byte[1024];
    for (int readBytes; (readBytes = fis.read(buf)) != -1; partOs.write(buf, 0, readBytes))
        ;/*www.  ja va 2 s  .co  m*/
    fis.close();
    partOs.close();

    XSLFSlide slide1 = pptx.createSlide();

    byte[] picture = IOUtils.toByteArray(new FileInputStream("audio.png"));

    //adding the image to the presentation
    XSLFPictureData idx = pptx.addPicture(picture, XSLFPictureData.PictureType.PNG);

    //creating a slide with given picture on it
    XSLFPictureShape pv1 = slide1.createPicture(idx);

    //XSLFPictureShape pv1 = // addPreview(pptx, slide1, part, 5, 50, 80);
    addAudio(pptx, slide1, part, pv1, 5);
    addTimingInfo(slide1, pv1);
    //XSLFPictureShape pv2 = addPreview(pptx, slide1, part, 9, 50, 250);
    //addVideo(pptx, slide1, part, pv2, 9);
    //addTimingInfo(slide1, pv2);

    FileOutputStream fos = new FileOutputStream("pptx-with-audio.pptx");
    pptx.write(fos);
    fos.close();

    pptx.close();
}

From source file:AddVideoToPptx.java

License:Apache License

public static void main(String[] args) throws Exception {
    URL video = new URL("http://archive.org/download/test-mpeg/test-mpeg.mpg");
    // URL video = new URL("file:test-mpeg.mpg");

    XMLSlideShow pptx = new XMLSlideShow();

    // add video file
    String videoFileName = video.getPath().substring(video.getPath().lastIndexOf('/') + 1);
    PackagePartName partName = PackagingURIHelper.createPartName("/ppt/media/" + videoFileName);
    PackagePart part = pptx.getPackage().createPart(partName, "video/mpeg");
    OutputStream partOs = part.getOutputStream();
    //InputStream fis = video.openStream();
    FileInputStream fis = new FileInputStream("Lea Michele - Cannonball.mp4");
    byte buf[] = new byte[1024];
    for (int readBytes; (readBytes = fis.read(buf)) != -1; partOs.write(buf, 0, readBytes))
        ;/*from   ww w.  j a  va2s .  co m*/
    fis.close();
    partOs.close();

    XSLFSlide slide1 = pptx.createSlide();
    XSLFPictureShape pv1 = addPreview(pptx, slide1, part, 5, 50, 80);
    addVideo(pptx, slide1, part, pv1, 5);
    addTimingInfo(slide1, pv1);
    //XSLFPictureShape pv2 = addPreview(pptx, slide1, part, 9, 50, 250);
    //addVideo(pptx, slide1, part, pv2, 9);
    //addTimingInfo(slide1, pv2);

    FileOutputStream fos = new FileOutputStream("pptx-with-video.pptx");
    pptx.write(fos);
    fos.close();

    pptx.close();
}

From source file:com.gezipu360.cashier.bean.word.UpdateEmbeddedDoc.java

License:Apache License

/**
 * Called to update the embedded Excel workbook. As the format and structire
 * of the workbook are known in advance, all this code attempts to do is
 * write a new value into the first cell on the first row of the first
 * worksheet. Prior to executing this method, that cell will contain the
 * value 1.//from ww  w .j a  va2 s  .  co m
 *
 * @throws org.apache.poi.openxml4j.exceptions.OpenXML4JException
 *                             Rather
 *                             than use the specific classes (HSSF/XSSF) to handle the embedded
 *                             workbook this method uses those defeined in the SS stream. As
 *                             a result, it might be the case that a SpreadsheetML file is
 *                             opened for processing, throwing this exception if that file is
 *                             invalid.
 * @throws java.io.IOException Thrown if a problem occurs in the underlying
 *                             file system.
 */
public void updateEmbeddedDoc() throws OpenXML4JException, IOException {
    Workbook workbook = null;
    Sheet sheet = null;
    Row row = null;
    Cell cell = null;
    PackagePart pPart = null;
    Iterator<PackagePart> pIter = null;
    List<PackagePart> embeddedDocs = this.doc.getAllEmbedds();
    if (embeddedDocs != null && !embeddedDocs.isEmpty()) {
        pIter = embeddedDocs.iterator();
        while (pIter.hasNext()) {
            pPart = pIter.next();
            if (pPart.getPartName().getExtension().equals(BINARY_EXTENSION)
                    || pPart.getPartName().getExtension().equals(OPENXML_EXTENSION)) {

                // Get an InputStream from the pacage part and pass that
                // to the create method of the WorkbookFactory class. Update
                // the resulting Workbook and then stream that out again
                // using an OutputStream obtained from the same PackagePart.
                workbook = WorkbookFactory.create(pPart.getInputStream());
                sheet = workbook.getSheetAt(SHEET_NUM);
                row = sheet.getRow(ROW_NUM);
                cell = row.getCell(CELL_NUM);
                cell.setCellValue(NEW_VALUE);
                workbook.write(pPart.getOutputStream());
            }
        }

        // Finally, write the newly modified Word document out to file.
        this.doc.write(new FileOutputStream(this.docFile));
    }
}

From source file:com.vodafone.poms.ii.helpers.ExportManager.java

private static String addFile(XSSFSheet sh, String filePath, double oleId)
        throws IOException, InvalidFormatException {
    File file = new File(filePath);
    FileInputStream fin = new FileInputStream(file);
    byte[] data;/*from   ww w  .j  av  a  2s . co  m*/
    data = new byte[fin.available()];
    fin.read(data);
    Ole10Native ole10 = new Ole10Native(file.getAbsolutePath(), file.getAbsolutePath(), file.getAbsolutePath(),
            data);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(500);
    ole10.writeOut(bos);

    POIFSFileSystem poifs = new POIFSFileSystem();
    poifs.getRoot().createDocument(Ole10Native.OLE10_NATIVE, new ByteArrayInputStream(bos.toByteArray()));

    poifs.getRoot().setStorageClsid(ClassID.OLE10_PACKAGE);

    final PackagePartName pnOLE = PackagingURIHelper
            .createPartName("/xl/embeddings/oleObject" + oleId + Math.random() + ".bin");
    final PackagePart partOLE = sh.getWorkbook().getPackage().createPart(pnOLE,
            "application/vnd.openxmlformats-officedocument.oleObject");
    PackageRelationship prOLE = sh.getPackagePart().addRelationship(pnOLE, TargetMode.INTERNAL,
            POIXMLDocument.OLE_OBJECT_REL_TYPE);
    OutputStream os = partOLE.getOutputStream();
    poifs.writeFilesystem(os);
    os.close();
    poifs.close();

    return prOLE.getId();

}

From source file:de.kiwiwings.jasperreports.exporter.PptxShapeExporter.java

License:Open Source License

protected void embedFonts() throws JRException, IOException {
    RepositoryUtil ru = RepositoryUtil.getInstance(jasperReportsContext);

    List<FontFamily> ferList = jasperReportsContext.getExtensions(FontFamily.class);
    if (ferList == null || ferList.size() == 0)
        return;//from  w w w .  ja v a  2 s.  c o m

    FontFactory fontfac = FontFactory.getInstance();
    EOTWriter conv = new EOTWriter(true); // generate MicroTypeExpress (mtx) fonts

    CTEmbeddedFontList fontList = null;

    boolean anyFontsEmbedded = false;
    for (FontFamily ff : ferList) {
        if (!"embed".equals(ff.getExportFont(PPTX_EXPORTER_KEY))) {
            // don't export font
            continue;
        }
        anyFontsEmbedded = true;

        // "<p:font typeface=\"AllianzLogo\" pitchFamily=\"49\" charset=\"2\"/><p:regular r:id=\"rId13\"/>"
        FontFace ffTypes[] = { ff.getNormalFace(), ff.getBoldFace(), ff.getItalicFace(),
                ff.getBoldItalicFace() };

        CTEmbeddedFontListEntry fntEntry = null;

        for (int i = 0; i < 4; i++) {
            if (ffTypes[i] == null)
                continue;
            String fileRelPath = ffTypes[i].getTtf();
            String fntDataName = fileRelPath.replaceAll(".*/(.*).ttf", "$1.fntdata");
            byte fontBytes[] = ru.getBytesFromLocation(fileRelPath);

            Font fonts[] = fontfac.loadFonts(fontBytes);
            assert (fonts != null && fonts.length == 1);

            WritableFontData eotFont = conv.convert(fonts[0]);

            PackagePartName partName;
            try {
                partName = PackagingURIHelper.createPartName("/ppt/fonts/" + fntDataName);
            } catch (InvalidFormatException e) {
                throw new IOException(e);
            }
            PackagePart part = ppt.getPackage().createPart(partName, "application/x-fontdata");
            OutputStream partOs = part.getOutputStream();
            eotFont.copyTo(partOs);
            partOs.close();

            PackageRelationship prs = ppt.getPackagePart().addRelationship(partName, TargetMode.INTERNAL,
                    "http://schemas.openxmlformats.org/officeDocument/2006/relationships/font");

            if (fontList == null) {
                fontList = ppt.getCTPresentation().addNewEmbeddedFontLst();
            }
            if (fntEntry == null) {
                fntEntry = fontList.addNewEmbeddedFont();
                CTTextFont fnt = fntEntry.addNewFont();
                fnt.setTypeface(ff.getName());
                // TODO set charset / pitchFamily
            }

            CTEmbeddedFontDataId fntId = null;
            switch (i) {
            case 0:
                fntId = fntEntry.addNewRegular();
                break;
            case 1:
                fntId = fntEntry.addNewBold();
                break;
            case 2:
                fntId = fntEntry.addNewItalic();
                break;
            case 3:
                fntId = fntEntry.addNewBoldItalic();
                break;
            }

            fntId.setId(prs.getId());
        }
    }

    if (anyFontsEmbedded) {
        CTPresentation pres = ppt.getCTPresentation();
        pres.setEmbedTrueTypeFonts(true);
        pres.setSaveSubsetFonts(true);
    }
}