dotnet / dotnet/Open-XML-SDK

AddAlternativeFormatImportPart produces malformed documents if both parent and child contain shape-lines or similar objects

Open
#917 6 comments 0 reactions 1 assignee Claimed by @tomjebo View on GitHub
request: feature
Dominant language
C#
Stars
4.6k
Forks
605
PR merge metrics
No merged PRs in 30d

Description

**Description**

Document is malformed if I use AddAlternativeFormatImportPart on a document with a shape-line (or similar objects) in the root *and* the attached documents.

**Information**

- .NET Target: .NET Framework 4.6.2
- DocumentFormat.OpenXml Version: 2.12.3

**Repro**

Create 2 word documents "Template.docx" and "ExampleAttachment.docx". Insert a single shape-line in both.
Note this was originally discovered using a shape-line and an attached image, but it can work with a shape-line.

```csharp
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReproAddAlternativeFormatImportPartCorruption
{
class Program
{
static void Main(string[] args)
{
var iterations = 1;
var outputPath = $"test_{DateTime.Now.ToString("yyyyMMdd-hhmmss")}.docx";
var attachmentPath = "ExampleAttachment.docx";
File.Copy("Template.docx", outputPath);

var attachmentBytes = File.ReadAllBytes(attachmentPath);
using (var doc = WordprocessingDocument.Open(outputPath, true))
{
var firstPar = doc.MainDocumentPart.Document.Body.FirstChild;

for (int i = 0; i < iterations; i++)
{
var attachmentHeading = $"{attachmentPath} {i}";
var xmlFileId = attachmentHeading
.Replace(".", "_")
.Replace(" ", "_");

var compositeElements = AddDocumentAttachment(attachmentBytes, attachmentHeading, xmlFileId, doc.MainDocumentPart, AlternativeFormatImportPartType.WordprocessingML);
foreach (var element in compositeElements)
{
firstPar.InsertBeforeSelf(element);
}
}
doc.Save();
}

}

private static IEnumerable AddDocumentAttachment(
byte[] fileData,
string attachmentHeading,
string xmlFileId,
MainDocumentPart mainPart,
AlternativeFormatImportPartType? alternativeFormatImportPartType)
{

// Document is automatically saved and closed onDispose.
var chunk = mainPart.AddAlternativeFormatImportPart(alternativeFormatImportPartType.Value, xmlFileId);
using (var chunkStream = chunk.GetStream(FileMode.Create, FileAccess.Write))
using (var w = new BinaryWriter(chunkStream))
{
w.Write(fileData);
w.Flush();
}

var altChunk = new AltChunk
{
Id = xmlFileId
};

mainPart.Document.Save();

return new OpenXmlCompositeElement[]{
altChunk
};

}
}
}
```

**Observed**

Open the file in Word (Office 2019, version 1808, build 10372.20060), get the following error:

> We're sorry. We can't open .docx because we found a problem with its contents [OK] [Details]

> Details:

> HRESULT 0x800004005

> Location: Part: /word/document.xml, Line: 0, Column: 0

**Expected**

Create a document that does not cause errors.

**Additional notes**

Initially I discovered this issue happening inconsistently - like, I would attach 4 documents and it would be okay, but 5 was too much. And the only attached objects within the document were images.

Now, I can raise it consistently with the method described above, using documents created from scratch. If needed, I can send the docx files that will reproduce this issue.

If I use the same process *without* a shapes-line drawn into one or both of the files, the error does not occur.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.