Hello!
I should to add a textbox shape to each page of document to the top right.
Shape must include string with current page number and total pages, for example "Page 1 of 4"
Can you help me? How I can do it?
private void AddTextBoxWithPageNumber()
{
Document doc = new Document("..\\..\\Input\\sample.docx");
foreach(Section section in doc.Sections)
{
InsertHeader(section);
}
string result = "result.docx";
doc.SaveToFile(result,FileFormat.Docx);
}
private void InsertHeader(Section section)
{
//Add a header
HeaderFooter header = section.HeadersFooters.Header;
Paragraph headerParagraph = header.AddParagraph();
//Add a textbox in header paragraph
Spire.Doc.Fields.TextBox textbox = headerParagraph.AppendTextBox(70, 20);
headerParagraph.Format.HorizontalAlignment= Spire.Doc.Documents.HorizontalAlignment.Right;
Paragraph pageNumberParagrap=new Paragraph(section.Document);
//insert page number
pageNumberParagrap.AppendText("Page ");
pageNumberParagrap.AppendField("page number", FieldType.FieldPage);
pageNumberParagrap.AppendText(" of ");
pageNumberParagrap.AppendField("number of pages", FieldType.FieldNumPages);
pageNumberParagrap.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
textbox.ChildObjects.Add(pageNumberParagrap);
textbox.Format.HorizontalAlignment = ShapeHorizontalAlignment.Right;
}