3

I want to add custom page numbers (like 1/2,2/2) to word document with using Aspose.Words. But I couldn't find any sample for c# language. I tried to overrite footer but i couldn't give a format to page numbers. Pls help! Thanks!


edit


After i tried first answer,it worked as what i want but another problem came up. I adding child documents to main document. I can only formatting main document's number. Child documents still have ordinary page number. Here a sample of code;

   public void AddChildDocs (System.IO.Stream parentStream, List<System.IO.Stream> childStreams)
    {

        doc = new Aspose.Words.Document(parentStream);

        if (Items.Count > 0)
        {
            WordReplacer evaluator = new WordReplacer(this);
            doc.Range.Replace(new Regex(ReplaceRegex), evaluator, false);
        }
        foreach (var item in childStreams)
        {
            Aspose.Words.Document childDoc = new Aspose.Words.Document(item);
            if (Items.Count > 0)
            {
                WordReplacer evaluator = new WordReplacer(this);
                childDoc.Range.Replace(new Regex(ReplaceRegex), evaluator, false);
            }
            doc.AppendDocument(childDoc, ImportFormatMode.KeepSourceFormatting);
        }

        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
        builder.InsertField("PAGE", "");
        builder.Write(" / ");
        builder.InsertField("NUMPAGES", "");
    }
Zehra Subaş
  • 463
  • 7
  • 17

2 Answers2

2

You can get idea from this page in Aspose documentation. Below is the sample code taken from the same page, but only related to custom page numbers.

String src = dataDir + "Page numbers.docx";
String dst = dataDir + "Page numbers_out.docx";

// Create a new document or load from disk
Aspose.Words.Document doc = new Aspose.Words.Document(src);
// Create a document builder
Aspose.Words.DocumentBuilder builder = new DocumentBuilder(doc);
// Go to the primary footer
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
// Add fields for current page number
builder.InsertField("PAGE", "");
// Add any custom text
builder.Write(" / ");
// Add field for total page numbers in document
builder.InsertField("NUMPAGES", "");

// Import new document
Aspose.Words.Document newDoc = new Aspose.Words.Document(dataDir + "new.docx");
// Link the header/footer of first section to previous document
newDoc.FirstSection.HeadersFooters.LinkToPrevious(true);
doc.AppendDocument(newDoc, ImportFormatMode.UseDestinationStyles);
// Save the document
doc.Save(dst);

I work with Aspose as Developer Evangelist.

Saqib Razzaq
  • 1,408
  • 1
  • 10
  • 10
  • Thanks for your reply, I have a main document and i am adding another documents to it. Whit this solution, i can add page number only main document. Is there any way to add page number child documents to. – Zehra Subaş Mar 05 '15 at 07:23
  • The page numbers are set in header/footers. Each section has its own header/footers. Whenever you add a new document, a new section is created, so its header/footers may differ from the main document. One solution is to call LinkToPrevious(true) for all the imported documents. This will keep all your pages consistent. – Saqib Razzaq Mar 05 '15 at 11:13
  • I have updated the answer and imported a new document to main. I have called LinkToPrevious(true), so that it will keep the same page numbering style of the main document. – Saqib Razzaq Mar 05 '15 at 11:14
  • 1
    Thanks a lot, this is totally working as i want! I couldn't vote this answer up because i have low reputation. But after i have anough reputation, i will. Have a nice day. – Zehra Subaş Mar 05 '15 at 11:28
  • This is perhaps the worst API ever for adding such a simple thing as a page number. Why isn't there a simple method like .addFooter(new SimplePageNumberFooter()) in the document? It's like the concept of self documenting code was not just ignored, but outright opposed. This was seemingly purposefully made to be confusing and impossible to use without specific knowledge of these magical words and objects. – Taugenichts May 09 '16 at 19:44
0

Here is the code to set custom page number in aspose.word, when you set page margins and starting page number then it automatically get next page when that particular page area is finished. Try this it will work...

section.PageSetup.PaperSize = PaperSize.Letter;
section.PageSetup.LeftMargin = 10;
section.PageSetup.RightMargin = 10;
section.PageSetup.TopMargin = 00;
section.PageSetup.BottomMargin = 0;
section.PageSetup.HeaderDistance = 50;
section.PageSetup.FooterDistance = 50;
section.PageSetup.Borders.Color = Color.Black;
section.PageSetup.PageStartingNumber = 1;
Michael Dorner
  • 17,587
  • 13
  • 87
  • 117