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.

Sun Mar 01, 2020 8:32 am

Hey,
I'm trying to write a java code using Spire.Office that would add text and page numbering to a word document in the beggining of the first paragraph and in the end of the last paragraph in every page. I know that it kind of sounds like header and footer but that is not what I'm reffering to.
However, I was still unable to implement this. I thought of trying to do so using page break but still unsuccessful.
Thank you in advance.

Tburtons
 
Posts: 1
Joined: Tue Feb 18, 2020 1:36 pm

Mon Mar 02, 2020 7:06 am

Hello,

Thanks for your inquiry.
The following code demonstrates how to insert page numbers into corresponding paragraphs based on page breaks. And attached is my input file with page breaks. If this is not what you want, please provide you input file as well as your desired output to help us do further investigation.
Code: Select all
    Document doc = new Document();
    doc.loadFromFile("file/test.docx");
    //Page number
    int index = 1;
    //Insert page number to the first paragraph
    Paragraph para = doc.getSections().get(0).getParagraphs().get(0);
    TextRange range = new TextRange(doc);
    range.setText(" page " + index + " ");
    para.getChildObjects().insert(0,range);
    //Insert page number to the previous and next paragraphs of the page break
    for (int j = 0; j < doc.getSections().get(0).getParagraphs().getCount(); j++) {
        Paragraph p = doc.getSections().get(0).getParagraphs().get(j);
        //Traverse every child object of a paragraph
        for (int i = 0; i < p.getChildObjects().getCount(); i++) {
            DocumentObject obj = p.getChildObjects().get(i);
            //Find the page break object
            if (obj.getDocumentObjectType().equals(DocumentObjectType.Break)) {
                Break b = (Break) obj;
                //Get the previous paragraph
                Paragraph paragraphPrevious = (Paragraph)b.getOwnerParagraph().getPreviousSibling();
                range = new TextRange(doc);
                range.setText(" page " + index + " ");
                paragraphPrevious.getChildObjects().insert(paragraphPrevious.getChildObjects().getCount(),range);
                index++;
                //Get the next paragraph
                Paragraph paragraphNext = (Paragraph)b.getOwnerParagraph().getNextSibling();
                range = new TextRange(doc);
                range.setText(" page " + index + " ");
                paragraphNext.getChildObjects().insert(0,range);
            }
        }
    }
    //Insert page number to the last paragraph
    int paraCount = doc.getSections().get(0).getParagraphs().getCount();
    para = doc.getSections().get(0).getParagraphs().get(paraCount - 1);
    range = new TextRange(doc);
    range.setText(" page " + index + " ");
    para.getChildObjects().insert(para.getChildObjects().getCount(),range);
    doc.saveToFile("file/result.docx", FileFormat.Docx);


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Doc