Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed May 11, 2016 12:33 pm

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?

yoggyburg
 
Posts: 1
Joined: Wed May 11, 2016 11:34 am

Thu May 12, 2016 2:14 am

Hi,

Thanks for your posting.
In order to present a good effect, I add textbox with page number in header. Sample code is attached here for your reference.
I recommend you to use the latest version, Spire.Doc Pack Version:5.7 .
Code: Select all
 
       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;
        }


Please feel free to contact us if you need further assistance.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc