Aspose.Words使用教程之(如何使用ChartDataLabel),(如何重新命名合併欄位)

風靈使發表於2018-08-16

使用ChartDataLabel你可以規定單個圖表系列的資料標籤的格式,像顯現/隱藏圖例、種類名字、SeriesName、值等等。

ChartDataLabelCollection dataLabelCollection = series0.DataLabels;

// Add data label to the first and second point of the first series.
ChartDataLabel chartDataLabel00 = dataLabelCollection.Add(0);
ChartDataLabel chartDataLabel01 = dataLabelCollection.Add(1);

// Set properties.
chartDataLabel00.ShowLegendKey = true;

// By default, when you add data labels to the data points in a pie chart, leader lines are displayed for data labels that are
// positioned far outside the end of data points. Leader lines create a visual connection between a data label and its
// corresponding data point.
chartDataLabel00.ShowLeaderLines = true;

chartDataLabel00.ShowCategoryName = false;
chartDataLabel00.ShowPercentage = false;
chartDataLabel00.ShowSeriesName = true;
chartDataLabel00.ShowValue = true;
chartDataLabel00.Separator = "/";

chartDataLabel01.ShowValue = true;

結果如下:
Aspose.Words


一個示例展示如何建立自己的合併欄位類,代表一個在微軟的Word文件中允許您獲取或設定它合併欄位的名稱。

Example

如何在一個文件裡重新命名欄位。

using System;
using System.Text;
using System.Text.RegularExpressions;
using Aspose.Words;
using Aspose.Words.Fields;

namespace Examples
{
/// <summary>
/// Shows how to rename merge fields in a Word document.
/// </summary>
public class ExRenameMergeFields : ExBase
{
/// <summary>
/// Finds all merge fields in a Word document and changes their names.
/// </summary>
public void RenameMergeFields()
{
// Specify your document name here.
Document doc = new Document(MyDir + "RenameMergeFields.doc");

// Select all field start nodes so we can find the merge fields.
NodeCollection fieldStarts = doc.GetChildNodes(NodeType.FieldStart, true);
foreach (FieldStart fieldStart in fieldStarts)
{
if (fieldStart.FieldType.Equals(FieldType.FieldMergeField))
{
MergeField mergeField = new MergeField(fieldStart);
mergeField.Name = mergeField.Name + "_Renamed";
}
}

doc.Save(MyDir + "RenameMergeFields Out.doc");
}
}

/// <summary>
/// Represents a facade object for a merge field in a Microsoft Word document.
/// </summary>
internal class MergeField
{
internal MergeField(FieldStart fieldStart)
{
if (fieldStart.Equals(null))
throw new ArgumentNullException("fieldStart");
if (!fieldStart.FieldType.Equals(FieldType.FieldMergeField))
throw new ArgumentException("Field start type must be FieldMergeField.");

mFieldStart = fieldStart;

// Find the field separator node.
mFieldSeparator = fieldStart.GetField().Separator;
if (mFieldSeparator == null)
throw new InvalidOperationException("Cannot find field separator.");

mFieldEnd = fieldStart.GetField().End;
}

/// <summary>
/// Gets or sets the name of the merge field.
/// </summary>
internal string Name
{
get
{
return ((FieldStart)mFieldStart).GetField().Result.Replace("«", "").Replace("»", "");
}
set
{
// Merge field name is stored in the field result which is a Run
// node between field separator and field end.
Run fieldResult = (Run)mFieldSeparator.NextSibling;
fieldResult.Text = string.Format("«{0}»", value);

// But sometimes the field result can consist of more than one run, delete these runs.
RemoveSameParent(fieldResult.NextSibling, mFieldEnd);

UpdateFieldCode(value);
}
}

private void UpdateFieldCode(string fieldName)
{
// Field code is stored in a Run node between field start and field separator.
Run fieldCode = (Run)mFieldStart.NextSibling;

Match match = gRegex.Match(((FieldStart)mFieldStart).GetField().GetFieldCode());

string newFieldCode = string.Format(" {0}{1} ", match.Groups["start"].Value, fieldName);
fieldCode.Text = newFieldCode;

// But sometimes the field code can consist of more than one run, delete these runs.
RemoveSameParent(fieldCode.NextSibling, mFieldSeparator);
}

/// <summary>
/// Removes nodes from start up to but not including the end node.
/// Start and end are assumed to have the same parent.
/// </summary>
private static void RemoveSameParent(Node startNode, Node endNode)
{
if ((endNode != null) && (startNode.ParentNode != endNode.ParentNode))
throw new ArgumentException("Start and end nodes are expected to have the same parent.");

Node curChild = startNode;
while ((curChild != null) && (curChild != endNode))
{
Node nextChild = curChild.NextSibling;
curChild.Remove();
curChild = nextChild;
}
}

private readonly Node mFieldStart;
private readonly Node mFieldSeparator;
private readonly Node mFieldEnd;

private static readonly Regex gRegex = new Regex(@"\s*(?<start>MERGEFIELD\s|)(\s|)(?<name>\S+)\s+");
}
}

相關文章