Search in sources :

Example 11 with Docx4JException

use of org.docx4j.openpackaging.exceptions.Docx4JException in project com.revolsys.open by revolsys.

the class XlsxRecordWriter method close.

/**
 * Closes the underlying reader.
 */
@Override
public synchronized void close() {
    if (this.out != null) {
        try {
            final long fieldCount = this.recordDefinition.getFieldCount();
            final String ref = "A1:" + getRef(fieldCount, this.sheetRows.size());
            final TablePart tablePart = new TablePart();
            this.spreadsheetPackage.addTargetPart(tablePart);
            final CTTable table = smlObjectFactory.createCTTable();
            tablePart.setContents(table);
            table.setId(1);
            table.setName("Table1");
            table.setDisplayName(this.recordDefinition.getName());
            table.setRef(ref);
            final CTAutoFilter autoFilter = smlObjectFactory.createCTAutoFilter();
            autoFilter.setRef(ref);
            table.setAutoFilter(autoFilter);
            long columnIndex = 1;
            final CTTableColumns tableColumns = smlObjectFactory.createCTTableColumns();
            tableColumns.setCount(fieldCount);
            table.setTableColumns(tableColumns);
            final List<CTTableColumn> columns = tableColumns.getTableColumn();
            for (final String fieldName : this.recordDefinition.getFieldNames()) {
                final CTTableColumn column = smlObjectFactory.createCTTableColumn();
                column.setId(columnIndex);
                column.setName(fieldName);
                columns.add(column);
                columnIndex++;
            }
            final CTTableStyleInfo tableStyleInfo = smlObjectFactory.createCTTableStyleInfo();
            table.setTableStyleInfo(tableStyleInfo);
            tableStyleInfo.setName("TableStyleMedium14");
            tableStyleInfo.setShowFirstColumn(false);
            tableStyleInfo.setShowLastColumn(false);
            tableStyleInfo.setShowRowStripes(true);
            tableStyleInfo.setShowColumnStripes(false);
            this.sheet.addTargetPart(tablePart, "rId1");
            final Save save = new Save(this.spreadsheetPackage);
            save.save(this.out);
            try {
                this.out.flush();
            } catch (final IOException e) {
            }
        } catch (final Docx4JException e) {
            throw Exceptions.wrap(e);
        } finally {
            FileUtil.closeSilent(this.out);
            this.out = null;
            this.sheet = null;
            this.sheetData = null;
            this.spreadsheetPackage = null;
            this.sheetRows = Collections.emptyList();
        }
    }
}
Also used : CTTableColumns(org.xlsx4j.sml.CTTableColumns) CTAutoFilter(org.xlsx4j.sml.CTAutoFilter) CTTableStyleInfo(org.xlsx4j.sml.CTTableStyleInfo) TablePart(org.docx4j.openpackaging.parts.SpreadsheetML.TablePart) CTTablePart(org.xlsx4j.sml.CTTablePart) CTTable(org.xlsx4j.sml.CTTable) Save(org.docx4j.openpackaging.io3.Save) IOException(java.io.IOException) CTTableColumn(org.xlsx4j.sml.CTTableColumn) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException)

Example 12 with Docx4JException

use of org.docx4j.openpackaging.exceptions.Docx4JException 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)

Example 13 with Docx4JException

use of org.docx4j.openpackaging.exceptions.Docx4JException in project docx4j-template by vindell.

the class PhysicalFontUtils method setWmlPackageFonts.

/**
 * 为 {@link org.docx4j.openpackaging.packages.WordprocessingMLPackage} 配置中文字体;解决中文乱码问题
 */
public static void setWmlPackageFonts(WordprocessingMLPackage wmlPackage) throws Docx4JException {
    try {
        // 字体映射;
        Mapper fontMapper = newFontMapper();
        // 设置文档字体库
        wmlPackage.setFontMapper(fontMapper, true);
    } catch (Exception e) {
        throw new Docx4JException(e.getMessage(), e.getCause());
    }
}
Also used : Mapper(org.docx4j.fonts.Mapper) IdentityPlusMapper(org.docx4j.fonts.IdentityPlusMapper) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException)

Example 14 with Docx4JException

use of org.docx4j.openpackaging.exceptions.Docx4JException in project docx4j-template by vindell.

the class Word_解压_Unzip_S3_Test2 method zipXml.

