【问题标题】:Aspose.Word page number in right marginAspose.Word 页码在右边距
【发布时间】:2017-08-30 00:14:00
【问题描述】:

我使用 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);

【问题讨论】:

    标签: c# aspose aspose.words


    【解决方案1】:

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

    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 合作,担任开发人员传道者。

    【讨论】:

      猜你喜欢
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      • 2016-12-07
      • 1970-01-01
      • 2018-07-21
      • 2016-09-29
      相关资源
      最近更新 更多