private static void FillSimpleText(SdtRun simpleTextCC, string replacementText)
{
// remove the showing place holder element
SdtProperties ccProperties = simpleTextCC.SdtProperties;
ccProperties.RemoveAllChildren<ShowingPlaceholder>();
// fetch content block Run element
SdtContentRun contentRun = simpleTextCC.SdtContentRun;
var ccRun = contentRun.GetFirstChild<Run>();
// if there was no placeholder text in the content control, then the SdtContentRun
// block will be empty -> ccRun will be null, so create a new instance
if (ccRun == null)
{
ccRun = new Run(
new RunProperties() { RunStyle = null },
new Text());
contentRun.Append(ccRun);
}
// remove revision identifier & replace text
ccRun.RsidRunProperties = null;
ccRun.GetFirstChild<Text>().Text = replacementText;
// set the run style to that stored in the SdtProperties block, if there was
// one. Otherwise the existing style will be used.
var props = ccProperties.GetFirstChild<RunProperties>();
if (props != null)
if (props != null)
{
RunStyle runStyle = props.RunStyle;
if (runStyle != null)
{
// set the run style to the same as content block property style.
var runProps = ccRun.RunProperties;
runProps.RunStyle = new RunStyle() { Val = runStyle.Val };
runProps.RunFonts = null;
}
}
}
/// <summary>
///
/// </summary>
/// <param name="dotxPath"></param>
/// <param name="contentControlTag"></param>
/// <param name="replaceText"></param>
public static void SetSimpleTextInFooter(string dotxPath, string contentControlTag, string replaceText)
{
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(dotxPath, true))
{
foreach (FooterPart footerPart in wordprocessingDocument.MainDocumentPart.FooterParts)
{
var footerPartSdtRuns = footerPart.Footer.Descendants<SdtRun>()
.Where(run => run.SdtProperties.GetFirstChild<Tag>().Val.Value == contentControlTag);
foreach (SdtRun sdtRun in footerPartSdtRuns)
{
sdtRun.Descendants<Text>().First().Text = replaceText;
}
}
wordprocessingDocument.MainDocumentPart.Document.Save();
}
}
/// <summary>
///
/// </summary>
/// <param name="dotxPath"></param>
/// <param name="contentControlTag"></param>
/// <param name="replaceText"></param>
public static void SetSimpleTextInHeader(string dotxPath, string contentControlTag, string replaceText)
{
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(dotxPath, true))
{
foreach (HeaderPart headerPart in wordprocessingDocument.MainDocumentPart.HeaderParts)
{
var headerPartSdtRuns = headerPart.Header.Descendants<SdtRun>()
.Where(run => run.SdtProperties.GetFirstChild<Tag>().Val.Value == contentControlTag);
foreach (SdtRun sdtRun in headerPartSdtRuns)
{
sdtRun.Descendants<Text>().First().Text = replaceText;
}
}
wordprocessingDocument.MainDocumentPart.Document.Save();
}
}
public static void SetSimpleTextInDocument(string dotxPath, string contentControlTag, string replaceText)
{
using (WordprocessingDocument wordProcessingDocument = WordprocessingDocument.Open(dotxPath, true))
{
MainDocumentPart mainDocumentPart = wordProcessingDocument.MainDocumentPart;
IEnumerable<SdtRun> sdtRuns = mainDocumentPart.Document.Descendants<SdtRun>()
.Where(run => run.SdtProperties.GetFirstChild<Tag>().Val == contentControlTag);
foreach (SdtRun item in sdtRuns)
{
FillSimpleText(item, replaceText);
}
}
}
public static Table GetTableByControlTag(MainDocumentPart mainPart, string contentControlTag)
{
// This should return only one content control element: the one with
// the specified tag value.
// If not, "Single()" throws an exception.
SdtBlock ccWithTable = mainPart.Document.Body.Descendants<SdtBlock>().Where
(r => r.SdtProperties.GetFirstChild<Tag>().Val == contentControlTag).Single();
// This should return only one table.
return ccWithTable.Descendants<Table>().Single();
}
/// <summary>
/// 根据数据源DataTable动态生成DOC文档中的表行
/// </summary>
/// <param name="theTable">DOC文档中的表</param>
/// <param name="dtSource">数据源DataTable</param>
public static void GenerateRowFromDataTable(Table theTable, DataTable dtSource)
{
//Copy the last row format
TableRow theRow = theTable.Elements<TableRow>().Last();
foreach (DataRow dr in dtSource.Rows)
{
TableRow rowCopy = (TableRow)theRow.CloneNode(true);
object[] objs = dr.ItemArray;
IEnumerable<TableCell> tcs = rowCopy.Descendants<TableCell>();
int i = 0;
foreach (TableCell item in tcs)
{
//IEnumerable<Paragraph> ps = item.Descendants<Paragraph>();
item.RemoveAllChildren<Paragraph>();
item.Append(new Paragraph(new Run(new Text(objs[i].ToString()))));
}
theTable.AppendChild(rowCopy);
}
// Remove the empty placeholder row from the table.
theTable.RemoveChild(theRow);
}