facebooktwittermenuarrow-up

GemBox Support Forum

TableOfEntries - Figures problem

Hello, I’m having trouble with a Table of figures in my document.

My objective is to merge two Word files, update the first table of content than the first table of figures. Both of the documents have a table of content and a table of figures and I planned on just delete the table of entries of the second document.

I can merge them without any problem (like in here ), I then update the table of content ( like this ).

The problem is that is when I try to update the first table of figures, it is emptied. Using the following code:

var mainTableOfFigures  = (TableOfEntries)destination.GetChildElements(true, ElementType.TableOfEntries).Skip(1).First();
mainTableOfFigures.Update();

I tried with the table of figures of the second document, the same thing, I tried without merging the two files, the same things. It really is happening when I try to update a TableOfEntries that is a figure table.
I also tried to “manually” merge the two table of figures but it does not update the document somehow.

I can’t share the documents as they are confidential. So I know it’s not evident to investigate this with only what I said. But if someone can give me a lead on where I should look to resolve this or give me any advice on how to do it differently that would really help me.

Hi Axel,

The TableOfEntries.Update method currently supports only TOC elements, not TOF.

Nevertheless, can you try using the following UpdateTablesOfEntries extension method:

using System.Linq;
using System.Text.RegularExpressions;

namespace GemBox.Document
{
    public static class GemBoxDocumentHelper
    {
        public static void UpdateTablesOfEntries(this DocumentModel document)
        {
            var sequenceSwitchRegex = new Regex(@"\\c\s+""?(?<Identifier>[\w]+)""?", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            foreach (TableOfEntries toe in document.GetChildElements(true, ElementType.TableOfEntries).Reverse())
            {
                var match = sequenceSwitchRegex.Match(toe.InstructionText);
                if (match.Success)
                    toe.UpdateTableOfFigures(match.Groups["Identifier"].Value);
                else
                    toe.Update();
            }
        }

        private static void UpdateTableOfFigures(this TableOfEntries tof, string identifier)
        {
            tof.Entries.Clear();
            var document = tof.Document;

            if (!document.Styles.Contains("table of figures"))
                document.Styles.Add(new ParagraphStyle("table of figures") { ParagraphFormat = { SpaceAfter = 0 } });

            var style = (ParagraphStyle)document.Styles["table of figures"];

            Element parent = tof.ParentCollection.Content.Start.Parent;
            while (parent.ElementType != ElementType.Section)
                parent = parent.Parent;

            var pageSetup = ((Section)parent).PageSetup;
            var pageMargins = pageSetup.PageMargins;
            var tabStop = new TabStop(pageSetup.PageWidth - pageMargins.Left - pageMargins.Right, TabStopAlignment.Right, TabStopLeader.Dot);

            var seqs = document.GetChildElements(true, ElementType.Field)
                .Cast<Field>()
                .Where(f => f.FieldType == FieldType.Seq && f.GetInstructionText().Contains(identifier))
                .ToArray();

            int counter = 0;
            foreach (var seq in seqs)
            {
                var paragraph = (Paragraph)seq.Parent;
                var text = paragraph.Content.ToString().Trim();
                var bookmark = CreateTocBookmark(paragraph.Inlines, ref counter);
                tof.Entries.Add(CreateTocEntry(document, style, text, bookmark, tabStop));
            }
        }

        private static string CreateTocBookmark(InlineCollection inlines, ref int bookmarkCounter)
        {
            const string TocBookmarkPrefix = "_Toc";

            for (int i = inlines.Count - 1; i >= 0; --i)
            {
                Inline inline = inlines[i];
                if ((inline.ElementType == ElementType.BookmarkStart && ((BookmarkStart)inline).Name.StartsWith(TocBookmarkPrefix)) ||
                    (inline.ElementType == ElementType.BookmarkEnd && ((BookmarkEnd)inline).Name.StartsWith(TocBookmarkPrefix)))
                    inlines.RemoveAt(i);
            }

            DocumentModel document = inlines[0].Document;
            BookmarkCollection bookmarks = document.Bookmarks;

            string bookmarkName;
            do { bookmarkName = TocBookmarkPrefix + ++bookmarkCounter; }
            while (bookmarks[bookmarkName] != null);

            inlines.Insert(0, new BookmarkStart(document, bookmarkName));
            inlines.Add(new BookmarkEnd(document, bookmarkName));

            return bookmarkName;
        }

        private static Paragraph CreateTocEntry(DocumentModel document, ParagraphStyle style, string text, string bookmark, TabStop tabStop)
        {
            var entry = new Paragraph(document,
                new Hyperlink(document, bookmark,
                    new Run(document, text),
                    new SpecialCharacter(document, SpecialCharacterType.Tab),
                    new Field(document, FieldType.PageRef, bookmark + " \\h", "1"))
                { IsBookmarkLink = true });

            entry.ParagraphFormat.Style = style;
            entry.ParagraphFormat.Tabs.Add(tabStop);

            return entry;
        }
    }
}

I hope this helps.

Regards,
Mario

Hi Mario,

Thanks for the answer. I was starting to suspect that it does not work with TOF.

I’m looking into what you gave me. Thank you very much.