public static void reverter(String inputfilepath, String instancePath) throws Docx4JException {

    WordprocessingMLPackage instancePkg =
        WordprocessingMLPackage.load(new java.io.File(instancePath));

    OpenDoPEReverter reverter =
        new OpenDoPEReverter(
            WordprocessingMLPackage.load(new java.io.File(inputfilepath)), instancePkg);

    System.out.println("reverted? " + reverter.revert());

    SaveToZipFile saver = new SaveToZipFile(instancePkg);
    saver.save(filepathprefix + "_5_reverted.docx");
    System.out.println("Saved: " + filepathprefix + "_5_reverted.docx");
  }
示例#2
0
  /** Example of how to find an object in document.xml via traversal (as opposed to XPath) */
  public static void main(String[] args) throws Exception {

    String inputfilepath = System.getProperty("user.dir") + "/checkbox.docx";

    WordprocessingMLPackage wordMLPackage =
        WordprocessingMLPackage.load(new java.io.File(inputfilepath));
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

    Finder finder = new Finder(FldChar.class); // <----- change this to suit
    new TraversalUtil(documentPart.getContent(), finder);

    System.out.println("got " + finder.results.size() + " of type " + finder.typeToFind.getName());

    for (Object o : finder.results) {

      Object o2 = XmlUtils.unwrap(o);
      // this is ok, provided the results of the Callback
      // won't be marshalled

      if (o2 instanceof org.docx4j.wml.Text) {

        org.docx4j.wml.Text txt = (org.docx4j.wml.Text) o2;

        System.out.println(txt.getValue());

      } else {
        System.out.println(XmlUtils.marshaltoString(o, true, true));
      }
    }
  }
  public static void main(String[] args) throws Exception {

    try {
      getInputFilePath(args);
    } catch (IllegalArgumentException e) {
      inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/databinding/invoice.docx";
    }

    WordprocessingMLPackage wordMLPackage =
        WordprocessingMLPackage.load(new java.io.File(inputfilepath));

    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
    if (wordMLPackage.getMainDocumentPart().getXPathsPart() == null) {
      throw new Docx4JException("OpenDoPE XPaths part missing");
    } else {
      xPaths = wordMLPackage.getMainDocumentPart().getXPathsPart().getJaxbElement();
      //			System.out.println(XmlUtils.marshaltoString(xPaths, true, true));
    }
    if (wordMLPackage.getMainDocumentPart().getConditionsPart() != null) {
      conditions = wordMLPackage.getMainDocumentPart().getConditionsPart().getJaxbElement();
      //			System.out.println(XmlUtils.marshaltoString(conditions, true, true));
    }

    TraversalUtilContentControlVisitor visitor = new TraversalUtilContentControlVisitor();
    IndentingVisitorCallback contentControlCallback = new IndentingVisitorCallback(visitor);

    visitor.callback = contentControlCallback;

    contentControlCallback.walkJAXBElements(wordMLPackage.getMainDocumentPart().getJaxbElement());
  }
