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")); }
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")); }
/** * 传入一个 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); } }