我正在尝试找出如何更新/更改模板上的字段。到目前为止,我发现的每一个好的intel都导致当前的页眉或页脚被完全覆盖。很明显问题是我找不到如何在页脚而不是整个页脚中专门修改某些字段。
// Set headers
foreach (Word.Section section in doc.Sections)
{
Word.Range headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage);
headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
}
// Set footers
foreach (Word.Section wordSection in doc.Sections)
{
Word.Range footerRange = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
footerRange.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;
footerRange.Font.Size = 20;
footerRange.Text = "Confidential";
}
在这段代码中,我试图获取“机密”一词来覆盖中间字段。当前中间字段为空白。左字段和右字段分别是页码和日期。一旦运行代码,所有这些都将替换为“机密”代码。它还可以完全删除作为页脚的背景色放置的形状。
如何修改现有字段?会以这种方式删除形状吗?
更新: 到目前为止,下面的代码是我一直想绕圈子而已。这似乎非常有序,就好像您在物理上键入\编辑文档一样。 ck无论哪种方式,这都很接近。但是由于某种原因,格式被忽略了,我无法将其仅应用于中心文本。另外,如果我取消注释日期字段行,则会删除整个页脚。
// Set footers
foreach (Word.Section wordSection in doc.Sections)
{
Word.Range footerRange = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
footerRange.Paragraphs.TabStops.Add(wordApp.InchesToPoints(3.25F), Word.WdTabAlignment.wdAlignTabCenter);
footerRange.Paragraphs.TabStops.Add(wordApp.InchesToPoints(6.5F), Word.WdTabAlignment.wdAlignTabRight);
footerRange.Fields.Add(footerRange, Word.WdFieldType.wdFieldPage, "\t", true);
footerRange.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;
footerRange.Font.Size = 20;
footerRange.Text = "\tCONFIDENTIAL\t";
footerRange.InsertBefore("01-DEC-18");
//footerRange.Fields.Add(footerRange, Word.WdFieldType.wdFieldDate);
}
答案 0 :(得分:0)
我只是继续手动操作以获得最佳效果。
string tab = "\t";
string note = "Confidential";
string dt = DateTime.Today.Date.ToString("d");
int pg = 0;
// Set footers
foreach (Word.Section wordSection in doc.Sections)
{
pg += 1;
Word.Range footerRange = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
footerRange.Paragraphs.TabStops.Add(wordApp.InchesToPoints(3.25F), Word.WdTabAlignment.wdAlignTabCenter);
footerRange.Paragraphs.TabStops.Add(wordApp.InchesToPoints(6.5F), Word.WdTabAlignment.wdAlignTabRight);
string footerString = $"pg. {pg.ToString()}{tab}{note}{tab}{dt}";
footerRange.Text = footerString;
}
答案 1 :(得分:0)
以下内容用指定的格式替换“机密”,以替换文档中每个主要页脚范围的第二部分中的当前内容。 (“第二部分”表示第一个和第二个制表位之间的内容。)
关键是找到两个制表位的Range
位置,然后将内容的目标Range
设置在第一个制表位之后的一个字符和第二个制表位之前的一个字符。
Word.Range ftr = null;
Word.Paragraph para = null;
Word.Range paraRng = null;
Word.Range tStop1 = null;
Word.Range tStop2 = null;
int nrTabs;
foreach (Word.Section sec in doc.Sections)
{
ftr = sec.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
para = ftr.Paragraphs[1];
nrTabs = para.TabStops.Count;
if (nrTabs == 2)
{
paraRng = para.Range;
tStop1 = paraRng.Duplicate;
tStop1.Find.Execute("^t", missing, missing, missing, missing, missing,
missing, missing, Word.WdFindWrap.wdFindStop);
tStop1.MoveStart(Word.WdUnits.wdCharacter, 1); //put it after the tab character
tStop2 = tStop1.Duplicate;
tStop2.Find.Execute("^t", missing, missing, missing, missing, missing,
missing, missing, Word.WdFindWrap.wdFindStop);
tStop2.MoveEnd(Word.WdUnits.wdCharacter, -1); //put it before the tab character
tStop1.End = tStop2.End;
tStop1.Text = "Confidential";
tStop1.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;
tStop1.Font.Size = 20f;
}
}