示例#4
0
  public static void main(String[] args) throws Exception {
    String filepath = System.getProperty("user.dir") + "/sample-docs/FontEmbedded.docx";
    WordprocessingMLPackage wordMLPackage =
        WordprocessingMLPackage.load(new java.io.File(filepath));

    wordMLPackage.getMainDocumentPart().getFontTablePart().processEmbeddings();
  }
  /** @param args */
  public static void main(String[] args) throws Exception {

    String inputfilepath =
        System.getProperty("user.dir") + "/sample-docs/word/databinding/invoice.docx";

    String data = System.getProperty("user.dir") + "/sample-docs/word/databinding/invoice-data.xml";

    WordprocessingMLPackage wordMLPackage =
        WordprocessingMLPackage.load(new java.io.File(inputfilepath));

    filepathprefix = inputfilepath.substring(0, inputfilepath.lastIndexOf("."));
    System.out.println(filepathprefix);

    StringBuilder timingSummary = new StringBuilder();

    // Find custom xml item id and inject data_file.xml
    long startTime = System.currentTimeMillis();
    CustomXmlDataStoragePart customXmlDataStoragePart =
        CustomXmlDataStoragePartSelector.getCustomXmlDataStoragePart(wordMLPackage);
    if (customXmlDataStoragePart == null) {
      throw new RuntimeException("no xml");
    }
    customXmlDataStoragePart.getData().setDocument(new FileInputStream(new File(data)));
    long endTime = System.currentTimeMillis();
    timingSummary.append("\nmerge data: " + (endTime - startTime));
    System.out.println("data merged");

    SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
    saver.save(new File(System.getProperty("user.dir") + "/OUT_injected.docx"));

    // Process conditionals and repeats
    startTime = System.currentTimeMillis();
    OpenDoPEHandler odh = new OpenDoPEHandler(wordMLPackage);
    odh.preprocess();
    endTime = System.currentTimeMillis();
    timingSummary.append("OpenDoPEHandler: " + (endTime - startTime));

    //		System.out.println(
    //				XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
    //				);
    saver.save(filepathprefix + "_1_preprocessed.docx");
    System.out.println("Saved: " + filepathprefix + "_1_preprocessed.docx");

    startTime = System.currentTimeMillis();
    OpenDoPEIntegrity odi = new OpenDoPEIntegrity();
    odi.process(wordMLPackage);
    endTime = System.currentTimeMillis();
    timingSummary.append("\nOpenDoPEIntegrity: " + (endTime - startTime));

    //		System.out.println(
    //				XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
    //				);
    saver = new SaveToZipFile(wordMLPackage);
    saver.save(filepathprefix + "_2_integrity.docx");
    System.out.println("Saved: " + filepathprefix + "_2_integrity.docx");

    // Apply the bindings
    saver = new SaveToZipFile(wordMLPackage);

    BindingHandler.setHyperlinkStyle("Hyperlink");
    startTime = System.currentTimeMillis();

    //			AtomicInteger bookmarkId = odh.getNextBookmarkId();
    AtomicInteger bookmarkId = new AtomicInteger();

    BindingHandler bh = new BindingHandler(wordMLPackage);
    bh.setStartingIdForNewBookmarks(bookmarkId);
    bh.applyBindings(wordMLPackage.getMainDocumentPart());

    endTime = System.currentTimeMillis();
    timingSummary.append("\nBindingHandler.applyBindings: " + (endTime - startTime));
    //		System.out.println(
    //				XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
    //				);
    saver.save(filepathprefix + "_3_bound.docx");
    System.out.println("Saved: " + filepathprefix + "_3_bound.docx");

    // Either demonstrate reverter, or stripping of controls;
    // you can't do both. So comment out one or the other.
    //		reverter(inputfilepath, filepathprefix + "_bound.docx");
    //
    // Strip content controls
    startTime = System.currentTimeMillis();
    RemovalHandler rh = new RemovalHandler();
    rh.removeSDTs(wordMLPackage, Quantifier.ALL);
    endTime = System.currentTimeMillis();
    timingSummary.append("\nRemovalHandler: " + (endTime - startTime));

    saver.save(filepathprefix + "_4_stripped.docx");
    System.out.println("Saved: " + filepathprefix + "_4_stripped.docx");

    System.out.println(timingSummary);
  }
