Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Current Page n #1515

Closed
adriana-185 opened this issue Aug 28, 2023 · 3 comments
Closed

Get Current Page n #1515

adriana-185 opened this issue Aug 28, 2023 · 3 comments
Assignees

Comments

@adriana-185
Copy link

adriana-185 commented Aug 28, 2023

I have a part of a code that opens a docx document and edits it, it adds several things and one of those is the page number you're currently in, so it should look like "Pag: 1/5". Until last week it worked just fine, but today I noticed that it no longer displays the current page number but displays in its' place "{PAGE}", ending up with "Pag:{PAGE}/5". I'm using this code on a project and both the testing code and the server code have this problem (and I haven't updated the server code in more than a month.

The following code is how it gets the current page number, which worked before:
run.AppendChild(new SimpleField() { Instruction = "PAGE" });

The full part that the code is in:

pageCount = int.Parse(doc.ExtendedFilePropertiesPart.Properties.Pages.Text);
paragraph = td32.AppendChild(new Paragraph(
        new ParagraphProperties(
            new Justification() { Val = JustificationValues.Left },
            new Bold() { Val = OnOffValue.FromBoolean(true) })));
run = paragraph.AppendChild(new Run());
run.RunProperties = new DocumentFormat.OpenXml.Wordprocessing.RunProperties();
fontSize = new DocumentFormat.OpenXml.Wordprocessing.FontSize() { Val = "22" };
run.RunProperties.Append(fontSize);
runFonts = new RunFonts() { Ascii = "Arial", HighAnsi = "Arial" };
run.RunProperties.Append(runFonts);
bold = new Bold() { Val = false };
run.RunProperties.Append(bold);
italic = new Italic() { Val = false };
run.RunProperties.Append(italic);
run.AppendChild(new Text("Pág: "));
run.AppendChild(new SimpleField() { Instruction = "PAGE" });
run.AppendChild(new Text("/" + pageCount));
@tomjebo
Copy link
Collaborator

tomjebo commented Aug 29, 2023

Hi @adriana-185,

The {PAGE} field that your code inserts is working as expected. The problem is that it is not automatically updated. If you right click on the field location in Word's UI, you can select !Update Field and it will show the correct page number. The reason for this is that PAGE or other pagination related fields are calculated at print time by default. Please refer to this article to see the explanation:
Some fields are updated while other fields are not

@tomjebo tomjebo self-assigned this Aug 29, 2023
@tomjebo
Copy link
Collaborator

tomjebo commented Aug 29, 2023

@adriana-185,

I can add a little more here. What I think you're missing is a w:t or text child element for the fldSimple element or SimpleField object. If you do something like this:

    run.RunProperties.Append(italic);
    run.AppendChild(new Text("Pág: "));
    run.AppendChild(new SimpleField(new Text("3")) { Instruction = "PAGE" });
    run.AppendChild(new Text("/" + pageCount));

Then at least you will see the placeholder number, in this case "3" next to the "/5" or whatever the page count ends up being. However, it still stands that this will not update automatically. You will have to tell Word to update the field to reflect the current page and not just the placeholder value you added in code.

The reason you saw "{PAGE}" is due to a setting in Word that tells it to display the field codes. With your source code, the field was always there, it just didn't have a placeholder text like "3" and Word was displaying the field codenames. To toggle this option, go to Options | Advanced | Show document content and you'll see the toggle:

image

Hopefully that will help you.

@tomjebo
Copy link
Collaborator

tomjebo commented Oct 19, 2023

@adriana-185 are you ok to close this?

@tomjebo tomjebo closed this as completed Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants