2017-08-29 168 views
0

我使用Aspose.Word构建文档。我尝试在右边添加页码。图片更好地解释它: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); 

回答

0

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

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 evangelist。