Search in sources :

Example 6 with PartName

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

the class Xlsx4jAddImage method main.

public static void main(String[] args) throws Exception {
    // The path to the documents directory.
    String dataDir = Utils.getDataDir(Xlsx4jAddImage.class);
    String outputfilepath = dataDir + "AddImage-Xlsx4j.xlsx";
    String imagefilePath = dataDir + "greentick.png";
    SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
    WorksheetPart worksheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
    // Create Drawing part and add to sheet
    Drawing drawingPart = new Drawing();
    Relationship drawingRel = worksheet.addTargetPart(drawingPart);
    // Add anchor XML to worksheet
    org.xlsx4j.sml.CTDrawing drawing = org.xlsx4j.jaxb.Context.getsmlObjectFactory().createCTDrawing();
    worksheet.getJaxbElement().setDrawing(drawing);
    drawing.setId(drawingRel.getId());
    // Create image part and add to Drawing part
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(pkg, drawingPart, FileUtils.readFileToByteArray(new File(imagefilePath)));
    String imageRelID = imagePart.getSourceRelationship().getId();
    // Create and set drawing part content
    // Take your pick ..
    // .. build it using code
    // drawingPart.setJaxbElement(
    // buildDrawingPartContentUsingCode(imageRelID));
    // .. or build it from an XML string
    drawingPart.setJaxbElement(buildDrawingPartContentFromXmlString(imageRelID));
    // Save the xlsx
    SaveToZipFile saver = new SaveToZipFile(pkg);
    saver.save(outputfilepath);
    System.out.println("\n\n done .. " + outputfilepath);
}
Also used : CTDrawing(org.docx4j.dml.spreadsheetdrawing.CTDrawing) Drawing(org.docx4j.openpackaging.parts.DrawingML.Drawing) SpreadsheetMLPackage(org.docx4j.openpackaging.packages.SpreadsheetMLPackage) PartName(org.docx4j.openpackaging.parts.PartName) WorksheetPart(org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart) Relationship(org.docx4j.relationships.Relationship) SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage) SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile) File(java.io.File)

Example 7 with PartName

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

the class Xlsx4jNewSpreadSheet method main.

public static void main(String[] args) throws Exception {
    // The path to the documents directory.
    String dataDir = Utils.getDataDir(Xlsx4jNewSpreadSheet.class);
    String outputfilepath = dataDir + "newWorksheet_Xlsx4j.xlsx";
    SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
    WorksheetPart sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
    addContent(sheet);
    SaveToZipFile saver = new SaveToZipFile(pkg);
    saver.save(outputfilepath);
    System.out.println("\n\n done .. " + outputfilepath);
}
Also used : SpreadsheetMLPackage(org.docx4j.openpackaging.packages.SpreadsheetMLPackage) PartName(org.docx4j.openpackaging.parts.PartName) WorksheetPart(org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart) SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile)

Example 8 with PartName

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

the class Xlsx4jHeightAdjustment method main.

/**
 * @param args
 * @throws JAXBException
 * @throws Docx4JException
 */
public static void main(String[] args) throws JAXBException, Docx4JException {
    // The path to the documents directory.
    String dataDir = Utils.getDataDir(Xlsx4jHeightAdjustment.class);
    // TODO Auto-generated method stub
    SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
    WorksheetPart sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
    CTSheetFormatPr format = Context.getsmlObjectFactory().createCTSheetFormatPr();
    format.setDefaultRowHeight(5);
    format.setCustomHeight(Boolean.TRUE);
    sheet.getJaxbElement().setSheetFormatPr(format);
    SheetData sheetData = sheet.getJaxbElement().getSheetData();
    Row row = Context.getsmlObjectFactory().createRow();
    row.setHt(66.0);
    row.setCustomHeight(Boolean.TRUE);
    row.setR(1L);
    Cell cell1 = Context.getsmlObjectFactory().createCell();
    cell1.setV("1234");
    row.getC().add(cell1);
    Cell cell2 = Context.getsmlObjectFactory().createCell();
    cell2.setV("56");
    row.getC().add(cell2);
    sheetData.getRow().add(row);
    SaveToZipFile saver = new SaveToZipFile(pkg);
    saver.save(dataDir + "RowHeight-Xlsx4j.xlsx");
}
Also used : SpreadsheetMLPackage(org.docx4j.openpackaging.packages.SpreadsheetMLPackage) PartName(org.docx4j.openpackaging.parts.PartName) SheetData(org.xlsx4j.sml.SheetData) WorksheetPart(org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart) SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile) Row(org.xlsx4j.sml.Row) CTSheetFormatPr(org.xlsx4j.sml.CTSheetFormatPr) Cell(org.xlsx4j.sml.Cell)

Example 9 with PartName

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

the class Docx4j_SaveDocxImg_S3_Test method saveDocxImg.

/**
 * @Description: 提取word图片
 */
public void saveDocxImg(String filePath, String savePath) throws Exception {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(filePath));
    for (Entry<PartName, Part> entry : wordMLPackage.getParts().getParts().entrySet()) {
        if (entry.getValue() instanceof BinaryPartAbstractImage) {
            BinaryPartAbstractImage binImg = (BinaryPartAbstractImage) entry.getValue();
            // 图片minetype
            String imgContentType = binImg.getContentType();
            PartName pt = binImg.getPartName();
            String fileName = null;
            if (pt.getName().indexOf("word/media/") != -1) {
                fileName = pt.getName().substring(pt.getName().indexOf("word/media/") + "word/media/".length());
            }
            System.out.println(String.format("mimetype=%s,filePath=%s", imgContentType, pt.getName()));
            FileOutputStream fos = new FileOutputStream(savePath + fileName);
            ((BinaryPart) entry.getValue()).writeDataToOutputStream(fos);
            fos.close();
        }
    }
}
Also used : BinaryPart(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart) PartName(org.docx4j.openpackaging.parts.PartName) BinaryPart(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart) Part(org.docx4j.openpackaging.parts.Part) FileOutputStream(java.io.FileOutputStream) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage) File(java.io.File)

Example 10 with PartName

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

the class Docx4j_删除所有批注_S3_Test method removeAllComment.

// 这里2个路径可以一致
public void removeAllComment(String filePath, String savePath) throws Exception {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(filePath));
    // 清空comments.xml内容
    Parts parts = wordMLPackage.getParts();
    HashMap<PartName, Part> partMap = parts.getParts();
    CommentsPart commentPart = (CommentsPart) partMap.get(new PartName("/word/comments.xml"));
    Comments comments = commentPart.getContents();
    List<Comment> commentList = comments.getComment();
    for (int i = 0, len = commentList.size(); i < len; i++) {
        commentList.remove(0);
    }
    // 清空document.xml文件中批注
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
    org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getContents();
    Body body = wmlDocumentEl.getBody();
    CommentFinder cf = new CommentFinder();
    new TraversalUtil(body, cf);
    for (Child commentElement : cf.commentElements) {
        System.out.println(commentElement.getClass().getName());
        Object parent = commentElement.getParent();
        List<Object> theList = ((ContentAccessor) parent).getContent();
        boolean removeResult = remove(theList, commentElement);
        System.out.println(removeResult);
    }
    wordMLPackage.save(new FileOutputStream(savePath));
}
Also used : WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) PartName(org.docx4j.openpackaging.parts.PartName) Parts(org.docx4j.openpackaging.parts.Parts) ContentAccessor(org.docx4j.wml.ContentAccessor) Body(org.docx4j.wml.Body) Child(org.jvnet.jaxb2_commons.ppp.Child) Comment(org.docx4j.wml.Comments.Comment) Comments(org.docx4j.wml.Comments) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) CommentsPart(org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart) TraversalUtil(org.docx4j.TraversalUtil) Part(org.docx4j.openpackaging.parts.Part) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) CommentsPart(org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart) FileOutputStream(java.io.FileOutputStream)

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