在字头表C#中添加页码 [英] Adding page number in word header table C#

查看:50
本文介绍了在字头表C#中添加页码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 1x3 表格作为我在 Word 中的标题.这就是我想要的样子.

I have created a 1x3 table as my header in word. This is how I want it to look like.

LeftText MiddleText 页码:

LeftText MiddleText PageNumber:

我希望 PageNumber 单元格看起来像这样 -

I want the PageNumber cell to look like this -

页面:Y 中的 X

我已经成功完成了单元格 (1,1) 和 (1,2).我发现这可以帮助我处理单元格 (1,3),但它并没有像我喜欢的那样工作.我知道如何获得文档的总数.我不确定如何正确实施它.

I have managed to do cell (1,1) and (1,2). I found this to help me with cell (1,3) but it is not working as I like. I know how to get the total count of the document. I'm not sure how to implement it properly.

Range rRange = restheaderTable.Cell(1, 3).Range;
rRange.End = rRange.End - 1;
oDoc.Fields.Add(rRange, Type: WdFieldType.wdFieldPage, Text: "Page Number: ");

我什至无法在单元格中显示文本页码:".它现在只有一个数字.

I can't even get the Text "Page Number: " to display in the cell. All it has is a number right now.

推荐答案

您要查找的字段枚举是 WordWdFieldType.wdFieldNumPages.

The field enumeration you're looking for is WordWdFieldType.wdFieldNumPages.

下一个障碍是如何构建字段 + 文本 + 字段,因为当按此顺序添加内容时,Word 的行为并不合乎逻辑".目标点保持在插入的字段之前.所以要么需要向后工作,要么在每一位内容之后移动目标范围.

The next hurdle is how to construct field + text + field as Word doesn't behave "logically" when things are added in this order. The target point remains before the field that's inserted. So it's either necessary to work backwards, or to move the target range after each bit of content.

这是我演示后一种方法的一些代码.插入文本和插入字段在两个单独的过程中,它们将目标 Range 和文本(无论是文字还是字段文本)作为参数.通过这种方式,可以逻辑地构建域代码(第 x 页,共 n 页).目标 Range 从两个过程返回,已经折叠到它的终点,准备追加更多内容.

Here's some code I have the demonstrates the latter approach. Inserting text and inserting fields are in two separate procedures that take the target Range and the text (whether literal or the field text) as parameters. This way the field code can be built up logically (Page x of n). The target Range is returned from both procedures, already collapsed to its end-point, ready for appending further content.

请注意,我更喜欢使用字段的文本(包括任何字段开关)而不是指定字段类型(WdFieldType 枚举)来构造一个字段.这提供了更大的灵活性.我还强烈建议将 PreserveFormatting 参数设置为 false,因为 true 设置会在字段更新时导致非常奇怪的格式设置.它应该只在非常特定的情况下使用(通常涉及链接表).

Note that I prefer to construct a field using the field's text (including any field switches) rather than specifying a field type (the WdFieldType enumeration). This provides greater flexibility. I also highly recommend setting the PreserveFormatting parameter to false as the true setting can result in very odd formatting when fields are updated. It should only be used in very specific instances (usually involving linked tables).

private void btnInsertPageNr_Click(object sender, EventArgs e)
{
    getWordInstance();
    Word.Document doc = null;
    if (wdApp.Documents.Count > 0)
    {
        doc = wdApp.ActiveDocument;
        Word.Range rngHeader = doc.Sections[1].Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
        if (rngHeader.Tables.Count > 0)
        {
            Word.Table tbl = rngHeader.Tables[1];
            Word.Range rngPageNr = tbl.Range.Cells[tbl.Range.Cells.Count].Range;
            //Collapse the range so that it's within the cell and 
            //doesn't include the end-of-cell markers
            object oCollapseStart = Word.WdCollapseDirection.wdCollapseStart;
            rngPageNr.Collapse(ref oCollapseStart);
            rngPageNr = InsertNewText(rngPageNr, "Page ");
            rngPageNr = InsertAField(rngPageNr, "Page");
            rngPageNr = InsertNewText(rngPageNr, " of ");
            rngPageNr = InsertAField(rngPageNr, "NumPages");
        }
    }
}

private Word.Range InsertNewText(Word.Range rng, string newText)
{
    object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
    rng.Text = newText;
    rng.Collapse(ref oCollapseEnd);
    return rng;
}

private Word.Range InsertAField(Word.Range rng,
                      string fieldText)
{
    object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
    object unitCharacter = Word.WdUnits.wdCharacter;
    object oOne = 1;
    Word.Field fld = rng.Document.Fields.Add(rng, missing, fieldText, false);

    Word.Range rngField = fld.Result;
    rngField.Collapse(ref oCollapseEnd);
    rngField.MoveStart(ref unitCharacter, ref oOne);
    return rngField;
}

这篇关于在字头表C#中添加页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