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

Table of Contents - Completion #582

Merged
merged 9 commits into from
Jun 26, 2021

Conversation

AnonymousWalker
Copy link

@AnonymousWalker AnonymousWalker commented Jun 19, 2021

This PR completes the functionality of creating Table of Contents (TOC) which was not working normally before.
After digging into the way Microsoft Words generates TOC and updates it, I found that there are two requirements:

  1. The styles.xml in the .docx file should have those "Heading..." and other stuff that the TOC needed to look for and update pagination.
  2. Obviously, we need to set style for the paragraphs that need to be included in the TOC: "Heading{#}"

Before: xwpfDocument.CreateToc() will produces

image
Press Shift + F9 to see field:
image


After:

image
Shift + F9:
image

Sample Code:

            var outputFile = "output/dir/location/filename.docx"
            XWPFDocument newDoc = new XWPFDocument();

            XWPFParagraph newHeader = newDoc.CreateParagraph();
            newHeader.Style = "Heading1"; // for TOC entry
            newHeader.SpacingAfter = 200;
            XWPFRun headerText = newHeader.CreateRun();
            headerText.SetText("Matthew");
            headerText.FontSize = 24;

            newDoc.CreateTOC();

            using (Stream str = File.Create(outputFile))
            {
                newDoc.Write(str);
            }

Reference: WycliffeAssociates#5. This fork is maintained by our organization and it's actively being used for converting different format to .docx

@tonyqus tonyqus added this to the NPOI 2.5.5 milestone Jun 19, 2021
@tonyqus tonyqus added the docx label Jun 26, 2021
@tonyqus tonyqus changed the base branch from master to DocxFixes June 26, 2021 05:25
@tonyqus tonyqus merged commit 34ae23a into nissl-lab:DocxFixes Jun 26, 2021
@tonyqus tonyqus modified the milestones: NPOI 2.5.5, NPOI 2.5.4 Jul 5, 2021
@tonyqus tonyqus mentioned this pull request Jul 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants