Package org.openxml4j.opc

Examples of org.openxml4j.opc.PackagePart


   * Returns the low level slide object from
   *  the supplied slide reference
   */
  public CTSlide getSlide(CTSlideIdListEntry slide) throws IOException, XmlException {
    try {
      PackagePart slidePart =
        getTargetPart(getCorePart().getRelationship(slide.getId2()));
      SldDocument slideDoc =
        SldDocument.Factory.parse(slidePart.getInputStream());
      return slideDoc.getSld();
    } catch(InvalidFormatException e) {
      throw new XmlException(e);
    }
  }
View Full Code Here


   *  slide, as found from the supplied slide reference
   */
  public CTNotesSlide getNotes(CTSlideIdListEntry slide) throws IOException, XmlException {
    PackageRelationshipCollection notes;
    try {
      PackagePart slidePart =
        getTargetPart(getCorePart().getRelationship(slide.getId2()));
   
      notes = slidePart.getRelationshipsByType(NOTES_RELATION_TYPE);
    } catch(InvalidFormatException e) {
      throw new IllegalStateException(e);
    }
   
    if(notes.size() == 0) {
      // No notes for this slide
      return null;
    }
    if(notes.size() > 1) {
      throw new IllegalStateException("Expecting 0 or 1 notes for a slide, but found " + notes.size());
    }
   
    try {
      PackagePart notesPart =
        getTargetPart(notes.getRelationship(0));
      NotesDocument notesDoc =
        NotesDocument.Factory.parse(notesPart.getInputStream());
     
      return notesDoc.getNotes();
    } catch(InvalidFormatException e) {
      throw new IllegalStateException(e);
    }
View Full Code Here

      pkg.getRelationshipsByType(CORE_DOCUMENT_REL);
    if(core.size() != 1) {
      throw new IllegalArgumentException("Invalid OOXML Package received - expected 1 core document, found " + core.size());
    }
   
    PackagePart corePart = pkg.getPart(core.getRelationship(0));
    if(corePart.getContentType().equals(XSSFWorkbook.WORKBOOK.getContentType())) {
      return new XSSFExcelExtractor(pkg);
    }
    if(corePart.getContentType().equals(XWPFDocument.MAIN_CONTENT_TYPE)) {
      return new XWPFWordExtractor(pkg);
    }
    if(corePart.getContentType().equals(XSLFSlideShow.MAIN_CONTENT_TYPE)) {
      return new XSLFPowerPointExtractor(pkg);
    }
    throw new IllegalArgumentException("No supported documents found in the OOXML package");
  }
View Full Code Here

    }

    // Get the comments, if there are any
    PackageRelationshipCollection commentsRel = getCmntRelations();
    if(commentsRel != null && commentsRel.size() > 0) {
      PackagePart commentsPart = getTargetPart(commentsRel.getRelationship(0));
      CommentsDocument cmntdoc = CommentsDocument.Factory.parse(commentsPart.getInputStream());
      for(CTComment ctcomment : cmntdoc.getComments().getCommentArray())
      {
        comments.add(new XWPFComment(ctcomment));
      }
     
View Full Code Here

     * @return The target part
     * @throws InvalidFormatException
     */
    protected PackagePart getTargetPart(PackageRelationship rel) throws InvalidFormatException {
        PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
        PackagePart part = getPackage().getPart(relName);
        if (part == null) {
            throw new IllegalArgumentException("No part found for relationship " + rel);
        }
        return part;
    }
View Full Code Here

        if(rel == null) {
          throw new IllegalArgumentException("No Sheet found with r:id " + relId);
        }
       
        PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
        PackagePart sheet = pkg.getPart(relName);
        if(sheet == null) {
          throw new IllegalArgumentException("No data found for Sheet with r:id " + relId);
        }
        return sheet.getInputStream();
  }
View Full Code Here

    public InputStream next() {
      PackageRelationship sheet = sheetRels.next();
      try {
            PackagePartName relName = PackagingURIHelper.createPartName(sheet.getTargetURI());
        PackagePart sheetPkg = pkg.getPart(relName);
        return sheetPkg.getInputStream();
      } catch(IOException e) {
        throw new RuntimeException(e);
      } catch(InvalidFormatException ife) {
        throw new RuntimeException(ife);
      }
View Full Code Here

              throw new IOException("Unable to load styles - " + e.toString());
            }
           
            // Load individual sheets
            for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
                PackagePart part = getPackagePart(ctSheet);
                if (part == null) {
                  log.log(POILogger.WARN, "Sheet with name " + ctSheet.getName() + " and r:id " + ctSheet.getId()+ " was defined, but didn't exist in package, skipping");
                    continue;
                }
               
                // Get the comments for the sheet, if there are any
                CommentsSource comments = null;
                PackageRelationshipCollection commentsRel =
                  part.getRelationshipsByType(SHEET_COMMENTS.REL);
                if(commentsRel != null && commentsRel.size() > 0) {
                  PackagePart commentsPart =
                    getTargetPart(commentsRel.getRelationship(0));
                  comments = new CommentsTable(commentsPart.getInputStream());
                }
               
                // Now create the sheet
                WorksheetDocument worksheetDoc = WorksheetDocument.Factory.parse(part.getInputStream());
                XSSFSheet sheet = new XSSFSheet(ctSheet, worksheetDoc.getWorksheet(), this, comments);
View Full Code Here

    public List<PictureData> getAllPictures() {
        // In OOXML pictures are referred to in sheets
        List<PictureData> pictures = new LinkedList<PictureData>();
        for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
            try {
                PackagePart sheetPart = getPackagePart(ctSheet);
                if (sheetPart == null) {
                    continue;
                }
                PackageRelationshipCollection prc = sheetPart.getRelationshipsByType(DRAWINGS.getRelation());
                for (PackageRelationship rel : prc) {
                    PackagePart drawingPart = getTargetPart(rel);
                    PackageRelationshipCollection prc2 = drawingPart.getRelationshipsByType(IMAGES.getRelation());
                    for (PackageRelationship rel2 : prc2) {
                        PackagePart imagePart = getTargetPart(rel2);
                        XSSFPictureData pd = new XSSFPictureData(imagePart);
                        pictures.add(pd);
                    }
                }
            } catch (InvalidFormatException e) {
View Full Code Here

            // Main part
            PackagePartName corePartName = PackagingURIHelper.createPartName("/xl/workbook.xml");
            // Create main part relationship
            pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT, "rId1");
            // Create main document part
            PackagePart corePart = pkg.createPart(corePartName, WORKBOOK.getContentType());
            OutputStream out;

            XmlOptions xmlOptions = new XmlOptions();
            // Requests use of whitespace for easier reading
            xmlOptions.setSavePrettyPrint();
            xmlOptions.setSaveOuter();
            xmlOptions.setUseDefaultNamespace();
           
            // Write out our sheets, updating the references
            //  to them in the main workbook as we go
            for (int i=0 ; i < this.getNumberOfSheets(); i++) {
              int sheetNumber = (i+1);
              XSSFSheet sheet = (XSSFSheet) this.getSheetAt(i);
                PackagePartName partName = PackagingURIHelper.createPartName(
                    WORKSHEET.getFileName(sheetNumber));
                PackageRelationship rel =
                   corePart.addRelationship(partName, TargetMode.INTERNAL, WORKSHEET.getRelation(), "rSheet" + sheetNumber);
                PackagePart part = pkg.createPart(partName, WORKSHEET.getContentType());
                
                // XXX This should not be needed, but apparently the setSaveOuter call above does not work in XMLBeans 2.2
                xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorksheet.type.getName().getNamespaceURI(), "worksheet"));
                sheet.save(part, xmlOptions);
                
                // Update our internal reference for the package part
                workbook.getSheets().getSheetArray(i).setId(rel.getId());
                workbook.getSheets().getSheetArray(i).setSheetId(sheetNumber);
               
                // If our sheet has comments, then write out those
                if(sheet.hasComments()) {
                  CommentsTable ct = (CommentsTable)sheet.getCommentsSourceIfExists();
                    PackagePartName ctName = PackagingURIHelper.createPartName(
                        SHEET_COMMENTS.getFileName(sheetNumber));
                    part.addRelationship(ctName, TargetMode.INTERNAL, SHEET_COMMENTS.getRelation(), "rComments");
                    PackagePart ctPart = pkg.createPart(ctName, SHEET_COMMENTS.getContentType());
                   
                    out = ctPart.getOutputStream();
                    ct.writeTo(out);
                    out.close();
                }
            }
            
View Full Code Here

TOP

Related Classes of org.openxml4j.opc.PackagePart

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.