Search in sources :

Example 1 with NumberingDefinitionsPart

use of org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart in project Java-Tutorial by gpcodervn.

the class NumberingTest method addNumberingPart.

private void addNumberingPart(MainDocumentPart mdp) throws InvalidFormatException {
    NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
    mdp.addTargetPart(ndp);
    ndp.setJaxbElement(getNumbering());
}
Also used : NumberingDefinitionsPart(org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart)

Example 2 with NumberingDefinitionsPart

use of org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart in project Java-Tutorial by gpcodervn.

the class ConvertInXHTMLDocument method main.

public static void main(String[] args) throws Exception {
    // The input would generally be an XHTML document,
    // but for convenience, this sample can convert a
    // docx to XHTML first (ie round trip).
    String inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/sample-docx.docx";
    // Create an empty docx package
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
    wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
    ndp.unmarshalDefaultNumbering();
    XHTMLImporterImpl xHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
    xHTMLImporter.setHyperlinkStyle("Hyperlink");
    if (inputfilepath.endsWith("html")) {
        // Convert the XHTML, and add it into the empty docx we made
        wordMLPackage.getMainDocumentPart().getContent().addAll(xHTMLImporter.convert(new File(inputfilepath), null));
    } else if (inputfilepath.endsWith("docx")) {
        // Round trip docx -> XHTML -> docx
        WordprocessingMLPackage docx = WordprocessingMLPackage.load(new File(inputfilepath));
        AbstractHtmlExporter exporter = new HtmlExporterNG2();
        // Use file system, so there is somewhere to save images (if any)
        OutputStream os = new java.io.FileOutputStream(inputfilepath + ".html");
        HtmlSettings htmlSettings = new HtmlSettings();
        htmlSettings.setImageDirPath(inputfilepath + "_files");
        htmlSettings.setImageTargetUri(inputfilepath.substring(inputfilepath.lastIndexOf("/") + 1) + "_files");
        javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(os);
        exporter.html(docx, result, htmlSettings);
        // Now after all that, we have XHTML we can convert
        wordMLPackage.getMainDocumentPart().getContent().addAll(xHTMLImporter.convert(new File(inputfilepath + ".html"), null));
    } else {
        return;
    }
    System.out.println(XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true));
    wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/html_output.docx"));
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl) NumberingDefinitionsPart(org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart) OutputStream(java.io.OutputStream) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) HtmlSettings(org.docx4j.convert.out.html.AbstractHtmlExporter.HtmlSettings) AbstractHtmlExporter(org.docx4j.convert.out.html.AbstractHtmlExporter) HtmlExporterNG2(org.docx4j.convert.out.html.HtmlExporterNG2) File(java.io.File)

Example 3 with NumberingDefinitionsPart

use of org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart in project Java-Tutorial by gpcodervn.

the class ConvertInXHTMLURL method main.

public static void main(String[] args) throws Exception {
    // Must tidy first :-(
    // URL url = new URL("http://stackoverflow.com/questions/10887580/how-to-convert-a-webpage-from-an-intranet-wiki-to-an-office-document");
    // URL url = new URL("http://en.wikipedia.org/wiki/Office_Open_XML");
    // URL url = new URL("http://en.wikipedia.org/w/index.php?title=Office_Open_XML&printable=yes");
    URL url = new URL("http://en.wikipedia.org/w/index.php?title=Microsoft_Word&printable=yes");
    // Create an empty docx package
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
    wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
    ndp.unmarshalDefaultNumbering();
    // Convert the XHTML, and add it into the empty docx we made
    XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
    XHTMLImporter.setHyperlinkStyle("Hyperlink");
    wordMLPackage.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert(url));
    System.out.println(XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true));
    wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/OUT_ConvertInXHTMLURL.docx"));
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl) NumberingDefinitionsPart(org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) URL(java.net.URL)

Example 4 with NumberingDefinitionsPart

use of org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart in project flexmark-java by vsch.

the class DocxHelper method inheritPBdr.

/**
 * Try to keep the border of the parent and offset the left border by the difference in indentation between the two so the border stays
 * aligned with the parent. Max offset for border is 31pt or 620tw
 * <p>
 * if the child has its own left border then nothing is done
 * <p>
 * other borders are not affected
 * <p>
 * Must be called after the child indent is set
 */
