private static int UpdateTestDocument(Stream stream, int start, int end)
        {
            using WordprocessingDocument wordDocument = WordprocessingDocument.Open(stream, true);
            wordDocument.AddParagraphIdFeature();

            var paraIdCollection = wordDocument.Features.GetRequired <IParagraphIdCollectionFeature>();

            MainDocumentPart part = wordDocument.MainDocumentPart !;
            Body             body = part.Document.Body !;

            // The MainDocumentPart's DOM tree must have been loaded by accessing
            // the Document property before determining the number of w14:paraId
            // values currently existing in the scope of the WordprocessingDocument.
            Assert.Equal(start, paraIdCollection.Count);

            for (var i = start; i < end; i++)
            {
                var paragraph = body.AppendChild(new Paragraph());
                paragraph.AppendChild(new Run(new Text(paragraph.ParagraphId !.Value !)));
            }

            Assert.Equal(end, paraIdCollection.Count);

            return(end - start);
        }
        public void CreateUniqueParagraphId_IterativelyUpdatedWordDocument_AllParaIdsUnique()
        {
            const int iterations = 3;
            const int count      = 10000;

            // Create new document in first iteration.
            using var stream = new MemoryStream();
            var total = CreateTestDocument(stream, count);

            for (var i = 1; i < iterations; i++)
            {
                // Update document in second and following iterations.
                total += UpdateTestDocument(stream, i * count, (i + 1) * count);
            }

            // Assert that we have the expected total number of w14:paraId values
            // and no duplicates.
            using WordprocessingDocument wordDocument = WordprocessingDocument.Open(stream, true);
            wordDocument.AddParagraphIdFeature();
            var paraIdCollection = wordDocument.Features.GetRequired <IParagraphIdCollectionFeature>();

            Body body = wordDocument.MainDocumentPart !.Document !.Body !;

            Assert.Equal(total, body.Elements <Paragraph>().Count());
            Assert.Equal(total, paraIdCollection.Count);
        }
Example #3
0
        private static void AddDocumentTask(string filePath)
        {
            Console.WriteLine("Processing presentation: {0}", filePath);

            using WordprocessingDocument docPackage = WordprocessingDocument.Open(filePath, true);
            string newFn = filePath.Replace(".docx", "_new.docx");

            using WordprocessingDocument newDocumentPackage = (WordprocessingDocument)docPackage.SaveAs(newFn.ToString());
            newDocumentPackage.AddParagraphIdFeature();
            MainDocumentPart mdp = newDocumentPackage.MainDocumentPart;

            string strCommentId = "3";
            string comment      = " Here's another sentence that is just too long. Shorten it please.";
            string run          = "The introduction to this article.";

            // userids, providers and names will have to be programmatically accessed via directory services and must be valid. These are just for example
            User tony  = new("S::[email protected]::3063813b-f01d-4030-9808-501a178e7963", "John Doe", "AD", "@John Doe", "*****@*****.**");
            User bruce = new("S::[email protected]::ec6240b1-52a3-46dd-9697-ef7bcc7a29e8", "Jane Doe", "AD", "@Jane Doe", "*****@*****.**");

            AddNewParagraphRunWithComment(mdp, strCommentId, run);

            AddMentionComment(mdp, strCommentId, bruce.Mention, comment, tony, bruce);

            string taskStr = string.Concat(bruce.Mention, " ", comment);

            AddNewTask(mdp, taskStr, bruce, tony);
        }
Example #4
0
        /// <summary>
        /// Registers a document to use a shared paragragraph collection, which potentially may ensure uniqueness among many documents.
        /// </summary>
        /// <param name="shared">Shared collection feature.</param>
        /// <param name="doc">Document to register with shared paragraph collection.</param>
        /// <returns></returns>
        public static ISharedFeature <IParagraphIdCollectionFeature> Add(this ISharedFeature <IParagraphIdCollectionFeature> shared, WordprocessingDocument doc)
        {
            doc.AddPackageEventsFeature();
            doc.AddParagraphIdFeature();

            var paragraphIdCollection = doc.Features.GetRequired <IParagraphIdCollectionFeature>();

            shared.Add(paragraphIdCollection);
            doc.Features.SetDisposable(new ParagraphIdSnapshotGenerator(paragraphIdCollection, shared, doc.Features.GetRequired <IPackageEventsFeature>()));
            doc.Features.Set(shared);
            doc.Features.Set(shared.Composite);

            return(shared);
        }
Example #5
0
        /// <summary>
        /// Registers a document for a shared paragraph id collection.
        /// </summary>
        /// <param name="doc">Document to register for a shared collection.</param>
        /// <returns>The existing or new shared collection.</returns>
        public static ISharedFeature <IParagraphIdCollectionFeature> AddSharedParagraphIdFeature(this WordprocessingDocument doc)
        {
            var existingShared = doc.Features.Get <ISharedFeature <IParagraphIdCollectionFeature> >();

            if (existingShared is not null)
            {
                return(existingShared);
            }

            doc.AddParagraphIdFeature();

            var sharedFeature = new SharedParagraphIdCollectionFeature
            {
                doc.Features.GetRequired <IParagraphIdCollectionFeature>(),
            };

            doc.Features.Set <IParagraphIdCollectionFeature>(sharedFeature);
            doc.Features.Set <ISharedFeature <IParagraphIdCollectionFeature> >(sharedFeature);

            return(sharedFeature);
        }
        private static int AddOtherParts(Stream stream, int count)
        {
            const int firstId = 0;

            using WordprocessingDocument wordDocument = WordprocessingDocument.Open(stream, true);
            wordDocument.AddParagraphIdFeature();

            MainDocumentPart mainDocumentPart = wordDocument.MainDocumentPart !;

            // Add Header.
            var headerParagraph = new Paragraph(new Run(new Text("Header")));

            var headerPart = mainDocumentPart.AddNewPart <HeaderPart>();

            headerPart.Header = new Header(headerParagraph);

            // Add Footer.
            var footerParagraph = new Paragraph(new Run(new Text("Footer")));

            var footerPart = mainDocumentPart.AddNewPart <FooterPart>();

            footerPart.Footer = new Footer(footerParagraph);

            // Add Comments.
            var comments = new Comments();

            for (var id = firstId; id < firstId + count; id++)
            {
                var paragraph = new Paragraph(new Run(new Text($"Comment #{id}")));

                comments.AppendChild(new Comment(paragraph)
                {
                    Id     = id.ToString(),
                    Author = $"Author #{id}",
                });
            }

            var commentsPart = mainDocumentPart.AddNewPart <WordprocessingCommentsPart>();

            commentsPart.Comments = comments;

            // Add Footnotes
            var footnotes = new Footnotes();

            for (var id = firstId; id < firstId + count; id++)
            {
                var paragraph = new Paragraph(new Run(new Text($"Footnote #{id}")));

                footnotes.AppendChild(new Footnote(paragraph)
                {
                    Id = id
                });
            }

            var footnotesPart = mainDocumentPart.AddNewPart <FootnotesPart>();

            footnotesPart.Footnotes = footnotes;

            // Add Endnotes.
            var endnotes = new Endnotes();

            for (var id = firstId; id < firstId + count; id++)
            {
                var paragraph = new Paragraph(new Run(new Text($"Endnote #{id}")));

                endnotes.AppendChild(new Endnote(paragraph)
                {
                    Id = id
                });
            }

            var endnotesPart = mainDocumentPart.AddNewPart <EndnotesPart>();

            endnotesPart.Endnotes = endnotes;

            return((3 * count) + 2);
        }