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();
}
}
}
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);
}
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());
}
}
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);
}
}
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();
}
}
Aggregations