繁体   English   中英

Aspose.Word页码在右边距

[英]Aspose.Word page number in right margin

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

我的结果pdf预览

怎么做?

我当前的代码:

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合作,担任Developer推广员。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM