Search in sources :

Example 1 with PartName

use of org.docx4j.openpackaging.parts.PartName in project Java-Tutorial by gpcodervn.

the class LinkToSlides method startPptx.

private static void startPptx() throws Exception {
    // generate Ppt file
    String outFileName = "output/LinkToSlides.pptx";
    PresentationMLPackage presentationMLPackage = PresentationMLPackage.createPackage();
    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 = null;
    slidePart = presentationMLPackage.createSlidePart(pp, layoutPart, new PartName("/ppt/slides/slide1.xml"));
    org.docx4j.relationships.ObjectFactory factory = new org.docx4j.relationships.ObjectFactory();
    // first Link relation
    org.docx4j.relationships.Relationship rel1 = factory.createRelationship();
    rel1.setType(Namespaces.PRESENTATIONML_SLIDE);
    rel1.setTarget("/ppt/slides/slide" + 5 + ".xml");
    slidePart.getRelationshipsPart().addRelationship(rel1);
    String linkedTable = table1.replaceFirst("replaceID", rel1.getId());
    // second link relation
    org.docx4j.relationships.Relationship rel2 = factory.createRelationship();
    rel2.setType(Namespaces.PRESENTATIONML_SLIDE);
    rel2.setTarget("/ppt/slides/slide" + 10 + ".xml");
    slidePart.getRelationshipsPart().addRelationship(rel2);
    linkedTable = linkedTable.replaceFirst("replaceID", rel2.getId());
    CTGraphicalObjectFrame graphicFrame2 = (CTGraphicalObjectFrame) XmlUtils.unmarshalString(linkedTable, Context.jcPML, CTGraphicalObjectFrame.class);
    slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(graphicFrame2);
    for (int i = 2; i < 15; i++) {
        slidePart = presentationMLPackage.createSlidePart(pp, layoutPart, new PartName("/ppt/slides/slide" + i + ".xml"));
        Shape sample = ((Shape) XmlUtils.unmarshalString(getTextData(i), Context.jcPML));
        slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(sample);
    }
    presentationMLPackage.save(new java.io.File(outFileName));
    System.out.println("Done");
}
Also used : CTGraphicalObjectFrame(org.pptx4j.pml.CTGraphicalObjectFrame) Shape(org.pptx4j.pml.Shape) PresentationMLPackage(org.docx4j.openpackaging.packages.PresentationMLPackage) SlideLayoutPart(org.docx4j.openpackaging.parts.PresentationML.SlideLayoutPart) SlidePart(org.docx4j.openpackaging.parts.PresentationML.SlidePart) PartName(org.docx4j.openpackaging.parts.PartName) MainPresentationPart(org.docx4j.openpackaging.parts.PresentationML.MainPresentationPart)

Example 2 with PartName

use of org.docx4j.openpackaging.parts.PartName in project Java-Tutorial by gpcodervn.

the class ReportFromTemplate method main.

public static void main(String[] args) {
    final String XPATH_TO_SELECT_TEXT_NODES = "//w:t";
    String fileName = "";
    try {
        // Populate the Strings that will replace the template text
        Map<String, String> map = new HashMap<String, String>();
        map.put("Project", "BP Mount");
        map.put("Date", "21-Mar-2011");
        // C:\\test\\template1.docx is the template file
        WordprocessingMLPackage template = WordprocessingMLPackage.load(new File("resources/Template1.docx"));
        Parts parts = template.getParts();
        HashMap<PartName, Part> partsMap = parts.getParts();
        PartName partName = null;
        Part part = null;
        Set<PartName> set = partsMap.keySet();
        for (Iterator<PartName> iterator = set.iterator(); iterator.hasNext(); ) {
            PartName name = (PartName) iterator.next();
            if (name.getName().equalsIgnoreCase("/word/media/image1.png")) {
                part = partsMap.get(name);
                partName = name;
            }
        }
        if (part != null && partName != null) {
            part = partsMap.get(partName);
            BinaryPart binaryPart = (BinaryPart) part;
            binaryPart.setBinaryData(fileToBytes(fileToReplace));
        }
        List<Object> texts = template.getMainDocumentPart().getJAXBNodesViaXPath(XPATH_TO_SELECT_TEXT_NODES, true);
        for (Object obj : texts) {
            Text text = (Text) ((JAXBElement) obj).getValue();
            String textValue = text.getValue();
            for (Object key : map.keySet()) {
                // textValue = textValue.replaceAll("\\$\\{" + key + "\\}", (String) map.get(key));
                textValue = textValue.trim().replace("${" + key + "}", (String) map.get(key));
            }
            text.setValue(textValue);
        }
        /*
			 * Add the other contents here
			 */
        template.save(new File("output/ReportFromTemplate.docx"));
        System.out.println("Done");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("Errors");
    }
}
Also used : BinaryPart(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart) HashMap(java.util.HashMap) Text(org.docx4j.wml.Text) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PartName(org.docx4j.openpackaging.parts.PartName) Parts(org.docx4j.openpackaging.parts.Parts) Part(org.docx4j.openpackaging.parts.Part) BinaryPart(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart) File(java.io.File)

Example 3 with PartName

use of org.docx4j.openpackaging.parts.PartName in project docx4j-template by vindell.

the class Docx4j_工具类_S3_Test method getComments.

public int getComments(WordprocessingMLPackage wordMLPackage) throws Exception {
    Parts parts = wordMLPackage.getParts();
    HashMap<PartName, Part> partMap = parts.getParts();
    CommentsPart commentPart = (CommentsPart) partMap.get(new CommentsPart().getPartName());
    Comments comments = commentPart.getContents();
    List<Comment> commentList = comments.getComment();
    for (Comment comment : commentList) {
        StringBuffer sb = new StringBuffer();
        sb.append(" ID: ").append(comment.getId());
        sb.append(" 作者:").append(comment.getAuthor());
        sb.append(" 时间: ").append(comment.getDate().toGregorianCalendar().getTime());
        sb.append(" 内容:").append(comment.getContent());
        // sb.append(" 文中内容:").append(docCmtMap.get(comment.getId().toString()));
        System.out.println(sb.toString());
    }
    return 0;
}
Also used : Comment(org.docx4j.wml.Comments.Comment) PartName(org.docx4j.openpackaging.parts.PartName) Parts(org.docx4j.openpackaging.parts.Parts) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) Part(org.docx4j.openpackaging.parts.Part) CommentsPart(org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart) Comments(org.docx4j.wml.Comments) CommentsPart(org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart)

Example 4 with PartName

use of org.docx4j.openpackaging.parts.PartName in project docx4j-template by vindell.

the class WMLPackageUtils method insertDocx.

// 插入文档
private static void insertDocx(MainDocumentPart main, byte[] bytes, int chunkId) {
    try {
        AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/part" + chunkId + ".docx"));
        afiPart.setContentType(new ContentType(CONTENT_TYPE));
        afiPart.setBinaryData(bytes);
        Relationship altChunkRel = main.addTargetPart(afiPart);
        CTAltChunk chunk = Context.getWmlObjectFactory().createCTAltChunk();
        chunk.setId(altChunkRel.getId());
        main.addObject(chunk);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : PartName(org.docx4j.openpackaging.parts.PartName) ContentType(org.docx4j.openpackaging.contenttype.ContentType) Relationship(org.docx4j.relationships.Relationship) CTAltChunk(org.docx4j.wml.CTAltChunk) AlternativeFormatInputPart(org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException)

Example 5 with PartName

use of org.docx4j.openpackaging.parts.PartName in project Aspose.Cells-for-Java by aspose-cells.

the class Xlsx4jAddComments method main.

/**
 * @param args
 */
public static void main(String[] args) {
    try {
        // The path to the documents directory.
        String dataDir = Utils.getDataDir(Xlsx4jAddComments.class);
        String outputfilepath = dataDir + "AddComments-Xlsx4j.xlsx";
        SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
        WorksheetPart sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
        addContent(sheet);
        pkg.save(new File(outputfilepath));
        System.out.println("\n\n done .. " + outputfilepath);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SpreadsheetMLPackage(org.docx4j.openpackaging.packages.SpreadsheetMLPackage) PartName(org.docx4j.openpackaging.parts.PartName) WorksheetPart(org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart) File(java.io.File) JAXBException(javax.xml.bind.JAXBException) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException)

Aggregations

PartName (org.docx4j.openpackaging.parts.PartName)11 File (java.io.File)4 SpreadsheetMLPackage (org.docx4j.openpackaging.packages.SpreadsheetMLPackage)4 Part (org.docx4j.openpackaging.parts.Part)4 WorksheetPart (org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart)4 IOException (java.io.IOException)3 Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)3 SaveToZipFile (org.docx4j.openpackaging.io.SaveToZipFile)3 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)3 Parts (org.docx4j.openpackaging.parts.Parts)3 Relationship (org.docx4j.relationships.Relationship)3 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 JAXBException (javax.xml.bind.JAXBException)2 ContentType (org.docx4j.openpackaging.contenttype.ContentType)2 AlternativeFormatInputPart (org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart)2 BinaryPart (org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart)2 BinaryPartAbstractImage (org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)2 CommentsPart (org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart)2 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)2