Search in sources :

Example 6 with SaveToZipFile

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

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

the class ContentControlsMergeXML281 method main.

public static void main(String[] args) throws Exception {
    // the docx 'template'
    String input_DOCX = System.getProperty("user.dir") + "/sample-docs/word/databinding/binding-simple.docx";
    // the instance data
    String input_XML = System.getProperty("user.dir") + "/sample-docs/word/databinding/binding-simple-data.xml";
    // resulting docx
    String OUTPUT_DOCX = System.getProperty("user.dir") + "/OUT_ContentControlsMergeXML.docx";
    // Load input_template.docx
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(input_DOCX));
    // Inject data_file.xml
    // (this code assumes it is not a StandardisedAnswersPart)
    CustomXmlDataStoragePart customXmlDataStoragePart = CustomXmlDataStoragePartSelector.getCustomXmlDataStoragePart(wordMLPackage);
    if (customXmlDataStoragePart == null) {
        System.out.println("Couldn't find CustomXmlDataStoragePart! exiting..");
        return;
    }
    System.out.println("Getting " + input_XML);
    FileInputStream fis = new FileInputStream(new File(input_XML));
    customXmlDataStoragePart.getData().setDocument(fis);
    SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
    OpenDoPEHandler odh = null;
    try {
        // Process conditionals and repeats
        odh = new OpenDoPEHandler(wordMLPackage);
        odh.preprocess();
        OpenDoPEIntegrity odi = new OpenDoPEIntegrity();
        odi.process(wordMLPackage);
        if (DEBUG) {
            String save_preprocessed;
            if (OUTPUT_DOCX.lastIndexOf(".") == -1) {
                save_preprocessed = OUTPUT_DOCX + "_INT.docx";
            } else {
                save_preprocessed = OUTPUT_DOCX.substring(0, OUTPUT_DOCX.lastIndexOf(".")) + "_INT.docx";
            }
            // System.out.println(
            // XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
            // );
            saver.save(save_preprocessed);
            System.out.println("Saved: " + save_preprocessed);
        }
    } catch (Docx4JException d) {
        // Probably this docx doesn't contain OpenDoPE convention parts
        System.out.println(d.getMessage());
    }
    // Apply the bindings
    BindingHandler.setHyperlinkStyle("Hyperlink");
    // For docx4j <= 3.2.0
    // BindingHandler.applyBindings(wordMLPackage.getMainDocumentPart());
    // For docx4j > 3.2.0, replace that with:
    AtomicInteger bookmarkId = odh.getNextBookmarkId();
    BindingHandler bh = new BindingHandler(wordMLPackage);
    bh.setStartingIdForNewBookmarks(bookmarkId);
    bh.applyBindings(wordMLPackage.getMainDocumentPart());
    // If you inspect the output, you should see your data in 2 places:
    // 1. the custom xml part
    // 2. (more importantly) the main document part
    // System.out.println(
    // XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
    // );
    // Strip content controls
    RemovalHandler rh = new RemovalHandler();
    rh.removeSDTs(wordMLPackage, Quantifier.ALL);
    saver.save(OUTPUT_DOCX);
    System.out.println("Saved: " + OUTPUT_DOCX);
}
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) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) 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)

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