Search in sources :

Example 1 with SaveToZipFile

use of org.docx4j.openpackaging.io.SaveToZipFile in project Java-Tutorial by gpcodervn.

the class ContentControlBindingExtensions method reverter.

public static void reverter(String inputfilepath, String instancePath) throws Docx4JException {
    WordprocessingMLPackage instancePkg = WordprocessingMLPackage.load(new java.io.File(instancePath));
    OpenDoPEReverter reverter = new OpenDoPEReverter(WordprocessingMLPackage.load(new java.io.File(inputfilepath)), instancePkg);
    System.out.println("reverted? " + reverter.revert());
    SaveToZipFile saver = new SaveToZipFile(instancePkg);
    saver.save(filepathprefix + "_5_reverted.docx");
    System.out.println("Saved: " + filepathprefix + "_5_reverted.docx");
}
Also used : SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile) File(java.io.File) OpenDoPEReverter(org.docx4j.model.datastorage.OpenDoPEReverter)

Example 2 with SaveToZipFile

use of org.docx4j.openpackaging.io.SaveToZipFile in project Java-Tutorial by gpcodervn.

the class ContentControlBindingExtensions method main.

/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    String inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/databinding/invoice.docx";
    String data = System.getProperty("user.dir") + "/sample-docs/word/databinding/invoice-data.xml";
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
    filepathprefix = inputfilepath.substring(0, inputfilepath.lastIndexOf("."));
    System.out.println(filepathprefix);
    StringBuilder timingSummary = new StringBuilder();
    // Find custom xml item id and inject data_file.xml
    long startTime = System.currentTimeMillis();
    CustomXmlDataStoragePart customXmlDataStoragePart = CustomXmlDataStoragePartSelector.getCustomXmlDataStoragePart(wordMLPackage);
    if (customXmlDataStoragePart == null) {
        throw new RuntimeException("no xml");
    }
    customXmlDataStoragePart.getData().setDocument(new FileInputStream(new File(data)));
    long endTime = System.currentTimeMillis();
    timingSummary.append("\nmerge data: " + (endTime - startTime));
    System.out.println("data merged");
    SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
    saver.save(new File(System.getProperty("user.dir") + "/OUT_injected.docx"));
    // Process conditionals and repeats
    startTime = System.currentTimeMillis();
    OpenDoPEHandler odh = new OpenDoPEHandler(wordMLPackage);
    odh.preprocess();
    endTime = System.currentTimeMillis();
    timingSummary.append("OpenDoPEHandler: " + (endTime - startTime));
    // System.out.println(
    // XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
    // );
    saver.save(filepathprefix + "_1_preprocessed.docx");
    System.out.println("Saved: " + filepathprefix + "_1_preprocessed.docx");
    startTime = System.currentTimeMillis();
    OpenDoPEIntegrity odi = new OpenDoPEIntegrity();
    odi.process(wordMLPackage);
    endTime = System.currentTimeMillis();
    timingSummary.append("\nOpenDoPEIntegrity: " + (endTime - startTime));
    // System.out.println(
    // XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
    // );
    saver = new SaveToZipFile(wordMLPackage);
    saver.save(filepathprefix + "_2_integrity.docx");
    System.out.println("Saved: " + filepathprefix + "_2_integrity.docx");
    // Apply the bindings
    saver = new SaveToZipFile(wordMLPackage);
    BindingHandler.setHyperlinkStyle("Hyperlink");
    startTime = System.currentTimeMillis();
    // AtomicInteger bookmarkId = odh.getNextBookmarkId();
    AtomicInteger bookmarkId = new AtomicInteger();
    BindingHandler bh = new BindingHandler(wordMLPackage);
    bh.setStartingIdForNewBookmarks(bookmarkId);
    bh.applyBindings(wordMLPackage.getMainDocumentPart());
    endTime = System.currentTimeMillis();
    timingSummary.append("\nBindingHandler.applyBindings: " + (endTime - startTime));
    // System.out.println(
    // XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
    // );
    saver.save(filepathprefix + "_3_bound.docx");
    System.out.println("Saved: " + filepathprefix + "_3_bound.docx");
    // Either demonstrate reverter, or stripping of controls;
    // you can't do both. So comment out one or the other.
    // reverter(inputfilepath, filepathprefix + "_bound.docx");
    // 
    // Strip content controls
    startTime = System.currentTimeMillis();
    RemovalHandler rh = new RemovalHandler();
    rh.removeSDTs(wordMLPackage, Quantifier.ALL);
    endTime = System.currentTimeMillis();
    timingSummary.append("\nRemovalHandler: " + (endTime - startTime));
    saver.save(filepathprefix + "_4_stripped.docx");
    System.out.println("Saved: " + filepathprefix + "_4_stripped.docx");
    System.out.println(timingSummary);
}
Also used : OpenDoPEIntegrity(org.docx4j.model.datastorage.OpenDoPEIntegrity) SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile) RemovalHandler(org.docx4j.model.datastorage.RemovalHandler) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) FileInputStream(java.io.FileInputStream) OpenDoPEHandler(org.docx4j.model.datastorage.OpenDoPEHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CustomXmlDataStoragePart(org.docx4j.openpackaging.parts.CustomXmlDataStoragePart) File(java.io.File) SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile) BindingHandler(org.docx4j.model.datastorage.BindingHandler)

Example 3 with SaveToZipFile

use of org.docx4j.openpackaging.io.SaveToZipFile 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 4 with SaveToZipFile

use of org.docx4j.openpackaging.io.SaveToZipFile 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 5 with SaveToZipFile

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

the class Xlsx4jOpenSaveSpreadSheet method main.

/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    // The path to the documents directory.
    String dataDir = Utils.getDataDir(Xlsx4jOpenSaveSpreadSheet.class);
    String inputfilepath = dataDir + "pivot.xlsm";
    boolean save = true;
    String outputfilepath = dataDir + "pivot-rtt-xlsx4j.xlsm";
    // Open a document from the file system
    // 1. Load the Package
    OpcPackage pkg = OpcPackage.load(new java.io.File(inputfilepath));
    // Save it
    if (save) {
        SaveToZipFile saver = new SaveToZipFile(pkg);
        saver.save(outputfilepath);
    }
}
Also used : OpcPackage(org.docx4j.openpackaging.packages.OpcPackage) SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile)

Aggregations

SaveToZipFile (org.docx4j.openpackaging.io.SaveToZipFile)7 File (java.io.File)4 SpreadsheetMLPackage (org.docx4j.openpackaging.packages.SpreadsheetMLPackage)3 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)3 PartName (org.docx4j.openpackaging.parts.PartName)3 WorksheetPart (org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart)3 FileInputStream (java.io.FileInputStream)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 BindingHandler (org.docx4j.model.datastorage.BindingHandler)2 OpenDoPEHandler (org.docx4j.model.datastorage.OpenDoPEHandler)2 OpenDoPEIntegrity (org.docx4j.model.datastorage.OpenDoPEIntegrity)2 RemovalHandler (org.docx4j.model.datastorage.RemovalHandler)2 CustomXmlDataStoragePart (org.docx4j.openpackaging.parts.CustomXmlDataStoragePart)2 CTDrawing (org.docx4j.dml.spreadsheetdrawing.CTDrawing)1 OpenDoPEReverter (org.docx4j.model.datastorage.OpenDoPEReverter)1 Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)1 OpcPackage (org.docx4j.openpackaging.packages.OpcPackage)1 Drawing (org.docx4j.openpackaging.parts.DrawingML.Drawing)1 BinaryPartAbstractImage (org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)1 Relationship (org.docx4j.relationships.Relationship)1