没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
原创|其它|编辑:郝浩|2012-10-17 14:34:43.000|阅读 1853 次
概述:本文给出一个例子,展示了在Aspose.Words for Java使用中,如何创建您自己的MergeField类,它代表在Word文档中的一个单独合并字段,并允许获取或设置它的名称。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
本文给出一个例子,展示了在Aspose.Words for Java使用中,如何创建您自己的MergeField类,它代表在Word文档中的一个单独合并字段,并允许获取或设置它的名称。
package Examples;
import org.testng.annotations.Test;
import com.aspose.words.Document;
import com.aspose.words.NodeCollection;
import com.aspose.words.NodeType;
import com.aspose.words.FieldStart;
import com.aspose.words.FieldType;
import com.aspose.words.Run;
import com.aspose.words.Node;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Shows how to rename merge fields in a Word document.
*/
public class ExRenameMergeFields extends ExBase
{
/**
* Finds all merge fields in a Word document and changes their names.
*/
public void renameMergeFields() throws Exception
{
// Specify your document name here.
Document doc = new Document(getMyDir() + "RenameMergeFields.doc");
// Select all field start nodes so we can find the merge fields.
NodeCollection fieldStarts = doc.getChildNodes(NodeType.FIELD_START, true);
for (FieldStart fieldStart : (Iterable<FieldStart>) fieldStarts)
{
if (fieldStart.getFieldType() == FieldType.FIELD_MERGE_FIELD)
{
MergeField mergeField = new MergeField(fieldStart);
mergeField.setName(mergeField.getName() + "_Renamed");
}
}
doc.save(getMyDir() + "RenameMergeFields Out.doc");
}
}
/**
* Represents a facade object for a merge field in a Microsoft Word document.
*/
class MergeField
{
MergeField(FieldStart fieldStart) throws Exception
{
if (fieldStart.equals(null))
throw new IllegalArgumentException("fieldStart");
if (fieldStart.getFieldType() != FieldType.FIELD_MERGE_FIELD)
throw new IllegalArgumentException("Field start type must be FieldMergeField.");
mFieldStart = fieldStart;
// Find the field separator node.
mFieldSeparator = findNextSibling(mFieldStart, NodeType.FIELD_SEPARATOR);
if (mFieldSeparator == null)
throw new IllegalStateException("Cannot find field separator.");
// Find the field end node. Normally field end will always be found, but in the example document
// there happens to be a paragraph break included in the hyperlink and this puts the field end
// in the next paragraph. It will be much more complicated to handle fields which span several
// paragraphs correctly, but in this case allowing field end to be null is enough for our purposes.
mFieldEnd = findNextSibling(mFieldSeparator, NodeType.FIELD_END);
}
/**
* Gets or sets the name of the merge field.
*/
String getName() throws Exception
{
String fieldResult = getTextSameParent(mFieldSeparator.getNextSibling(), mFieldEnd);
int startPos = fieldResult.indexOf("«");
startPos = (startPos >= 0) ? startPos + 1 : 0;
int endPos = fieldResult.indexOf("»");
endPos = (endPos >= 0) ? endPos : fieldResult.length();
return fieldResult.substring(startPos, endPos);
}
void setName(String value) throws Exception
{
// Merge field name is stored in the field result which is a Run
// node between field separator and field end.
Run fieldResult = (Run)mFieldSeparator.getNextSibling();
fieldResult.setText(java.text.MessageFormat.format("«{0}»", value));
// But sometimes the field result can consist of more than one run, delete these runs.
removeSameParent(fieldResult.getNextSibling(), mFieldEnd);
updateFieldCode(value);
}
private void updateFieldCode(String fieldName) throws Exception
{
// Field code is stored in a Run node between field start and field separator.
Run fieldCode = (Run)mFieldStart.getNextSibling();
Matcher matcher = G_REGEX.matcher(fieldCode.getText());
matcher.find();
String newFieldCode = java.text.MessageFormat.format(" {0}{1} ", matcher.group(1).toString(), fieldName);
fieldCode.setText(newFieldCode);
// But sometimes the field code can consist of more than one run, delete these runs.
removeSameParent(fieldCode.getNextSibling(), mFieldSeparator);
}
/**
* Goes through siblings starting from the start node until it finds a node of the specified type or null.
*/
private static Node findNextSibling(Node startNode, int nodeType) throws Exception
{
for (Node node = startNode; node != null; node = node.getNextSibling())
{
if (node.getNodeType() == nodeType)
return node;
}
return null;
}
/**
* Retrieves text from start up to but not including the end node.
*/
private static String getTextSameParent(Node startNode, Node endNode) throws Exception
{
if ((endNode != null) && (startNode.getParentNode() != endNode.getParentNode()))
throw new IllegalArgumentException("Start and end nodes are expected to have the same parent.");
StringBuilder builder = new StringBuilder();
for (Node child = startNode; !child.equals(endNode); child = child.getNextSibling())
builder.append(child.getText());
return builder.toString();
}
/**
* Removes nodes from start up to but not including the end node.
* Start and end are assumed to have the same parent.
*/
private static void removeSameParent(Node startNode, Node endNode) throws Exception
{
if ((endNode != null) && (startNode.getParentNode() != endNode.getParentNode()))
throw new IllegalArgumentException("Start and end nodes are expected to have the same parent.");
Node curChild = startNode;
while ((curChild != null) && (curChild != endNode))
{
Node nextChild = curChild.getNextSibling();
curChild.remove();
curChild = nextChild;
}
}
private final Node mFieldStart;
private final Node mFieldSeparator;
private final Node mFieldEnd;
private static final Pattern G_REGEX = Pattern.compile("\\s*(MERGEFIELD\\s|)(\\s|)(\\S+)\\s+");
}
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn
文章转载自:慧都控件网



接DevExpress原厂商通知,将于近日上调旗下产品授权价格,现在下单客户可享受优惠报价!
面对“数字中国”建设和中国制造2025战略实施的机遇期,中车信息公司紧跟时代的步伐,以“集约化、专业化、标准化、精益化、一体化、平台化”为工作目标,大力推进信息服务、工业软件等核心产品及业务的发展。在慧都3D解决方案的实施下,清软英泰建成了多模型来源的综合轻量化显示平台、实现文件不失真的百倍压缩比、针对模型中的大模型文件,在展示平台上进行流畅展示,提升工作效率,优化了使用体验。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@ke049m.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
半岛外围网上直营