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

AddAlternativeFormatImportPart produces corrupted docx document on net48, but works fine on net4.8 #1818

Closed
JerryJian opened this issue Nov 4, 2024 · 2 comments
Assignees

Comments

@JerryJian
Copy link

JerryJian commented Nov 4, 2024

Describe the bug
Run the following code on .net48 and .net8.0, and then extract the generated docx file with 7zip, the .net48 output file has an error:

data error: word\_rels\document.xml.rels

The code:

internal class Program
{
    static void Main(string[] args)
    {
        var m_stream = new MemoryStream();
        GenerateDocx(m_stream);
        m_stream.Position = 0;

        var mydoc = WordprocessingDocument.Open(m_stream, true);
        var mainPart = mydoc.MainDocumentPart!;

        var alternativeFormatImportPart = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html);
        alternativeFormatImportPart.FeedData(new MemoryStream(new UTF8Encoding(true).GetBytes("<html>Hello</html>")));
        mydoc.Save();

        using var fs = new FileStream("result.docx", FileMode.Create, FileAccess.ReadWrite);
        m_stream.Position = 0;
        m_stream.CopyTo(fs);
    }

    private static void GenerateDocx(MemoryStream m_stream)
    {
        using var wpDocument = WordprocessingDocument.Create(m_stream, WordprocessingDocumentType.Document);
        MainDocumentPart mainPart = wpDocument.AddMainDocumentPart();
        mainPart.Document = new Document(new Body(new Paragraph(new Run(new Text("Here comes HTML {{ds}:html}")))));
        wpDocument.Save();
    }
}

Screenshots
Image

To Reproduce
The code in the Describe the bug section.

Steps to reproduce the behavior:

  1. Create an console application, and paste the Program code above.
  2. Switch to net48, run the program, the output docx file is in the output directory of the project.
  3. Extract the output docx file with 7zip.
  4. See error like described above.

Observed behavior
The output file of net48 is corrupted

Expected behavior
The files produced by net48 are the same as those produced by net8.0

Desktop (please complete the following information):

  • OS: Windows 11
  • .NET Target: .NET Framework 4.8 and .NET 8.0
  • DocumentFormat.OpenXml Version: 3.1.1

Additional context
Add any other context about the problem here.

Edit
The OpenSettings is not relevant to the current problem, so it has been removed.

@tomjebo tomjebo self-assigned this Nov 4, 2024
@twsouthwick
Copy link
Member

@JerryJian is this a regression? i.e. did it work before?

It's generally expected to not use the stream for anything else until the package has been disposed. I'm honestly surprised it worked on .NET 8 - I'd expect it to not have finalized some things before dispose.

Regardless, the following works:

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO;
using System.Text;

var m_stream = new MemoryStream();
GenerateDocx(m_stream);
m_stream.Position = 0;

using (var mydoc = WordprocessingDocument.Open(m_stream, true))
{
    var mainPart = mydoc.MainDocumentPart!;

    var alternativeFormatImportPart = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html);
    alternativeFormatImportPart.FeedData(new MemoryStream(new UTF8Encoding(true).GetBytes("<html>Hello</html>")));
    mydoc.Save();
}

using var fs = new FileStream("result.docx", FileMode.Create, FileAccess.ReadWrite);
m_stream.Position = 0;
m_stream.CopyTo(fs);

static void GenerateDocx(MemoryStream m_stream)
{
    using var wpDocument = WordprocessingDocument.Create(m_stream, WordprocessingDocumentType.Document);
    MainDocumentPart mainPart = wpDocument.AddMainDocumentPart();
    mainPart.Document = new Document(new Body(new Paragraph(new Run(new Text("Here comes HTML {{ds}:html}")))));
    wpDocument.Save();
}

notice the using around the package manipulations.

I'm going to close this - however, if this worked on previous versions (i.e. 2.x), please reopen and I'll see what changed.

@JerryJian
Copy link
Author

@twsouthwick Thank you very much, it's my usage problem. I haven't used the previous version like this, it's not a regression problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants