Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

New bug in ApplyTemplate method after V2.1.0 released

Open
#445 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
backend

Research direction

Start at ApplyTemplate(Stream templateStream, bool includeContent) and reproduce the failure with the provided code using a DOCX containing an empty paragraph. Read the Paragraphs collection behavior and paragraph.Remove(false), especially RemoveParagraphFromCache; done means ApplyTemplate with includeContent set to false completes without InvalidOperationException.

Written by the indexing model from the issue text.

Description

Hello,
My old code stopped working after I updated nuget package from V2.0.0 to V2.3.0. The reason is that ApplyTemplate( Stream templateStream, bool includeContent ) method now throws an System.InvalidOperationException when includeContent is set to false because of those reasons:

 if( !includeContent )
        {
          foreach( Paragraph paragraph in this.Paragraphs )
          {
            paragraph.Remove( false );
          }
        }

In this part of ApplyTemplate method paragraph.Remove( false ); modifies Paragraphs collection, and this causes foreach-loop to break. This is happening because in V2.1.0 (commit 9c431afb) realization of Paragraphs collection was changed and now there is underlying List<Paragraph> _editableParagraphsCollection list which is modified when paragraph.Remove( false ); calls RemoveParagraphFromCache( paragraph );

The code to reproduce the bug is as simple as that:

using (var document = DocX.Create("test.docx"))
            {
                using (var templateStream = new FileStream("template.docx", FileMode.Open, FileAccess.Read))
                {
                    document.ApplyTemplate(templateStream, false);
                }
                document.Save();
            }

where template.docx is any docx created in MS Word, because even empty document includes one empty paragraph. I'm not sure which is the most accurate way to fix this issue.

Dominant language
C#
Stars
1.9k
Forks
483
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from xceedsoftware/DocX

All issues in xceedsoftware/DocX

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.