|
57 | 57 | import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
|
58 | 58 | import org.docx4j.relationships.Relationship;
|
59 | 59 | import org.docx4j.utils.ResourceUtils;
|
| 60 | +import org.docx4j.w14.CTSdtCheckbox; |
| 61 | +import org.docx4j.w14.CTSdtCheckboxSymbol; |
60 | 62 | import org.docx4j.wml.CTAltChunk;
|
61 | 63 | import org.docx4j.wml.CTBookmark;
|
| 64 | +import org.docx4j.wml.CTDataBinding; |
62 | 65 | import org.docx4j.wml.CTSdtDate;
|
63 | 66 | import org.docx4j.wml.Color;
|
64 | 67 | import org.docx4j.wml.P;
|
65 | 68 | import org.docx4j.wml.R;
|
| 69 | +import org.docx4j.wml.RFonts; |
66 | 70 | import org.docx4j.wml.RPr;
|
67 | 71 | import org.docx4j.wml.SdtPr;
|
68 | 72 | import org.docx4j.wml.Style;
|
@@ -1068,7 +1072,7 @@ public static DocumentFragment xpathInjectImage(WordprocessingMLPackage wmlPacka
|
1068 | 1072 |
|
1069 | 1073 |
|
1070 | 1074 | // Now add the inline in w:p/w:r/w:drawing
|
1071 |
| - org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory(); |
| 1075 | + org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory(); |
1072 | 1076 | org.docx4j.wml.Tc tc = factory.createTc();
|
1073 | 1077 | org.docx4j.wml.P p = factory.createP();
|
1074 | 1078 | if (sdtParent.equals("tr")) {
|
@@ -1258,7 +1262,7 @@ public static DocumentFragment nullResultParagraph(String sdtParent, String mess
|
1258 | 1262 |
|
1259 | 1263 | try
|
1260 | 1264 | {
|
1261 |
| - org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory(); |
| 1265 | + org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory(); |
1262 | 1266 | org.docx4j.wml.R run = factory.createR();
|
1263 | 1267 | org.docx4j.wml.Text text = factory.createText();
|
1264 | 1268 | text.setValue(message);
|
@@ -1346,7 +1350,7 @@ public static DocumentFragment xpathDate(WordprocessingMLPackage wmlPackage,
|
1346 | 1350 | }
|
1347 | 1351 |
|
1348 | 1352 | Format formatter = new SimpleDateFormat(format);
|
1349 |
| - org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory(); |
| 1353 | + org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory(); |
1350 | 1354 |
|
1351 | 1355 | Date date;
|
1352 | 1356 | RPr rPr = null;
|
@@ -1497,6 +1501,156 @@ public static DocumentFragment convertFlatOPC(
|
1497 | 1501 |
|
1498 | 1502 | }
|
1499 | 1503 |
|
| 1504 | + /** |
| 1505 | + * Support for w14 checkbox. |
| 1506 | + * |
| 1507 | + * @since 3.2.2 |
| 1508 | + */ |
| 1509 | + public static DocumentFragment w14Checkbox(WordprocessingMLPackage wmlPackage, |
| 1510 | + JaxbXmlPart sourcePart, |
| 1511 | + Map<String, CustomXmlPart> customXmlDataStorageParts, |
| 1512 | + NodeIterator sdtPrNodeIt, |
| 1513 | + String sdtParent, |
| 1514 | + String contentChild) { |
| 1515 | + |
| 1516 | + SdtPr sdtPr = null; |
| 1517 | + Node sdtPrNode = sdtPrNodeIt.nextNode(); |
| 1518 | + if (sdtPrNode==null) { |
| 1519 | + log.error("Couldn't get sdtPr!"); |
| 1520 | + return null; |
| 1521 | + } else { |
| 1522 | + try { |
| 1523 | + sdtPr = (SdtPr)XmlUtils.unmarshal(sdtPrNode, Context.jc, SdtPr.class); |
| 1524 | + } catch (JAXBException e) { |
| 1525 | + log.error(e.getMessage(), e); |
| 1526 | + } |
| 1527 | + } |
| 1528 | + |
| 1529 | + /* |
| 1530 | + <w14:checkbox> |
| 1531 | + <w14:checked w14:val="0"/> |
| 1532 | + <w14:checkedState w14:val="2612" w14:font="MS Gothic"/> |
| 1533 | + <w14:uncheckedState w14:val="2610" w14:font="MS Gothic"/> |
| 1534 | + </w14:checkbox> |
| 1535 | + */ |
| 1536 | + CTSdtCheckbox sdtCheckbox = (CTSdtCheckbox)sdtPr.getByClass(CTSdtCheckbox.class); |
| 1537 | + |
| 1538 | + CTDataBinding dataBinding = sdtPr.getDataBinding(); |
| 1539 | + CustomXmlPart part = customXmlDataStorageParts.get(dataBinding.getStoreItemID().toLowerCase()); |
| 1540 | + |
| 1541 | + if (part==null) { |
| 1542 | + log.error("Couldn't locate part by storeItemId " + dataBinding.getStoreItemID()); |
| 1543 | + return null; |
| 1544 | + } |
| 1545 | + |
| 1546 | + try { |
| 1547 | + String r = part.xpathGetString(dataBinding.getXpath(), dataBinding.getPrefixMappings()); |
| 1548 | + log.debug(dataBinding.getXpath() + " yielded result " + r); |
| 1549 | + if (r==null) return nullResultParagraph(sdtParent, "[missing!]"); |
| 1550 | + |
| 1551 | + org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory(); |
| 1552 | + |
| 1553 | + org.docx4j.wml.Text text = factory.createText(); |
| 1554 | + |
| 1555 | + // At present we ignore the checkedState and uncheckedState, except to warn.. |
| 1556 | + if (r.equals("true") || r.equals("1")) { |
| 1557 | + |
| 1558 | + if (log.isWarnEnabled() && sdtCheckbox.getCheckedState()!=null) { |
| 1559 | + CTSdtCheckboxSymbol sdtCheckboxSymbol = sdtCheckbox.getCheckedState(); |
| 1560 | + if (sdtCheckboxSymbol.getVal()!=null |
| 1561 | + && !sdtCheckboxSymbol.getVal().equals("2612") ) { |
| 1562 | + log.warn("TODO: handle checkedState " + sdtCheckboxSymbol.getVal()); |
| 1563 | + } |
| 1564 | + } |
| 1565 | + text.setValue("☒"); |
| 1566 | + |
| 1567 | + } else { // Word treats everything else as false |
| 1568 | + |
| 1569 | + if (log.isWarnEnabled() && sdtCheckbox.getUncheckedState()!=null) { |
| 1570 | + CTSdtCheckboxSymbol sdtCheckboxSymbol = sdtCheckbox.getUncheckedState(); |
| 1571 | + if (sdtCheckboxSymbol.getVal()!=null |
| 1572 | + && !sdtCheckboxSymbol.getVal().equals("2610") ) { |
| 1573 | + log.warn("TODO: handle uncheckedState " + sdtCheckboxSymbol.getVal()); |
| 1574 | + } |
| 1575 | + } |
| 1576 | + |
| 1577 | + text.setValue("☐"); |
| 1578 | + } |
| 1579 | + |
| 1580 | + /* |
| 1581 | + <w:p> |
| 1582 | + <w:r> |
| 1583 | + <w:rPr> |
| 1584 | + <w:rFonts w:ascii="MS Gothic" w:eastAsia="MS Gothic" w:hAnsi="MS Gothic" w:hint="eastAsia"/> |
| 1585 | + </w:rPr> |
| 1586 | + <w:t>☐</w:t> |
| 1587 | + </w:r> |
| 1588 | + </w:p> |
| 1589 | + */ |
| 1590 | + org.docx4j.wml.P p = factory.createP(); |
| 1591 | + |
| 1592 | + org.docx4j.wml.R run = factory.createR(); |
| 1593 | + RPr rpr = factory.createRPr(); |
| 1594 | + RFonts rfonts = factory.createRFonts(); |
| 1595 | + rpr.setRFonts(rfonts); |
| 1596 | + rfonts.setEastAsia( "MS Gothic"); |
| 1597 | + rfonts.setHint(org.docx4j.wml.STHint.EAST_ASIA); |
| 1598 | + rfonts.setHAnsi( "MS Gothic"); |
| 1599 | + rfonts.setAscii( "MS Gothic"); |
| 1600 | + run.setRPr(rpr); |
| 1601 | + |
| 1602 | + run.getContent().add(text); |
| 1603 | + |
| 1604 | + org.docx4j.wml.Tc tc = factory.createTc(); |
| 1605 | + if (sdtParent.equals("tr")) { |
| 1606 | + tc.getContent().add(p); |
| 1607 | + } |
| 1608 | + |
| 1609 | + if (sdtParent.equals("body") |
| 1610 | + || sdtParent.equals("tr") |
| 1611 | + || sdtParent.equals("tc") ) { |
| 1612 | + p.getContent().add(run); |
| 1613 | + } |
| 1614 | + |
| 1615 | + Document document = null; |
| 1616 | + |
| 1617 | + if (sdtParent.equals("body") |
| 1618 | + || sdtParent.equals("tc") ) { |
| 1619 | + document = XmlUtils.marshaltoW3CDomDocument(p); |
| 1620 | + log.debug(XmlUtils.marshaltoString(p, true, true)); |
| 1621 | + } else if ( sdtParent.equals("tr") ) { |
| 1622 | + document = XmlUtils.marshaltoW3CDomDocument(tc); |
| 1623 | + log.debug(XmlUtils.marshaltoString(tc, true, true)); |
| 1624 | + } else if ( sdtParent.equals("p") ) { |
| 1625 | + document = XmlUtils.marshaltoW3CDomDocument(run); |
| 1626 | + log.debug(XmlUtils.marshaltoString(run, true, true)); |
| 1627 | + } else if ( sdtParent.equals("sdtContent") ) { |
| 1628 | + log.info("contentChild: " + contentChild); |
| 1629 | + if (contentChild.equals("p")) { |
| 1630 | + p.getContent().add(run); |
| 1631 | + document = XmlUtils.marshaltoW3CDomDocument(p); |
| 1632 | + log.debug(XmlUtils.marshaltoString(p, true, true)); |
| 1633 | + } else if (contentChild.equals("r")) { |
| 1634 | + document = XmlUtils.marshaltoW3CDomDocument(run); |
| 1635 | + log.debug(XmlUtils.marshaltoString(run, true, true)); |
| 1636 | + } else { |
| 1637 | + log.error("how to inject checkbox for unexpected sdt's content: " + contentChild); |
| 1638 | + } |
| 1639 | + } else { |
| 1640 | + log.error("how to inject checkbox for unexpected sdt's parent: " + sdtParent); |
| 1641 | + } |
| 1642 | + |
| 1643 | + DocumentFragment docfrag = document.createDocumentFragment(); |
| 1644 | + docfrag.appendChild(document.getDocumentElement()); |
| 1645 | + |
| 1646 | + return docfrag; |
| 1647 | + |
| 1648 | + } catch (Exception e) { |
| 1649 | + e.printStackTrace(); |
| 1650 | + return null; |
| 1651 | + } |
| 1652 | + } |
| 1653 | + |
1500 | 1654 | // TODO - add something like this to RelationshipsPart??
|
1501 | 1655 | private static PartName getNewPartName(String prefix, String suffix,
|
1502 | 1656 | RelationshipsPart rp) throws InvalidFormatException {
|
|
0 commit comments