示例#6
0
  public static void main(String[] args) throws Exception {

    String inputfilepath = System.getProperty("user.dir") + "/sample-docs/sample-docx.xml";

    WordprocessingMLPackage wordMLPackage =
        WordprocessingMLPackage.load(new java.io.File(inputfilepath));
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

    org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getJaxbElement();
    Body body = wmlDocumentEl.getBody();

    new TraversalUtil(
        body,
        new Callback() {

          String indent = "";

          @Override
          public List<Object> apply(Object o) {

            String text = "";
            if (o instanceof org.docx4j.wml.Text) text = ((org.docx4j.wml.Text) o).getValue();

            System.out.println(indent + o.getClass().getName() + "  \"" + text + "\"");
            return null;
          }

          @Override
          public boolean shouldTraverse(Object o) {
            return true;
          }

          // Depth first
          @Override
          public void walkJAXBElements(Object parent) {

            indent += "    ";

            List children = getChildren(parent);
            if (children != null) {

              for (Object o : children) {

                // if its wrapped in javax.xml.bind.JAXBElement, get its
                // value; this is ok, provided the results of the Callback
                // won't be marshalled
                o = XmlUtils.unwrap(o);

                this.apply(o);

                if (this.shouldTraverse(o)) {
                  walkJAXBElements(o);
                }
              }
            }

            indent = indent.substring(0, indent.length() - 4);
          }

          @Override
          public List<Object> getChildren(Object o) {
            return TraversalUtil.getChildrenImpl(o);
          }
        });
  }
 /** Load a Docx Document from an InputStream */
 public static WordprocessingMLPackage load(InputStream inStream) throws Docx4JException {
   return WordprocessingMLPackage.load(inStream);
 }
 /** Load a Docx Document from a File */
 public static WordprocessingMLPackage load(File inFile) throws Docx4JException {
   return WordprocessingMLPackage.load(inFile);
 }
  /** @param args */
  public static void main(String[] args) throws Exception {

    String inputfilepath =
        System.getProperty("user.dir") + "/sample-docs/word/databinding/invoice2.docx";

    WordprocessingMLPackage wordMLPackage =
        WordprocessingMLPackage.load(new java.io.File(inputfilepath));

    filepathprefix = inputfilepath.substring(0, inputfilepath.lastIndexOf("."));
    System.out.println(filepathprefix);

    StringBuilder timingSummary = new StringBuilder();

    // Process conditionals and repeats
    long startTime = System.currentTimeMillis();
    OpenDoPEHandler odh = new OpenDoPEHandler(wordMLPackage);
    odh.preprocess();
    long endTime = System.currentTimeMillis();
    timingSummary.append("OpenDoPEHandler: " + (endTime - startTime));

    System.out.println(
        XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true));
    SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
    saver.save(filepathprefix + "_1_preprocessed.docx");
    System.out.println("Saved: " + filepathprefix + "_1_preprocessed.docx");

    startTime = System.currentTimeMillis();
    OpenDoPEIntegrity odi = new OpenDoPEIntegrity();
    odi.process(wordMLPackage);
    endTime = System.currentTimeMillis();
    timingSummary.append("\nOpenDoPEIntegrity: " + (endTime - startTime));

    System.out.println(
        XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true));
    saver = new SaveToZipFile(wordMLPackage);
    saver.save(filepathprefix + "_2_integrity.docx");
    System.out.println("Saved: " + filepathprefix + "_2_integrity.docx");

    // Apply the bindings
    BindingHandler.setHyperlinkStyle("Hyperlink");
    startTime = System.currentTimeMillis();
    BindingHandler.applyBindings(wordMLPackage.getMainDocumentPart());
    endTime = System.currentTimeMillis();
    timingSummary.append("\nBindingHandler.applyBindings: " + (endTime - startTime));
    System.out.println(
        XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true));
    saver.save(filepathprefix + "_3_bound.docx");
    System.out.println("Saved: " + filepathprefix + "_3_bound.docx");

    // Either demonstrate reverter, or stripping of controls;
    // you can't do both. So comment out one or the other.
    //		reverter(inputfilepath, filepathprefix + "_bound.docx");
    //
    // Strip content controls
    startTime = System.currentTimeMillis();
    RemovalHandler rh = new RemovalHandler();
    rh.removeSDTs(wordMLPackage, Quantifier.ALL);
    endTime = System.currentTimeMillis();
    timingSummary.append("\nRemovalHandler: " + (endTime - startTime));

    saver.save(filepathprefix + "_4_stripped.docx");
    System.out.println("Saved: " + filepathprefix + "_4_stripped.docx");

    System.out.println(timingSummary);
  }