如何正确复制文本字段?
How to correctly make a copy of a textfield?
我的问题是我无法使用 document.getContent()
和 document.getContents()
获取我的文本字段。
所以我尝试使用 XPath 来 selecting 对象,它有效但我无法复制对象并再次添加它。
例如:
XPath
Exception
Comment
//wps:txbx/w:txbxContent
javax.xml.bind.MarshalException
SAXException2, Missing @XmlRootElement-Annotation
//wps:txbx/w:txbxContent/w:p/w:r
javax.xml.transform.TransformerException
unexpected Element, because it is on the wrong place
我也尝试创建一个自己的对象,但这对我也不起作用,因为它不被接受为 JAXB 对象。
这是我的代码:
public static void main(final String[] args) throws Docx4JException, JAXBException
{
final String filePath = "./files/GetTextfield.docx";
final WordprocessingMLPackage word = Docx4J.load(new File(filePath));
final MainDocumentPart document = word.getMainDocumentPart();
final String xpathExpr = "//wps:txbx/w:txbxContent/w:p/w:r";
final List<Object> nodes = document.getJAXBNodesViaXPath(xpathExpr, false);
addPageBreak(document);
final XmlUtils xmlUtils = new XmlUtils();
final Object field = nodes.get(0);
final Object copy = xmlUtils.deepCopy(field);
document.addObject(copy);
System.out.println(document.getXML());
// TODO Before MailMerge need to copy template content to all pages
}
我重新创建了这个问题,因为我猜 the old one 有点令人困惑。
我的文档为 XML (document.getXML())
<w:document mc:Ignorable="w14 w15 wp14" xmlns:cppr="http://schemas.microsoft.com/office/2006/coverPageProps" xmlns:dgm1611="http://schemas.microsoft.com/office/drawing/2016/11/diagram" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wp15="http://schemas.microsoft.com/office/word/2012/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:a1611="http://schemas.microsoft.com/office/drawing/2016/11/main" xmlns:a16svg="http://schemas.microsoft.com/office/drawing/2016/SVG/main" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:pvml="urn:schemas-microsoft-com:office:powerpoint" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:dgm1612="http://schemas.microsoft.com/office/drawing/2016/12/diagram" xmlns:comp="http://schemas.openxmlformats.org/drawingml/2006/compatibility" xmlns:xvml="urn:schemas-microsoft-com:office:excel" xmlns:c173="http://schemas.microsoft.com/office/drawing/2017/03/chart" xmlns:anam3d="http://schemas.microsoft.com/office/drawing/2018/animation/model3d" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:adec="http://schemas.microsoft.com/office/drawing/2017/decorative" xmlns:oda="http://opendope.org/answers" xmlns:a18hc="http://schemas.microsoft.com/office/drawing/2018/hyperlinkcolor" xmlns:odc="http://opendope.org/conditions" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:cdr="http://schemas.openxmlformats.org/drawingml/2006/chartDrawing" xmlns:odi="http://opendope.org/components" xmlns:msink="http://schemas.microsoft.com/ink/2010/main" xmlns:cdr14="http://schemas.microsoft.com/office/drawing/2010/chartDrawing" xmlns:iact="http://schemas.microsoft.com/office/powerpoint/2014/inkAction" xmlns:an18="http://schemas.microsoft.com/office/drawing/2018/animation" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:odq="http://opendope.org/questions" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:dsp="http://schemas.microsoft.com/office/drawing/2008/diagram" xmlns:odx="http://opendope.org/xpaths" xmlns:a15="http://schemas.microsoft.com/office/drawing/2012/main" xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" xmlns:c15="http://schemas.microsoft.com/office/drawing/2012/chart" xmlns:a13cmd="http://schemas.microsoft.com/office/drawing/2013/main/command" xmlns:c14="http://schemas.microsoft.com/office/drawing/2007/8/2/chart" xmlns:a16="http://schemas.microsoft.com/office/drawing/2014/main" xmlns:odgm="http://opendope.org/SmartArt/DataHierarchy" xmlns:c16="http://schemas.microsoft.com/office/drawing/2014/chart" xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:thm15="http://schemas.microsoft.com/office/thememl/2012/main" xmlns:we="http://schemas.microsoft.com/office/webextensions/webextension/2010/11" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:ns39="http://www.w3.org/2003/InkML" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:ns38="http://www.w3.org/1998/Math/MathML" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:dgm14="http://schemas.microsoft.com/office/drawing/2010/diagram" xmlns:c16ac="http://schemas.microsoft.com/office/drawing/2014/chart/ac" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:b="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:cs="http://schemas.microsoft.com/office/drawing/2012/chartStyle" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:pic14="http://schemas.microsoft.com/office/drawing/2010/picture" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:lc="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas" xmlns:wetp="http://schemas.microsoft.com/office/webextensions/taskpanes/2010/11">
<w:body>
<w:p w:rsidRPr="009670CB" w:rsidR="00CF457B" w:rsidP="009670CB" w:rsidRDefault="00BD5B37">
<w:bookmarkStart w:name="_GoBack" w:id="0"/>
<w:bookmarkEnd w:id="0"/>
<w:r>
<w:rPr>
<w:noProof/>
</w:rPr>
<mc:AlternateContent>
<mc:Choice Requires="wps">
<w:drawing>
<wp:anchor distT="45720" distB="45720" distL="114300" distR="114300" simplePos="false" relativeHeight="251659264" behindDoc="false" locked="false" layoutInCell="true" allowOverlap="true">
<wp:simplePos x="0" y="0"/>
<wp:positionH relativeFrom="column">
<wp:posOffset>-256540</wp:posOffset>
</wp:positionH>
<wp:positionV relativeFrom="paragraph">
<wp:posOffset>0</wp:posOffset>
</wp:positionV>
<wp:extent cx="2360930" cy="1404620"/>
<wp:effectExtent l="0" t="0" r="22860" b="11430"/>
<wp:wrapSquare wrapText="bothSides"/>
<wp:docPr id="217" name="Textfeld 2"/>
<wp:cNvGraphicFramePr>
<a:graphicFrameLocks/>
</wp:cNvGraphicFramePr>
<a:graphic>
<a:graphicData uri="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
<wps:wsp>
<wps:cNvSpPr txBox="true">
<a:spLocks noChangeArrowheads="true"/>
</wps:cNvSpPr>
<wps:spPr bwMode="auto">
<a:xfrm>
<a:off x="0" y="0"/>
<a:ext cx="2360930" cy="1404620"/>
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst/>
</a:prstGeom>
<a:solidFill>
<a:srgbClr val="FFFFFF"/>
</a:solidFill>
<a:ln w="9525">
<a:solidFill>
<a:srgbClr val="000000"/>
</a:solidFill>
<a:miter lim="800000"/>
<a:headEnd/>
<a:tailEnd/>
</a:ln>
</wps:spPr>
<wps:txbx>
<w:txbxContent>
<w:p w:rsidR="00BD5B37" w:rsidRDefault="00BD5B37">
<w:proofErr w:type="spellStart"/>
<w:r>
<w:t>GetThisContent</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
</w:p>
</w:txbxContent>
</wps:txbx>
<wps:bodyPr rot="0" vert="horz" wrap="square" lIns="91440" tIns="45720" rIns="91440" bIns="45720" anchor="t" anchorCtr="false">
<a:spAutoFit/>
</wps:bodyPr>
</wps:wsp>
</a:graphicData>
</a:graphic>
<wp14:sizeRelH relativeFrom="margin">
<wp14:pctWidth>40000</wp14:pctWidth>
</wp14:sizeRelH>
<wp14:sizeRelV relativeFrom="margin">
<wp14:pctHeight>20000</wp14:pctHeight>
</wp14:sizeRelV>
</wp:anchor>
</w:drawing>
</mc:Choice>
<mc:Fallback>
<w:pict>
<v:shapetype o:spt="202.0" path="m,l,21600r21600,l21600,xe" coordsize="21600,21600" id="_x0000_t202">
<v:stroke joinstyle="miter"/>
<v:path gradientshapeok="t" o:connecttype="rect"/>
</v:shapetype>
<v:shape o:gfxdata="UEsDBBQABgAIAAAAIQC2gziS/gAAAOEBAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbJSRQU7DMBBF90jcwfIWJU67QAgl6YK0S0CoHGBkTxKLZGx5TGhvj5O2G0SRWNoz/78nu9wcxkFMGNg6quQqL6RA0s5Y6ir5vt9lD1JwBDIwOMJKHpHlpr69KfdHjyxSmriSfYz+USnWPY7AufNIadK6MEJMx9ApD/oDOlTrorhX2lFEilmcO2RdNtjC5xDF9pCuTyYBB5bi6bQ4syoJ3g9WQ0ymaiLzg5KdCXlKLjvcW893SUOqXwnz5DrgnHtJTxOsQfEKIT7DmDSUCaxw7Rqn8787ZsmRM9e2VmPeBN4uqYvTtW7jvijg9N/yJsXecLq0q+WD6m8AAAD//wMAUEsDBBQABgAIAAAAIQA4/SH/1gAAAJQBAAALAAAAX3JlbHMvLnJlbHOkkMFqwzAMhu+DvYPRfXGawxijTi+j0GvpHsDYimMaW0Yy2fr2M4PBMnrbUb/Q94l/f/hMi1qRJVI2sOt6UJgd+ZiDgffL8ekFlFSbvV0oo4EbChzGx4f9GRdb25HMsYhqlCwG5lrLq9biZkxWOiqY22YiTra2kYMu1l1tQD30/bPm3wwYN0x18gb45AdQl1tp5j/sFB2T0FQ7R0nTNEV3j6o9feQzro1iOWA14Fm+Q8a1a8+Bvu/d/dMb2JY5uiPbhG/ktn4cqGU/er3pcvwCAAD//wMAUEsDBBQABgAIAAAAIQAd1cykJgIAAEcEAAAOAAAAZHJz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fx/oPoBAAD//wMAUEsBAi0AFAAGAAgAAAAhALaDOJL+AAAA4QEAABMAAAAAAAAAAAAAAAAAAAAAAFtDb250ZW50X1R5cGVzXS54bWxQSwECLQAUAAYACAAAACEAOP0h/9YAAACUAQAACwAAAAAAAAAAAAAAAAAvAQAAX3JlbHMvLnJlbHNQSwECLQAUAAYACAAAACEAHdXMpCYCAABHBAAADgAAAAAAAAAAAAAAAAAuAgAAZHJzL2Uyb0RvYy54bWxQSwECLQAUAAYACAAAACEAtYk1StsAAAAIAQAADwAAAAAAAAAAAAAAAACABAAAZHJzL2Rvd25yZXYueG1sUEsFBgAAAAAEAAQA8wAAAIgFAAAAAA==" type="#_x0000_t202" style="position:absolute;margin-left:-20.2pt;margin-top:0;width:185.9pt;height:110.6pt;z-index:251659264;visibility:visible;mso-wrap-style:square;mso-width-percent:400;mso-height-percent:200;mso-wrap-distance-left:9pt;mso-wrap-distance-top:3.6pt;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:3.6pt;mso-position-horizontal:absolute;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;mso-width-percent:400;mso-height-percent:200;mso-width-relative:margin;mso-height-relative:margin;v-text-anchor:top" id="Textfeld 2" o:spid="_x0000_s1026">
<v:textbox style="mso-fit-shape-to-text:t">
<w:txbxContent>
<w:p w:rsidR="00BD5B37" w:rsidRDefault="00BD5B37">
<w:proofErr w:type="spellStart"/>
<w:r>
<w:t>GetThisContent</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
</w:p>
</w:txbxContent>
</v:textbox>
<w10:wrap type="square"/>
</v:shape>
</w:pict>
</mc:Fallback>
</mc:AlternateContent>
</w:r>
</w:p>
<w:sectPr w:rsidRPr="009670CB" w:rsidR="00CF457B" w:rsidSect="00BD5B37">
<w:type w:val="continuous"/>
<w:pgSz w:w="11907" w:h="8391" w:orient="landscape" w:code="11"/>
<w:pgMar w:top="1418" w:right="1418" w:bottom="1418" w:left="1134" w:header="709" w:footer="709" w:gutter="0"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>
我需要 select 文本字段来进行邮件合并,我的计划是复制和移动这些字段。
如果有更好的方法,我愿意尝试一下:)
编辑:
我目前正在尝试进行整页复制,它适用于合并字段,但问题是当我尝试使用文本字段进行此操作时,我在第 369 行第 0 列出现错误,我决定上传 XML here。
我尝试了不同的东西并改变了一些东西,我总是在 Choice 元素的行中得到错误。
我可以通过某种方式调试它还是我实现错误了?
private static void generatePagesFromTemplate(final MainDocumentPart document)
{
// final String xpath = "//mc:AlternateContent";
// final List<Object> pageContent = document.getJAXBNodesViaXPath(xpath, false);
final List<Object> pageContent = document.getContent();
/** This is needed if you don't want a endless loop,
otherwise it will increase with adding new objects **/
final int nrOfElements = pageContent.size();
final int nrOfSheets = 2;
// Make a copy of the first sheet, to the nr of pages that exist
for (int sheetNr = 1; sheetNr < nrOfSheets; sheetNr++)
{
addPageBreak(document);
for (int i = 0; i < nrOfElements; i++)
{
final Object tmp = pageContent.get(i);
document.addObject(tmp);
System.out.println("Added object: " + tmp.toString());
}
}
}
可能是命名空间问题...
txbx 元素位于 xmlns:v="urn:schemas-microsoft-com:vml"
所以也许只需将其更改为:(我不知道您如何在 docx4j 中声明名称空间)
//v:txbx/w:txbxContent/w:p/w:r
这将 select 所有 w:r 的实际文本节点。
您会看到在上面的 XML 中,单个文本框出现了两次(在 Choice 元素和 Fallback 元素中)。
复制文本框时,应该复制祖先 mc:AlternateContent 元素或 Choice 元素或 Fallback 元素的内容。你应该将它复制到 w:p/w:r.
如果您只使用文本框内的 w:r,您将丢失文本框定位等。
而且您不能只使用 w:txbxContent 元素,因为它必须存在于上面找到的结构中。
就像@JasonPlutext 在他的 的最后评论中指出的那样,我无法进行整页克隆的问题是来自 docPr 的 ID 保持不变,因此您必须手动更改它.非常感谢!
一个简单的解决方案是在复制所有元素后,使用 XPath 选择所有元素并将其更改为随机 ID。
private static void setRandomIdsForDocPr(final MainDocumentPart document)
throws JAXBException, XPathBinderAssociationIsPartialException
{
final String xpath = "//wp:docPr";
final List<Object> docPr = document.getJAXBNodesViaXPath(xpath, false);
CTNonVisualDrawingProps props;
final Random random = new Random();
final int min = 1000;
final int max = 9999;
for (int i = 0; i < docPr.size(); i++)
{
props = (CTNonVisualDrawingProps) docPr.get(i);
props.setId(random.nextInt((max - min) + 1) + min);
}
}
唯一仍然不起作用的是进行邮件合并,我有一个列表 List<Map<DataFieldName, String>> data
,里面有两张地图,当我使用 MailMerger.performMerge(...)
时,我认为它覆盖了两张纸。
当我使用与 MailMergerWithNext.performLabelMerge(..)
相同的数据时,它只使用第一个地图,仅此而已。
所以我在每个页面上都有相同的内容,这也是元素不唯一的问题吗?
我的问题是我无法使用 document.getContent()
和 document.getContents()
获取我的文本字段。
所以我尝试使用 XPath 来 selecting 对象,它有效但我无法复制对象并再次添加它。
例如:
XPath | Exception | Comment |
---|---|---|
//wps:txbx/w:txbxContent | javax.xml.bind.MarshalException | SAXException2, Missing @XmlRootElement-Annotation |
//wps:txbx/w:txbxContent/w:p/w:r | javax.xml.transform.TransformerException | unexpected Element, because it is on the wrong place |
我也尝试创建一个自己的对象,但这对我也不起作用,因为它不被接受为 JAXB 对象。
这是我的代码:
public static void main(final String[] args) throws Docx4JException, JAXBException
{
final String filePath = "./files/GetTextfield.docx";
final WordprocessingMLPackage word = Docx4J.load(new File(filePath));
final MainDocumentPart document = word.getMainDocumentPart();
final String xpathExpr = "//wps:txbx/w:txbxContent/w:p/w:r";
final List<Object> nodes = document.getJAXBNodesViaXPath(xpathExpr, false);
addPageBreak(document);
final XmlUtils xmlUtils = new XmlUtils();
final Object field = nodes.get(0);
final Object copy = xmlUtils.deepCopy(field);
document.addObject(copy);
System.out.println(document.getXML());
// TODO Before MailMerge need to copy template content to all pages
}
我重新创建了这个问题,因为我猜 the old one 有点令人困惑。
我的文档为 XML (document.getXML())
<w:document mc:Ignorable="w14 w15 wp14" xmlns:cppr="http://schemas.microsoft.com/office/2006/coverPageProps" xmlns:dgm1611="http://schemas.microsoft.com/office/drawing/2016/11/diagram" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wp15="http://schemas.microsoft.com/office/word/2012/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:a1611="http://schemas.microsoft.com/office/drawing/2016/11/main" xmlns:a16svg="http://schemas.microsoft.com/office/drawing/2016/SVG/main" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:pvml="urn:schemas-microsoft-com:office:powerpoint" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:dgm1612="http://schemas.microsoft.com/office/drawing/2016/12/diagram" xmlns:comp="http://schemas.openxmlformats.org/drawingml/2006/compatibility" xmlns:xvml="urn:schemas-microsoft-com:office:excel" xmlns:c173="http://schemas.microsoft.com/office/drawing/2017/03/chart" xmlns:anam3d="http://schemas.microsoft.com/office/drawing/2018/animation/model3d" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:adec="http://schemas.microsoft.com/office/drawing/2017/decorative" xmlns:oda="http://opendope.org/answers" xmlns:a18hc="http://schemas.microsoft.com/office/drawing/2018/hyperlinkcolor" xmlns:odc="http://opendope.org/conditions" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:cdr="http://schemas.openxmlformats.org/drawingml/2006/chartDrawing" xmlns:odi="http://opendope.org/components" xmlns:msink="http://schemas.microsoft.com/ink/2010/main" xmlns:cdr14="http://schemas.microsoft.com/office/drawing/2010/chartDrawing" xmlns:iact="http://schemas.microsoft.com/office/powerpoint/2014/inkAction" xmlns:an18="http://schemas.microsoft.com/office/drawing/2018/animation" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:odq="http://opendope.org/questions" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:dsp="http://schemas.microsoft.com/office/drawing/2008/diagram" xmlns:odx="http://opendope.org/xpaths" xmlns:a15="http://schemas.microsoft.com/office/drawing/2012/main" xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" xmlns:c15="http://schemas.microsoft.com/office/drawing/2012/chart" xmlns:a13cmd="http://schemas.microsoft.com/office/drawing/2013/main/command" xmlns:c14="http://schemas.microsoft.com/office/drawing/2007/8/2/chart" xmlns:a16="http://schemas.microsoft.com/office/drawing/2014/main" xmlns:odgm="http://opendope.org/SmartArt/DataHierarchy" xmlns:c16="http://schemas.microsoft.com/office/drawing/2014/chart" xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:thm15="http://schemas.microsoft.com/office/thememl/2012/main" xmlns:we="http://schemas.microsoft.com/office/webextensions/webextension/2010/11" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:ns39="http://www.w3.org/2003/InkML" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:ns38="http://www.w3.org/1998/Math/MathML" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:dgm14="http://schemas.microsoft.com/office/drawing/2010/diagram" xmlns:c16ac="http://schemas.microsoft.com/office/drawing/2014/chart/ac" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:b="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:cs="http://schemas.microsoft.com/office/drawing/2012/chartStyle" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:pic14="http://schemas.microsoft.com/office/drawing/2010/picture" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:lc="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas" xmlns:wetp="http://schemas.microsoft.com/office/webextensions/taskpanes/2010/11">
<w:body>
<w:p w:rsidRPr="009670CB" w:rsidR="00CF457B" w:rsidP="009670CB" w:rsidRDefault="00BD5B37">
<w:bookmarkStart w:name="_GoBack" w:id="0"/>
<w:bookmarkEnd w:id="0"/>
<w:r>
<w:rPr>
<w:noProof/>
</w:rPr>
<mc:AlternateContent>
<mc:Choice Requires="wps">
<w:drawing>
<wp:anchor distT="45720" distB="45720" distL="114300" distR="114300" simplePos="false" relativeHeight="251659264" behindDoc="false" locked="false" layoutInCell="true" allowOverlap="true">
<wp:simplePos x="0" y="0"/>
<wp:positionH relativeFrom="column">
<wp:posOffset>-256540</wp:posOffset>
</wp:positionH>
<wp:positionV relativeFrom="paragraph">
<wp:posOffset>0</wp:posOffset>
</wp:positionV>
<wp:extent cx="2360930" cy="1404620"/>
<wp:effectExtent l="0" t="0" r="22860" b="11430"/>
<wp:wrapSquare wrapText="bothSides"/>
<wp:docPr id="217" name="Textfeld 2"/>
<wp:cNvGraphicFramePr>
<a:graphicFrameLocks/>
</wp:cNvGraphicFramePr>
<a:graphic>
<a:graphicData uri="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
<wps:wsp>
<wps:cNvSpPr txBox="true">
<a:spLocks noChangeArrowheads="true"/>
</wps:cNvSpPr>
<wps:spPr bwMode="auto">
<a:xfrm>
<a:off x="0" y="0"/>
<a:ext cx="2360930" cy="1404620"/>
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst/>
</a:prstGeom>
<a:solidFill>
<a:srgbClr val="FFFFFF"/>
</a:solidFill>
<a:ln w="9525">
<a:solidFill>
<a:srgbClr val="000000"/>
</a:solidFill>
<a:miter lim="800000"/>
<a:headEnd/>
<a:tailEnd/>
</a:ln>
</wps:spPr>
<wps:txbx>
<w:txbxContent>
<w:p w:rsidR="00BD5B37" w:rsidRDefault="00BD5B37">
<w:proofErr w:type="spellStart"/>
<w:r>
<w:t>GetThisContent</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
</w:p>
</w:txbxContent>
</wps:txbx>
<wps:bodyPr rot="0" vert="horz" wrap="square" lIns="91440" tIns="45720" rIns="91440" bIns="45720" anchor="t" anchorCtr="false">
<a:spAutoFit/>
</wps:bodyPr>
</wps:wsp>
</a:graphicData>
</a:graphic>
<wp14:sizeRelH relativeFrom="margin">
<wp14:pctWidth>40000</wp14:pctWidth>
</wp14:sizeRelH>
<wp14:sizeRelV relativeFrom="margin">
<wp14:pctHeight>20000</wp14:pctHeight>
</wp14:sizeRelV>
</wp:anchor>
</w:drawing>
</mc:Choice>
<mc:Fallback>
<w:pict>
<v:shapetype o:spt="202.0" path="m,l,21600r21600,l21600,xe" coordsize="21600,21600" id="_x0000_t202">
<v:stroke joinstyle="miter"/>
<v:path gradientshapeok="t" o:connecttype="rect"/>
</v:shapetype>
<v:shape o:gfxdata="UEsDBBQABgAIAAAAIQC2gziS/gAAAOEBAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbJSRQU7DMBBF90jcwfIWJU67QAgl6YK0S0CoHGBkTxKLZGx5TGhvj5O2G0SRWNoz/78nu9wcxkFMGNg6quQqL6RA0s5Y6ir5vt9lD1JwBDIwOMJKHpHlpr69KfdHjyxSmriSfYz+USnWPY7AufNIadK6MEJMx9ApD/oDOlTrorhX2lFEilmcO2RdNtjC5xDF9pCuTyYBB5bi6bQ4syoJ3g9WQ0ymaiLzg5KdCXlKLjvcW893SUOqXwnz5DrgnHtJTxOsQfEKIT7DmDSUCaxw7Rqn8787ZsmRM9e2VmPeBN4uqYvTtW7jvijg9N/yJsXecLq0q+WD6m8AAAD//wMAUEsDBBQAB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UP7tiIJG30emZy4YrcmvU+TFcfh8pyyfs3/+icAAAD//wMAUEsDBBQABgAIAAAAIQC1iTVK2wAAAAgBAAAPAAAAZHJzL2Rvd25yZXYueG1sTI9PS8NAFMTvgt9heYK3dpM0FIl5KSXotdBW6PU1uybR/ROzmzR+e58nPQ4zzPym3C3WiFmPofcOIV0nILRrvOpdi/B2fl09gQiRnCLjnUb41gF21f1dSYXyN3fU8ym2gktcKAihi3EopAxNpy2FtR+0Y+/dj5Yiy7GVaqQbl1sjsyTZSku944WOBl13uvk8TRZhOtf7+VhnH5f5oPLD9oUsmS/Ex4dl/wwi6iX+heEXn9GhYqarn5wKwiCs8iTnKAI/YnuzSVleEbIszUBWpfx/oPoBAAD//wMAUEsBAi0AFAAGAAgAAAAhALaDOJL+AAAA4QEAABMAAAAAAAAAAAAAAAAAAAAAAFtDb250ZW50X1R5cGVzXS54bWxQSwECLQAUAAYACAAAACEAOP0h/9YAAACUAQAACwAAAAAAAAAAAAAAAAAvAQAAX3JlbHMvLnJlbHNQSwECLQAUAAYACAAAACEAHdXMpCYCAABHBAAADgAAAAAAAAAAAAAAAAAuAgAAZHJzL2Uyb0RvYy54bWxQSwECLQAUAAYACAAAACEAtYk1StsAAAAIAQAADwAAAAAAAAAAAAAAAACABAAAZHJzL2Rvd25yZXYueG1sUEsFBgAAAAAEAAQA8wAAAIgFAAAAAA==" type="#_x0000_t202" style="position:absolute;margin-left:-20.2pt;margin-top:0;width:185.9pt;height:110.6pt;z-index:251659264;visibility:visible;mso-wrap-style:square;mso-width-percent:400;mso-height-percent:200;mso-wrap-distance-left:9pt;mso-wrap-distance-top:3.6pt;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:3.6pt;mso-position-horizontal:absolute;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;mso-width-percent:400;mso-height-percent:200;mso-width-relative:margin;mso-height-relative:margin;v-text-anchor:top" id="Textfeld 2" o:spid="_x0000_s1026">
<v:textbox style="mso-fit-shape-to-text:t">
<w:txbxContent>
<w:p w:rsidR="00BD5B37" w:rsidRDefault="00BD5B37">
<w:proofErr w:type="spellStart"/>
<w:r>
<w:t>GetThisContent</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
</w:p>
</w:txbxContent>
</v:textbox>
<w10:wrap type="square"/>
</v:shape>
</w:pict>
</mc:Fallback>
</mc:AlternateContent>
</w:r>
</w:p>
<w:sectPr w:rsidRPr="009670CB" w:rsidR="00CF457B" w:rsidSect="00BD5B37">
<w:type w:val="continuous"/>
<w:pgSz w:w="11907" w:h="8391" w:orient="landscape" w:code="11"/>
<w:pgMar w:top="1418" w:right="1418" w:bottom="1418" w:left="1134" w:header="709" w:footer="709" w:gutter="0"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>
我需要 select 文本字段来进行邮件合并,我的计划是复制和移动这些字段。 如果有更好的方法,我愿意尝试一下:)
编辑:
我目前正在尝试进行整页复制,它适用于合并字段,但问题是当我尝试使用文本字段进行此操作时,我在第 369 行第 0 列出现错误,我决定上传 XML here。 我尝试了不同的东西并改变了一些东西,我总是在 Choice 元素的行中得到错误。
我可以通过某种方式调试它还是我实现错误了?
private static void generatePagesFromTemplate(final MainDocumentPart document)
{
// final String xpath = "//mc:AlternateContent";
// final List<Object> pageContent = document.getJAXBNodesViaXPath(xpath, false);
final List<Object> pageContent = document.getContent();
/** This is needed if you don't want a endless loop,
otherwise it will increase with adding new objects **/
final int nrOfElements = pageContent.size();
final int nrOfSheets = 2;
// Make a copy of the first sheet, to the nr of pages that exist
for (int sheetNr = 1; sheetNr < nrOfSheets; sheetNr++)
{
addPageBreak(document);
for (int i = 0; i < nrOfElements; i++)
{
final Object tmp = pageContent.get(i);
document.addObject(tmp);
System.out.println("Added object: " + tmp.toString());
}
}
}
可能是命名空间问题... txbx 元素位于 xmlns:v="urn:schemas-microsoft-com:vml" 所以也许只需将其更改为:(我不知道您如何在 docx4j 中声明名称空间)
//v:txbx/w:txbxContent/w:p/w:r
这将 select 所有 w:r 的实际文本节点。
您会看到在上面的 XML 中,单个文本框出现了两次(在 Choice 元素和 Fallback 元素中)。
复制文本框时,应该复制祖先 mc:AlternateContent 元素或 Choice 元素或 Fallback 元素的内容。你应该将它复制到 w:p/w:r.
如果您只使用文本框内的 w:r,您将丢失文本框定位等。
而且您不能只使用 w:txbxContent 元素,因为它必须存在于上面找到的结构中。
就像@JasonPlutext 在他的
一个简单的解决方案是在复制所有元素后,使用 XPath 选择所有元素并将其更改为随机 ID。
private static void setRandomIdsForDocPr(final MainDocumentPart document)
throws JAXBException, XPathBinderAssociationIsPartialException
{
final String xpath = "//wp:docPr";
final List<Object> docPr = document.getJAXBNodesViaXPath(xpath, false);
CTNonVisualDrawingProps props;
final Random random = new Random();
final int min = 1000;
final int max = 9999;
for (int i = 0; i < docPr.size(); i++)
{
props = (CTNonVisualDrawingProps) docPr.get(i);
props.setId(random.nextInt((max - min) + 1) + min);
}
}
唯一仍然不起作用的是进行邮件合并,我有一个列表 List<Map<DataFieldName, String>> data
,里面有两张地图,当我使用 MailMerger.performMerge(...)
时,我认为它覆盖了两张纸。
当我使用与 MailMergerWithNext.performLabelMerge(..)
相同的数据时,它只使用第一个地图,仅此而已。
所以我在每个页面上都有相同的内容,这也是元素不唯一的问题吗?