public void zipXml(String inputfilepath, String outFilePath) throws Exception {
    System.out.println(inputfilepath);
    // Load the docx
    File baseDir = new File(inputfilepath);
    UnzippedPartStore partLoader = new UnzippedPartStore(baseDir);
    final Load3 loader = new Load3(partLoader);
    OpcPackage opc = loader.get();
    // Save it zipped
    ZipPartStore zps = new ZipPartStore();
    zps.setSourcePartStore(opc.getSourcePartStore());
    Save saver = new Save(opc, zps);
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(new File(outFilePath));
        saver.save(fos);
    } catch (FileNotFoundException e) {
        throw new Docx4JException("Couldn't save " + outFilePath, e);
    } finally {
        IOUtils.closeQuietly(fos);
    }
}
Also used : UnzippedPartStore(org.docx4j.openpackaging.io3.stores.UnzippedPartStore) ZipPartStore(org.docx4j.openpackaging.io3.stores.ZipPartStore) FileOutputStream(java.io.FileOutputStream) OpcPackage(org.docx4j.openpackaging.packages.OpcPackage) FileNotFoundException(java.io.FileNotFoundException) Save(org.docx4j.openpackaging.io3.Save) File(java.io.File) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) Load3(org.docx4j.openpackaging.io3.Load3)

Example 15 with Docx4JException

use of org.docx4j.openpackaging.exceptions.Docx4JException in project flexmark-java by vsch.

the class DocxConverterPegdown method main.

public static void main(String[] args) {
    final String markdown = "#Heading\n" + "-----\n" + "paragraph text \n" + "lazy continuation\n" + "\n" + "* list item\n" + "    > block quote\n" + "    lazy continuation\n" + "\n" + "~~~info\n" + "   with uneven indent\n" + "      with uneven indent\n" + "indented code\n" + "~~~ \n" + "\n" + "       with uneven indent\n" + "          with uneven indent\n" + "    indented code\n" + "1. numbered item 1   \n" + "1. numbered item 2   \n" + "1. numbered item 3   \n" + "    - bullet item 1   \n" + "    - bullet item 2   \n" + "    - bullet item 3   \n" + "        1. numbered sub-item 1   \n" + "        1. numbered sub-item 2   \n" + "        1. numbered sub-item 3   \n" + "    \n" + "    ~~~info\n" + "       with uneven indent\n" + "          with uneven indent\n" + "    indented code\n" + "    ~~~ \n" + "    \n" + "           with uneven indent\n" + "              with uneven indent\n" + "        indented code\n" + "";
    System.out.println("markdown\n");
    System.out.println(markdown);
    final Parser PARSER = Parser.builder(OPTIONS).build();
    final DocxRenderer RENDERER = DocxRenderer.builder(OPTIONS).build();
    Node document = PARSER.parse(markdown);
    // to get XML
    String xml = RENDERER.render(document);
    // or to control the package
    final WordprocessingMLPackage template = DocxRenderer.getDefaultTemplate();
    RENDERER.render(document, template);
    File file = new File("/Users/vlad/src/pdf/flexmark-java.docx");
    try {
        template.save(file, Docx4J.FLAG_SAVE_ZIP_FILE);
    } catch (Docx4JException e) {
        e.printStackTrace();
    }
}
Also used : Node(com.vladsch.flexmark.ast.Node) DocxRenderer(com.vladsch.flexmark.docx.converter.internal.DocxRenderer) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) Parser(com.vladsch.flexmark.parser.Parser)

Aggregations

Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)19 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)10 File (java.io.File)7 IOException (java.io.IOException)4 SpecExample (com.vladsch.flexmark.spec.SpecExample)3 FileOutputStream (java.io.FileOutputStream)3 Save (org.docx4j.openpackaging.io3.Save)3 Node (com.vladsch.flexmark.ast.Node)2 DocxRenderer (com.vladsch.flexmark.docx.converter.internal.DocxRenderer)2 Parser (com.vladsch.flexmark.parser.Parser)2 FileNotFoundException (java.io.FileNotFoundException)2 BigInteger (java.math.BigInteger)2 Load3 (org.docx4j.openpackaging.io3.Load3)2 UnzippedPartStore (org.docx4j.openpackaging.io3.stores.UnzippedPartStore)2 ZipPartStore (org.docx4j.openpackaging.io3.stores.ZipPartStore)2 OpcPackage (org.docx4j.openpackaging.packages.OpcPackage)2 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)1 FootnoteBlock (com.vladsch.flexmark.ext.footnotes.FootnoteBlock)1 TrpDoc (eu.transkribus.core.model.beans.TrpDoc)1 ExportCache (eu.transkribus.core.model.builder.ExportCache)1