Aspose.Word 页码在右边距

Aspose.Word page number in right margin

我使用 Aspose.Word 构建文档。我尝试在右边距添加页码。图片更好解释一下:

My result pdf preview

怎么做?

我当前的代码:

var document = new Document();
        _builder = new DocumentBuilder(document)
        {
            PageSetup =
            {
                Orientation = Orientation.Portrait,
                PaperSize = PaperSize.A4,
                RightMargin = ConvertUtil.MillimeterToPoint(20),
                BottomMargin = ConvertUtil.MillimeterToPoint(35),
                LeftMargin = ConvertUtil.MillimeterToPoint(35),
                TopMargin = ConvertUtil.MillimeterToPoint(35)
            }
        };

        _builder.StartTable();
        _builder.InsertCell();
        _builder.Write("Test test test");
        _builder.EndTable();

        _builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
        _builder.Write("Pages: ");
        _builder.InsertField("PAGE", "");
        _builder.Write("/");
        _builder.InsertField("NUMPAGES");
        document.Save(stream, SaveFormat.Pdf);

对于您的情况,您需要在文档的页脚添加文本框,在其中插入页码字段,并设置其位置。请使用以下修改后的代码示例来获得所需的输出。

var document = new Document();
DocumentBuilder _builder = new DocumentBuilder(document)
{
    PageSetup =
    {
        Orientation = Orientation.Portrait,
        PaperSize = Aspose.Words.PaperSize.A4,
        RightMargin = ConvertUtil.MillimeterToPoint(20),
        BottomMargin = ConvertUtil.MillimeterToPoint(35),
        LeftMargin = ConvertUtil.MillimeterToPoint(35),
        TopMargin = ConvertUtil.MillimeterToPoint(35)
    }
        };

_builder.StartTable();
_builder.InsertCell();
_builder.Write("Test test test");
_builder.EndTable();

_builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);

Shape shape = new Shape(document, ShapeType.TextBox);
shape.Stroked = false;
shape.Width = _builder.CurrentSection.PageSetup.PageWidth;
shape.Height = 50;
shape.Left = 0;
shape.Left = - _builder.CurrentSection.PageSetup.LeftMargin; 
shape.Top = 0;

Paragraph paragraph = new Paragraph(document);
shape.AppendChild(paragraph);

_builder.InsertNode(shape);
_builder.MoveTo(paragraph);
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
_builder.Write("Pages: ");
_builder.InsertField("PAGE", "");
_builder.Write("/");
_builder.InsertField("NUMPAGES");
document.Save(MyDir + "output.pdf", SaveFormat.Pdf);

我在 Aspose 工作,担任开发人员布道师。