/** * Convert the document to HTML */ public static void toHTML(WordprocessingMLPackage wmlPackage, String imageDirPath, String imageTargetUri, OutputStream outputStream) throws Docx4JException { StartEvent startEvent = new StartEvent( wmlPackage, WellKnownProcessSteps.HTML_OUT ); startEvent.publish(); HTMLSettings settings = createHTMLSettings(); settings.setWmlPackage(wmlPackage); if (imageDirPath != null) { settings.setImageDirPath(imageDirPath); } if (imageTargetUri != null) { settings.setImageTargetUri(imageTargetUri); } toHTML(settings, outputStream, FLAG_NONE); new EventFinished(startEvent).publish(); }
startEvent.publish(); if (pdfViaFO()) { FOSettings settings = createFOSettings(); settings.setWmlPackage(wmlPackage); settings.setApacheFopMime("application/pdf"); toFO(settings, outputStream, FLAG_NONE); new EventFinished(startEvent).publish(); } else { save(wmlPackage, baos);
public static void main(String[] args) throws Docx4JException { String input_DOCX = System.getProperty("user.dir") + "/Altered2010.docx"; WordprocessingMLPackage wordMLPackage = Docx4J.load(new File(input_DOCX)); UpdateXmlFromDocumentSurface updater = new UpdateXmlFromDocumentSurface(wordMLPackage, false); List<CustomXmlPart> parts = updater.updateCustomXmlParts(); System.out.println( parts.get(0).getXML() ); // or we can save the docx updater.pkg.save(new File(System.getProperty("user.dir") + "/OUT_UpdateXmlFromDocumentSurface.docx")); }
protected static Exporter<FOSettings> getFOExporter(int flags) throws Docx4JException { switch (flags) { case FLAG_EXPORT_PREFER_NONXSL: return FOExporterVisitorGetInstance(); case FLAG_EXPORT_PREFER_XSL: default: return FOExporterXsltGetInstance(); } }
outputStream.flush(); } else { Docx4J.toPDF(wordprocessingMLPackage, outputStream); outputStream.flush(); outputStream.flush(); } else { HTMLSettings htmlSettings = Docx4J.createHTMLSettings(); htmlSettings.setWmlPackage(wordprocessingMLPackage); Docx4J.toHTML(htmlSettings, outputStream, Docx4J.FLAG_NONE); outputStream.flush();
startEvent.publish(); insertXMLData(customXmlPart, xmlDocument); Docx4J.save(tmpMergeResult, outStream); removeSDTs(wmlPackage); removeDefinedCustomXmlParts(wmlPackage, customXmlPart.getItemId());
/** * 传入一个 word 文件,和一个水印文件,给这个 word文件加水印 * @author eko.zhan at 2018年9月17日 上午11:09:56 * @param file * @param imgFile * @throws Docx4JException * @throws JAXBException * @throws IOException */ public void process() throws WatermarkException { try { WordprocessingMLPackage wordMLPackage = Docx4J.load(file); this.wordMLPackage = wordMLPackage; SectPr sectPr = createSectPr(); wordMLPackage.getMainDocumentPart().getContents().getBody().setSectPr(sectPr); Docx4J.save(wordMLPackage, file); } catch (Docx4JException e) { throw new WatermarkException("Docx4JException", e); } catch (JAXBException e) { throw new WatermarkException("JAXBException", e); } catch (IOException e) { throw new WatermarkException("IOException", e); } }
/** * Generate HTML for the entire MainDocumentPart. * @return */ public org.w3c.dom.Document export() { ByteArrayOutputStream outStream = new ByteArrayOutputStream(DEFAULT_OUTPUT_SIZE); Document ret = null; try { Docx4J.toHTML(htmlSettings, outStream, Docx4J.FLAG_EXPORT_PREFER_NONXSL); ret = XmlUtils.getNewDocumentBuilder().parse(new ByteArrayInputStream(outStream.toByteArray())); } catch (Docx4JException e) { log.error("Exception exporting document: " + e.getMessage(), e); } catch (SAXException e) { log.error("Exception parsing document: " + e.getMessage(), e); } catch (IOException e) { log.error("Exception parsing document: " + e.getMessage(), e); } return ret; }
/** * docx文档转换为PDF * * @param wordMLPackage * @param pdfPath PDF文档存储路径 * @throws Exception */ public static void convertDocxToPDF(WordprocessingMLPackage wordMLPackage, String pdfPath) throws Exception { //HashSet<String> features = new HashSet<>(); //features.add(PP_PDF_APACHEFOP_DISABLE_PAGEBREAK_LIST_ITEM); //WordprocessingMLPackage process = Preprocess.process(wordMLPackage, features); FileOutputStream fileOutputStream = new FileOutputStream(pdfPath); Docx4J.toPDF(wordMLPackage, fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); /*FOSettings foSettings = Docx4J.createFOSettings(); foSettings.setWmlPackage(wordMLPackage); Docx4J.toFO(foSettings, fileOutputStream, Docx4J.FLAG_EXPORT_PREFER_XSL);*/ }
try { Docx4J.save(wordMLPackage, tmpDocxFile, Docx4J.FLAG_SAVE_ZIP_FILE); } catch (Exception e) { throw new TocException("Error saving pkg as tmp file; " + e.getMessage(),e);
/** * If the docx4j-export-fo project is present, * we'll use FO for PDF export. * * Otherwise, we'll try to use Plutext's PDF Converter. * * @return * @since 3.3.0 */ public static boolean pdfViaFO() { if (EXPORT_FO_DETECTED==null) { try { Object o = FOExporterVisitorGetInstance(); EXPORT_FO_DETECTED = Boolean.TRUE; } catch (Docx4JException e) { EXPORT_FO_DETECTED = Boolean.FALSE; } } return EXPORT_FO_DETECTED; }
outputStream.flush(); } else { Docx4J.toPDF(wordprocessingMLPackage, outputStream); outputStream.flush(); outputStream.flush(); } else { HTMLSettings htmlSettings = Docx4J.createHTMLSettings(); htmlSettings.setWmlPackage(wordprocessingMLPackage); Docx4J.toHTML(htmlSettings, outputStream, Docx4J.FLAG_NONE); outputStream.flush();
startEvent.publish(); insertXMLData(customXmlPart, xmlDocument); Docx4J.save(tmpMergeResult, outStream); removeSDTs(wmlPackage); removeDefinedCustomXmlParts(wmlPackage, customXmlPart.getItemId());
htmlSettings.setWmlPackage(wmlPackage); Docx4J.toHTML(htmlSettings, outStream, Docx4J.FLAG_EXPORT_PREFER_XSL);
@Override public void convert() throws Exception{ loading(); InputStream iStream = inStream; WordprocessingMLPackage wordMLPackage = getMLPackage(iStream); processing(); Docx4J.toPDF(wordMLPackage, outStream); finished(); }
/** * 保存当前Docx文件 */ private static void saveDocx(WordprocessingMLPackage wordMLPackage, String savePath) throws FileNotFoundException, Docx4JException { Docx4J.save(wordMLPackage, new File(savePath), Docx4J.FLAG_SAVE_ZIP_FILE); }
protected static Exporter<FOSettings> getFOExporter(int flags) throws Docx4JException { switch (flags) { case FLAG_EXPORT_PREFER_NONXSL: return FOExporterVisitorGetInstance(); case FLAG_EXPORT_PREFER_XSL: default: return FOExporterXsltGetInstance(); } }
/** * If the docx4j-export-fo project is present, * we'll use FO for PDF export. * * Otherwise, we'll try to use Plutext's PDF Converter. * * @return * @since 3.3.0 */ public static boolean pdfViaFO() { if (EXPORT_FO_DETECTED==null) { try { Object o = FOExporterVisitorGetInstance(); EXPORT_FO_DETECTED = Boolean.TRUE; } catch (Docx4JException e) { EXPORT_FO_DETECTED = Boolean.FALSE; } } return EXPORT_FO_DETECTED; }
/** * Convert the document to HTML */ public static void toHTML(WordprocessingMLPackage wmlPackage, String imageDirPath, String imageTargetUri, OutputStream outputStream) throws Docx4JException { StartEvent startEvent = new StartEvent( wmlPackage, WellKnownProcessSteps.HTML_OUT ); startEvent.publish(); HTMLSettings settings = createHTMLSettings(); settings.setWmlPackage(wmlPackage); if (imageDirPath != null) { settings.setImageDirPath(imageDirPath); } if (imageTargetUri != null) { settings.setImageTargetUri(imageTargetUri); } toHTML(settings, outputStream, FLAG_NONE); new EventFinished(startEvent).publish(); }
startEvent.publish(); if (pdfViaFO()) { FOSettings settings = createFOSettings(); settings.setWmlPackage(wmlPackage); settings.setApacheFopMime("application/pdf"); toFO(settings, outputStream, FLAG_NONE); new EventFinished(startEvent).publish(); } else { save(wmlPackage, baos);