Package org.docx4j.openpackaging.packages

Examples of org.docx4j.openpackaging.packages.PresentationMLPackage


      org.docx4j.xmlPackage.Package wmlPackageEl = (org.docx4j.xmlPackage.Package)((JAXBElement)u.unmarshal(
          new javax.xml.transform.stream.StreamSource(new FileInputStream(inputfilepath)))).getValue();

      org.docx4j.convert.in.FlatOpcXmlImporter xmlPackage = new org.docx4j.convert.in.FlatOpcXmlImporter( wmlPackageEl);

      PresentationMLPackage pmlPackage = (PresentationMLPackage)xmlPackage.get();
     
      // Now that its loaded properly, lets just save it
     
      if (save) {
        SaveToZipFile saver = new SaveToZipFile(pmlPackage);
View Full Code Here


    // Where will we save our new .ppxt?
    String outputfilepath = System.getProperty("user.dir") + "/sample-docs/pptx-test.pptx";
    if (MACRO_ENABLE) outputfilepath += "m";
   
    // Create skeletal package, including a MainPresentationPart and a SlideLayoutPart
    PresentationMLPackage presentationMLPackage = PresentationMLPackage.createPackage();
   
    if (MACRO_ENABLE) {
      ContentTypeManager ctm = presentationMLPackage.getContentTypeManager();
      ctm.removeContentType(new PartName("/ppt/presentation.xml") );
      ctm.addOverrideContentType(new URI("/ppt/presentation.xml"), ContentTypes.PRESENTATIONML_MACROENABLED);
    }
    
    // Need references to these parts to create a slide
    // Please note that these parts *already exist* - they are
    // created by createPackage() above.  See that method
    // for instruction on how to create and add a part.
    MainPresentationPart pp = (MainPresentationPart)presentationMLPackage.getParts().getParts().get(
        new PartName("/ppt/presentation.xml"));   
    SlideLayoutPart layoutPart = (SlideLayoutPart)presentationMLPackage.getParts().getParts().get(
        new PartName("/ppt/slideLayouts/slideLayout1.xml"));
   
    // OK, now we can create a slide
    SlidePart slidePart = new SlidePart(new PartName("/ppt/slides/slide1.xml"));
    slidePart.setContents( SlidePart.createSld() );   
    pp.addSlide(0, slidePart);
   
    // Slide layout part
    slidePart.addTargetPart(layoutPart);
   
       
    // Create and add shape
    Shape sample = ((Shape)XmlUtils.unmarshalString(SAMPLE_SHAPE, Context.jcPML) );
    slidePart.getContents().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(sample);
   
    // All done: save it
    presentationMLPackage.save(new java.io.File(outputfilepath));

    System.out.println("\n\n done .. saved " + outputfilepath);
   
 
View Full Code Here

    // Where will we save our new .ppxt?
    String outputfilepath = System.getProperty("user.dir") + "/sample-docs/OUT_SlideNotes.pptx";
   
    // Create skeletal package, including a MainPresentationPart and a SlideLayoutPart
    PresentationMLPackage presentationMLPackage = PresentationMLPackage.createPackage();
   
    // Need references to these parts to create a slide
    // Please note that these parts *already exist* - they are
    // created by createPackage() above.  See that method
    // for instruction on how to create and add a part.
    MainPresentationPart pp = (MainPresentationPart)presentationMLPackage.getParts().getParts().get(
        new PartName("/ppt/presentation.xml"));   
    SlideLayoutPart layoutPart = (SlideLayoutPart)presentationMLPackage.getParts().getParts().get(
        new PartName("/ppt/slideLayouts/slideLayout1.xml"));
   
    // OK, now we can create a slide
    SlidePart slidePart = new SlidePart(new PartName("/ppt/slides/slide1.xml"));
    slidePart.setContents( SlidePart.createSld() );   
    pp.addSlide(0, slidePart);
   
    // Slide layout part
    slidePart.addTargetPart(layoutPart);
       
    // Create and add shape
    Shape sample = ((Shape)XmlUtils.unmarshalString(SAMPLE_SHAPE, Context.jcPML) );
    slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(sample);
   
    // Now add notes slide.
    // 1. Notes master
    NotesMasterPart nmp = new NotesMasterPart();
    NotesMaster notesmaster = (NotesMaster)XmlUtils.unmarshalString(notesMasterXml, Context.jcPML);
    nmp.setJaxbElement(notesmaster);
    // .. connect it to /ppt/presentation.xml
    Relationship ppRelNmp = pp.addTargetPart(nmp);
    /*
     *  <p:notesMasterIdLst>
                <p:notesMasterId r:id="rId3"/>
            </p:notesMasterIdLst>
     */
    pp.getJaxbElement().setNotesMasterIdLst(createNotesMasterIdListPlusEntry(ppRelNmp.getId()));
   
    // .. NotesMasterPart typically has a rel to a theme
    // .. can we get away without it?
    // Nope .. read this in from a file
    ThemePart themePart = new ThemePart(new PartName("/ppt/theme/theme2.xml"));
      // TODO: read it from a string instead
    themePart.unmarshal(
        FileUtils.openInputStream(new File(System.getProperty("user.dir") + "/theme2.xml"))
      );   
    nmp.addTargetPart(themePart);
   
    // 2. Notes slide
    NotesSlidePart nsp = new NotesSlidePart();
    Notes notes = (Notes)XmlUtils.unmarshalString(notesXML, Context.jcPML);
    nsp.setJaxbElement(notes);
    // .. connect it to the slide
    slidePart.addTargetPart(nsp);
    // .. it also has a rel to the slide
    nsp.addTargetPart(slidePart);
    // .. and the slide master
    nsp.addTargetPart(nmp);
   
   
   
    // All done: save it
    presentationMLPackage.save(new java.io.File(outputfilepath));

    System.out.println("\n\n done .. saved " + outputfilepath);
   
 
View Full Code Here

    // Where will we save our new .pptx?
    String outputfilepath = System.getProperty("user.dir") + "/pptx-picture.pptx";
   
    // Create skeletal package, including a MainPresentationPart and a SlideLayoutPart
    PresentationMLPackage presentationMLPackage = PresentationMLPackage.createPackage();
   
    // Need references to these parts to create a slide
    // Please note that these parts *already exist* - they are
    // created by createPackage() above.  See that method
    // for instruction on how to create and add a part.
    MainPresentationPart pp = (MainPresentationPart)presentationMLPackage.getParts().getParts().get(
        new PartName("/ppt/presentation.xml"));   
    SlideLayoutPart layoutPart = (SlideLayoutPart)presentationMLPackage.getParts().getParts().get(
        new PartName("/ppt/slideLayouts/slideLayout1.xml"));
   
    // OK, now we can create a slide
    SlidePart slidePart = presentationMLPackage.createSlidePart(pp, layoutPart,
        new PartName("/ppt/slides/slide1.xml"));
       
    // Add image part
    File file = new File(System.getProperty("user.dir") + "/src/test/resources/images/greentick.png" );
        BinaryPartAbstractImage imagePart
          = BinaryPartAbstractImage.createImagePart(presentationMLPackage, slidePart, file);
   
           
        // Add p:pic to slide
    slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(
        createPicture(imagePart.getSourceRelationship().getId()));
   

    // Do it again on another slide
    SlidePart slidePart2 = presentationMLPackage.createSlidePart(pp, layoutPart,
        new PartName("/ppt/slides/slide2.xml"));
    Relationship rel = slidePart2.addTargetPart(imagePart);
   
    slidePart2.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(
        createPicture(rel.getId()));
   
    // All done: save it
    presentationMLPackage.save(new java.io.File(outputfilepath));

    System.out.println("\n\n done .. saved " + outputfilepath);
   
 
View Full Code Here

     
  public static void main(String[] args) throws Exception {

    String inputfilepath = System.getProperty("user.dir") + "/Presentation1.pptx";
   
    PresentationMLPackage presentationMLPackage =
      (PresentationMLPackage)OpcPackage.load(new java.io.File(inputfilepath));
   
    MainPresentationPart mpp = presentationMLPackage.getMainPresentationPart();
   
    //mpp.removeSlide(10);
    Relationship rel = mpp.getRelationshipsPart().getRelationshipByID("rId2");
    mpp.removeSlide(rel);
   
   
    System.out.println("\n\n saving .. \n\n");
    String outputfilepath = System.getProperty("user.dir") + "/pptx-out.pptx";
    presentationMLPackage.save(new java.io.File(outputfilepath));

    System.out.println("\n\n done .. \n\n");
   
 
View Full Code Here

    // Where will we save our new .ppxt?
    String outputfilepath = System.getProperty("user.dir") + "/OUT_Table.pptx";
   
    // Create skeletal package, including a MainPresentationPart and a SlideLayoutPart
    PresentationMLPackage presentationMLPackage = PresentationMLPackage.createPackage();
   
    // Need references to these parts to create a slide
    // Please note that these parts *already exist* - they are
    // created by createPackage() above.  See that method
    // for instruction on how to create and add a part.
    MainPresentationPart pp = (MainPresentationPart)presentationMLPackage.getParts().getParts().get(
        new PartName("/ppt/presentation.xml"));   
    SlideLayoutPart layoutPart = (SlideLayoutPart)presentationMLPackage.getParts().getParts().get(
        new PartName("/ppt/slideLayouts/slideLayout1.xml"));
   
    // OK, now we can create a slide
    SlidePart slidePart = presentationMLPackage.createSlidePart(pp, layoutPart,
        new PartName("/ppt/slides/slide1.xml"));
       
    // Method 1 - programmatic
    slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add( getTable() );
   
    // Method 2 - from string - on slide 2
    SlidePart slide2 = presentationMLPackage.createSlidePart(pp, layoutPart,
        new PartName("/ppt/slides/slide2.xml"));
    slide2.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add( createGraphicFrameFromString() );
   
    // All done: save it
    presentationMLPackage.save(new java.io.File(outputfilepath));

    System.out.println("\n\n done .. saved " + outputfilepath);
   
 
View Full Code Here

    // Where will we save our new .ppxt?
    String outputfilepath = "data/pptx4j/Tables-Pptx4j.pptx";
   
    // Create skeletal package, including a MainPresentationPart and a SlideLayoutPart
    PresentationMLPackage presentationMLPackage = PresentationMLPackage.createPackage();
   
    // Need references to these parts to create a slide
    // Please note that these parts *already exist* - they are
    // created by createPackage() above.  See that method
    // for instruction on how to create and add a part.
    MainPresentationPart pp = (MainPresentationPart)presentationMLPackage.getParts().getParts().get(
        new PartName("/ppt/presentation.xml"));   
    SlideLayoutPart layoutPart = (SlideLayoutPart)presentationMLPackage.getParts().getParts().get(
        new PartName("/ppt/slideLayouts/slideLayout1.xml"));
   
    // OK, now we can create a slide
    SlidePart slidePart = presentationMLPackage.createSlidePart(pp, layoutPart,
        new PartName("/ppt/slides/slide1.xml"));
       
    // Method 1 - programmatic
    slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add( getTable() );
   
    // Method 2 - from string - on slide 2
    SlidePart slide2 = presentationMLPackage.createSlidePart(pp, layoutPart,
        new PartName("/ppt/slides/slide2.xml"));
    slide2.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add( createGraphicFrameFromString() );
   
    // All done: save it
    presentationMLPackage.save(new java.io.File(outputfilepath));

    System.out.println("\n\n done .. saved " + outputfilepath);
   
 
View Full Code Here

    String firstValue  = String.valueOf(rand.nextInt(99));
    String secondValue = String.valueOf(rand.nextInt(99));
   
    // Open the PPT template file
    PresentationMLPackage ppt = (PresentationMLPackage) OpcPackage
      .load(new java.io.File(inputfilepath));

    /*
     * Get the Chart object and update the values. Afterwards, we'll update
     * the associated spreadsheet so that the data is synchronized.
     */
    Chart chart = (Chart) ppt.getParts().get(new PartName(chartPartName));
   
    List<Object> objects = chart.getJaxbElement().getChart().getPlotArea()
        .getAreaChartOrArea3DChartOrLineChart();
   
    for (Object object : objects) {
     
      if (object instanceof CTBarChart) {

        List<CTBarSer> ctBarSers = ((CTBarChart) object).getSer();
       
        for (CTBarSer ctBarSer : ctBarSers)
        {
          List<CTNumVal> ctNumVals = ctBarSer.getVal().getNumRef().getNumCache().getPt();
          for (CTNumVal ctNumVal : ctNumVals)
          {
            System.out.println("ctNumVal Val BEFORE: " + ctNumVal.getV());
            if (ctNumVal.getIdx() == 0) {
              ctNumVal.setV(firstValue);
            }
            else if (ctNumVal.getIdx() == 1) {
              ctNumVal.setV(secondValue)
            }
            System.out.println("ctNumVal Val AFTER: " + ctNumVal.getV());
          }
        }
      }
    }
       
    /*
     * Get the spreadsheet and find the cell values that need to be updated
     */
   
    EmbeddedPackagePart epp  = (EmbeddedPackagePart) ppt
      .getParts().get(new PartName(xlsPartName));
   
    if (epp==null) {
      throw new Docx4JException("Could find EmbeddedPackagePart: " + xlsPartName);
    }
   
    InputStream is = BufferUtil.newInputStream(epp.getBuffer());
   
    SpreadsheetMLPackage spreadSheet = (SpreadsheetMLPackage) SpreadsheetMLPackage.load(is);

    Map<PartName,Part> partsMap = spreadSheet.getParts().getParts();    
    Iterator<Entry<PartName, Part>> it = partsMap.entrySet().iterator();

    while(it.hasNext()) {
      Map.Entry<PartName, Part> pairs = it.next();
     
      if (partsMap.get(pairs.getKey()) instanceof WorksheetPart) {
       
        WorksheetPart wsp = (WorksheetPart) partsMap.get(pairs.getKey()) ;
       
        List<Row> rows = wsp.getJaxbElement().getSheetData().getRow();

        for (Row row : rows) {
          List<Cell> cells = row.getC();
          for (Cell cell : cells)
          {
            if (cell.getR().equals("B2") && cell.getV() != null) {
              System.out.println("B2 CELL VAL: " + cell.getV());
              // change the B2 cell value
              cell.setT(STCellType.STR);
              cell.setV(firstValue);
            }
            else if (cell.getR().equals("B3") && cell.getV() != null) {
              System.out.println("B3 CELL VAL: " + cell.getV());
              // Change the B3 cell value
              cell.setT(STCellType.STR);
              cell.setV(secondValue);
            }
          }         
        }
      }
    }

    /*
     * Convert the Spreadsheet to a binary format, set it on the
     * EmbeddedPackagePart, add it back onto the deck and save to a file.
     * 
     */   
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
   
    SaveToZipFile saver = new SaveToZipFile(spreadSheet);

    saver.save(baos);
    epp.setBinaryData(baos.toByteArray());

    // Write the new file to disk
    ppt.save(new java.io.File(outputfilepath));

    System.out.println("\n\n done .. saved " + outputfilepath);
  }
View Full Code Here

    catch (IllegalArgumentException e)
    {
      inputfilepath = "data/pptx4j/pptx-basic.xml";
    }

    PresentationMLPackage pMLPackage = (PresentationMLPackage) OpcPackage
        .load(new java.io.File(inputfilepath));

    SlidePart slide = (SlidePart) pMLPackage.getParts().get(
        new PartName("/ppt/slides/slide1.xml"));

    new TraversalUtil(slide.getJaxbElement().getCSld().getSpTree()
        .getSpOrGrpSpOrGraphicFrame(),
View Full Code Here

{
  public static void main(String[] args) throws Exception
  {
    String inputfilepath = "data/pptx4j/presentation.pptx";
   
    PresentationMLPackage presentationMLPackage =
      (PresentationMLPackage)OpcPackage.load(new java.io.File(inputfilepath));
   
    MainPresentationPart mpp = presentationMLPackage.getMainPresentationPart();
   
    //mpp.removeSlide(10);
    Relationship rel = mpp.getRelationshipsPart().getRelationshipByID("rId2");
    mpp.removeSlide(rel);
   
    System.out.println("\n\n saving .. \n\n");
    String outputfilepath = "data/pptx4j/RemovedSlide-Pptx4j.pptx";
    presentationMLPackage.save(new java.io.File(outputfilepath));

    System.out.println("\n\n done .. \n\n");   
 
View Full Code Here

TOP

Related Classes of org.docx4j.openpackaging.packages.PresentationMLPackage

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.