public void inheritPBdr(PPr child, PPr parent) {
    parent = getResolver().getEffectivePPr(parent);
    PPr styledChild = getResolver().getEffectivePPr(child);
    if (has(parent.getPBdr()) && (!has(styledChild.getPBdr()) || !has(styledChild.getPBdr().getLeft()) && has(parent.getPBdr().getLeft()))) {
        PPrBase.Ind cInd = getCopy(styledChild.getInd(), true);
        PPrBase.Ind pInd = getCopy(parent.getInd(), false);
        CTBorder leftBorder = getCopy(parent.getPBdr().getLeft(), true);
        final PPrBase.NumPr numPr = styledChild.getNumPr();
        if (numPr != null) {
            // need to check that too, it may have settings we don't have
            NumberingDefinitionsPart ndp = myDocumentPart.getNumberingDefinitionsPart();
            if (ndp != null) {
                final PPrBase.Ind ndpInd = ndp.getInd(numPr);
                if (ndpInd != null) {
                    if (cInd.getLeft() == null && ndpInd.getLeft() != null) {
                        cInd.setLeft(ndpInd.getLeft());
                    }
                    if (cInd.getRight() == null && ndpInd.getRight() != null) {
                        cInd.setRight(ndpInd.getRight());
                    }
                    if (cInd.getHanging() == null && ndpInd.getHanging() != null) {
                        cInd.setHanging(ndpInd.getHanging());
                    }
                }
            }
        }
        // now add difference between the left indents
        final BigInteger indentDiff = safeIndLeft(cInd).subtract(safeIndHanging(cInd)).subtract(safeIndLeft(pInd));
        if (indentDiff.compareTo(ZERO) > 0) {
            final BigInteger[] divideAndRemainder = indentDiff.divideAndRemainder(BigInteger.valueOf(20));
            // convert to points and add to space
            BigInteger space = safeBigInt(leftBorder.getSpace()).add(divideAndRemainder[0]).min(BigInteger.valueOf(31));
            leftBorder.setSpace(space);
            int remainder = divideAndRemainder[1].intValue();
            if (remainder > 0) {
                // need to adjust indent since we could not adjust space exactly or it will appear off to the eye
                ensureInd(child);
                child.getInd().setLeft(ZERO.max(safeIndLeft(cInd).subtract(BigInteger.valueOf(remainder))));
            }
        }
        ensurePBdr(child).setLeft(leftBorder);
    }
}
Also used : NumberingDefinitionsPart(org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart) BigInteger(java.math.BigInteger)

Example 5 with NumberingDefinitionsPart

use of org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart in project flexmark-java by vsch.

the class DocxHelper method inheritInd.

/**
 * @param child  PPr of child element to inherit indent
 * @param parent PPr of parent element, must be explicit or effective ppr
 */
public void inheritInd(PPr child, PPr parent) {
    if (parent != null && has(parent.getInd())) {
        PPr styledChild = getResolver().getEffectivePPr(child);
        PPrBase.Ind cInd = getCopy(styledChild == null ? null : styledChild.getInd(), true);
        PPrBase.Ind pInd = parent.getInd();
        final PPrBase.NumPr numPr = styledChild == null ? null : styledChild.getNumPr();
        if (numPr != null) {
            // need to check that too, it may have settings we don't have
            NumberingDefinitionsPart ndp = myDocumentPart.getNumberingDefinitionsPart();
            if (ndp != null) {
                final PPrBase.Ind ndpInd = ndp.getInd(numPr);
                if (ndpInd != null) {
                    if (cInd.getLeft() == null && ndpInd.getLeft() != null) {
                        cInd.setLeft(ndpInd.getLeft());
                    }
                    if (cInd.getRight() == null && ndpInd.getRight() != null) {
                        cInd.setRight(ndpInd.getRight());
                    }
                    if (cInd.getHanging() == null && ndpInd.getHanging() != null) {
                        cInd.setHanging(ndpInd.getHanging());
                    }
                }
            }
        }
        combine(cInd, pInd, CombineBigInt.ADD, CombineBigInt.NONE);
        cInd = keepDiff(cInd, styledChild == null ? null : styledChild.getInd());
        child.setInd(cInd);
    }
}
Also used : NumberingDefinitionsPart(org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart)

Aggregations

NumberingDefinitionsPart (org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart)11 XHTMLImporterImpl (org.docx4j.convert.in.xhtml.XHTMLImporterImpl)4 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)4 File (java.io.File)3 OutputStream (java.io.OutputStream)2 BigInteger (java.math.BigInteger)2 AbstractHtmlExporter (org.docx4j.convert.out.html.AbstractHtmlExporter)2 HtmlSettings (org.docx4j.convert.out.html.AbstractHtmlExporter.HtmlSettings)2 HtmlExporterNG2 (org.docx4j.convert.out.html.HtmlExporterNG2)2 AttributesNode (com.vladsch.flexmark.ext.attributes.AttributesNode)1 URL (java.net.URL)1 Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)1 PPrBase (org.docx4j.wml.PPrBase)1 RFonts (org.docx4j.wml.RFonts)1