Нумерация страниц - это процесс применения последовательности цифр к страницам документа. Сам номер может
отображаться в разных местах страницы. Иногда Вы можете не захотеть, чтобы номера страниц, отображаемые
в Вашем верхнем или нижнем колонтитуле, совпадали с фактическими номерами страниц
документа.
Этот пример кода покажет Вам, как создать новый
документ с нумерацией страниц.
Полный код
using System;
using System.Collections.Generic;
using System.Linq;
using SautinSoft.Document;
namespace Sample
{
class Sample
{
static void Main(string[] args)
{
// Get your free 100-day key here:
// https://sautinsoft.com/start-for-free/
PageNumbering();
}
/// <summary>
/// Creates a new document with page numbering: Page N of M.
/// </summary>
/// <remarks>
/// https://sautinsoft.com/products/document/help/net/developer-guide/page-numbering.php
/// </remarks>
public static void PageNumbering()
{
string documentPath = @"PageNumbering.docx";
// Let's create a new document with multiple pages.
DocumentCore dc = new DocumentCore();
string[] pagesText = new string[] { "One", "Two", "Three", "Four", "Five" };
Random r = new Random();
// Create a new section.
Section section = new Section(dc);
dc.Sections.Add(section);
// We place our page numbers into the footer.
// Therefore we've to create a footer.
HeaderFooter footer = new HeaderFooter(dc, HeaderFooterType.FooterDefault);
// Create a new paragraph to insert a page numbering.
// So that, our page numbering looks as: Page N of M.
Paragraph par = new Paragraph(dc);
par.ParagraphFormat.Alignment = HorizontalAlignment.Left;
CharacterFormat cf = new CharacterFormat() { FontName = "Arial", Size = 12.0 };
par.Content.Start.Insert("Page ", cf.Clone());
// Page numbering is a Field.
// Create two fields: FieldType.Page and FieldType.NumPages.
Field fPage = new Field(dc, FieldType.Page);
fPage.CharacterFormat = cf.Clone();
par.Content.End.Insert(fPage.Content);
par.Content.End.Insert(" of ", cf.Clone());
Field fPages = new Field(dc, FieldType.NumPages);
fPages.CharacterFormat = cf.Clone();
par.Content.End.Insert(fPages.Content);
// Add the paragraph with Fields into the footer.
footer.Blocks.Add(par);
// Add the footer into the section.
section.HeadersFooters.Add(footer);
// Add some paragraphs with page breaks in the document,
// to make several pages.
foreach (string text in pagesText)
{
Paragraph p = new Paragraph(dc);
p.ParagraphFormat.Alignment = HorizontalAlignment.Center;
section.Blocks.Add(p);
string color = String.Format("#{0:X2}{1:X2}{2:X2}", r.Next(0,255),r.Next(0,255),r.Next(0,255));
p.Content.Start.Insert(text, new CharacterFormat() { FontName = "Arial", Size = 72.0, FontColor = new Color(color) });
if (text!=pagesText.Last())
p.Content.End.Insert(new SpecialCharacter(dc, SpecialCharacterType.PageBreak).Content);
}
// Save our document into DOCX format.
dc.Save(documentPath, new DocxSaveOptions());
// Open the result for demonstration purposes.
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(documentPath) { UseShellExecute = true });
}
}
}
Imports System
Imports System.IO
Imports System.Linq
Imports SautinSoft.Document
Module Sample
Sub Main()
PageNumbering()
End Sub
''' Get your free 100-day key here:
''' https://sautinsoft.com/start-for-free/
''' <summary>
''' Creates a new document with page numbering: Page N of M.
''' </summary>
''' <remarks>
''' https://sautinsoft.com/products/document/help/net/developer-guide/page-numbering.php
''' </remarks>
Sub PageNumbering()
Dim documentPath As String = "PageNumbering.docx"
' Let's create a new document with multiple pages.
Dim dc As New DocumentCore()
Dim pagesText() As String = {"One", "Two", "Three", "Four", "Five"}
Dim r As New Random()
' Create a new section.
Dim section As New Section(dc)
dc.Sections.Add(section)
' We place our page numbers into the footer.
' Therefore we've to create a footer.
Dim footer As New HeaderFooter(dc, HeaderFooterType.FooterDefault)
' Create a new paragraph to insert a page numbering.
' So that, our page numbering looks as: Page N of M.
Dim par As New Paragraph(dc)
par.ParagraphFormat.Alignment = HorizontalAlignment.Left
Dim cf As New CharacterFormat() With {
.FontName = "Arial",
.Size = 12.0
}
par.Content.Start.Insert("Page ", cf.Clone())
' Page numbering is a Field.
' Create two fields: FieldType.Page and FieldType.NumPages.
Dim fPage As New Field(dc, FieldType.Page)
fPage.CharacterFormat = cf.Clone()
par.Content.End.Insert(fPage.Content)
par.Content.End.Insert(" of ", cf.Clone())
Dim fPages As New Field(dc, FieldType.NumPages)
fPages.CharacterFormat = cf.Clone()
par.Content.End.Insert(fPages.Content)
' Add the paragraph with Fields into the footer.
footer.Blocks.Add(par)
' Add the footer into the section.
section.HeadersFooters.Add(footer)
' Add some paragraphs with page breaks in the document,
' to make several pages.
For Each text As String In pagesText
Dim p As New Paragraph(dc)
p.ParagraphFormat.Alignment = HorizontalAlignment.Center
section.Blocks.Add(p)
Dim color As String = String.Format("#{0:X2}{1:X2}{2:X2}", r.Next(0, 255), r.Next(0, 255), r.Next(0, 255))
p.Content.Start.Insert(text, New CharacterFormat() With {
.FontName = "Arial",
.Size = 72.0,
.FontColor = New Color(color)
})
If (text <> pagesText.Last()) Then
p.Content.End.Insert((New SpecialCharacter(dc, SpecialCharacterType.PageBreak)).Content)
End If
Next text
' Save our document into DOCX format.
dc.Save(documentPath, New DocxSaveOptions())
' Open the result for demonstration purposes.
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(documentPath) With {.UseShellExecute = True})
End Sub
End Module
Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.com или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже: