EvotecIT / OfficeIMO

Fast and easy to use cross-platform .NET library that creates or modifies Microsoft Word (DocX) and later also Excel (XLSX) files without installing any software. Library is based on Open XML SDK
MIT License
286 stars 50 forks source link

Open Office Writer can't open document created with OfficeIMO #189

Closed Goddfrey closed 1 week ago

Goddfrey commented 8 months ago

I'm getting "general input/output error" from Open Office. Here's the code I used to create a test document:

int rowCount = 5;
int colCount = 2;

using (WordDocument document = WordDocument.Create(path, true))
{
    var table = document.AddTable(rowCount, colCount, WordTableStyle.GridTable5DarkAccent5);
    for (int i = 0; i < rowCount; i++)
    {
        for (int c = 0; c < colCount; c++)
        {
            string text = "test " + i.ToString();
            table.Rows[i].Cells[c].Paragraphs[0].Text = text;
        }
    }

    document.Save();
}

Libre Office opens the same file just fine and I can see the table created. And I don't have MS Word. test.docx

Unfortunately OpenOffice can only read .docx it apparently has no way to save a document as .docx so I cannot check what's missing by creating similar table by hand and then extracting and comparing the two.

I did create a similar simple table docx using LibreOffice, that OpenOffice can open, and I can see quite a few differences. Folder docProps does not exist in the one created by OfficeIMO. Folder word created by Libre Office has 2 subfolders (_rels, theme) and 4 files while the one created by OfficeIMO has same two subfolders and 8 files, and so on. Alas I dont know which of the differences matter and which do not.

PrzemyslawKlys commented 8 months ago

Does it work with empty doc? If not can you attach empty for comparison that works?

Goddfrey commented 8 months ago

Sure. Although both Open Office and Libre Office are free if you want to run some tests of your own.

libre_office_empty.docx OfficeIMO_empty.docx

I created two empty docx files, one using Libre Office and another using this code:

            using (WordDocument document = WordDocument.Create(path, true))
            {

                document.Save();
            }

Open Office does open the one created with Libre Office, and does not open the one created with Office IMO.

PrzemyslawKlys commented 8 months ago

This needs testing:

Old, but maybe still there.

PrzemyslawKlys commented 8 months ago

So i'm pretty confident this is due to forward slash

image image

There are plenty of issues around that:

I saw some project actively changing it:

OpenXMLSDK doesn't expose those paths directly from what I see.

I can see ClosedXML having similar issue:

I am not sure how to fix it. I'm open for fixes/suggestions as I can see it being useful, but don't have high hopes.

I see this code:

But after trying to apply it I get unreadable word. I guess i'm missing some details.

PrzemyslawKlys commented 8 months ago

I've created a PR:

But requires testing, and I am pretty sure this is not the best option to do it like that

Rofliex commented 8 months ago

I have the same problem, but new documents created from a template (I use a copy in memory of a document that is on disk) cannot be opened in MS Word, but work correctly in LibreOffice and Google docs :)

PrzemyslawKlys commented 8 months ago

You mean created by OfficeIMO? Before or after the PR?

Rofliex commented 8 months ago

You mean created by OfficeIMO? Before or after the PR?

Im didnt test with your changes. Used only latest release from nuget.

PrzemyslawKlys commented 8 months ago

Can you test with the PR?

Rofliex commented 8 months ago

Can you test with the PR?

I checked your fix from PR. It doesn't work in my case :( I'll try to get the logs from Ms Word where the docx file parser got an exception, and maybe figure out what's wrong.

Rofliex commented 8 months ago

I found compatibility issues with the property Paragraph .ParagraphAlignment. If you override this property or set it for a new paragraph, it will cause errors in Ms Word and you will not be able to open a new document.

PrzemyslawKlys commented 8 months ago

I found compatibility issues with the property Paragraph .ParagraphAlignment. If you override this property or set it for a new paragraph, it will cause errors in Ms Word and you will not be able to open a new document.

So under which conditions exactly your documents don't open? They don't open in MS Word? What is the code you're using - i'm a bit confused what doesn't work for you?

Rofliex commented 8 months ago

I found compatibility issues with the property Paragraph .ParagraphAlignment. If you override this property or set it for a new paragraph, it will cause errors in Ms Word and you will not be able to open a new document.

So under which conditions exactly your documents don't open? They don't open in MS Word? What is the code you're using - i'm a bit confused what doesn't work for you?

I apologize for flooding this issue. In general, if I add any paragraph to a document and define the ParagraphAlignment property using the library, the document stops opening. If I don’t touch the ParagraphAlignment property, then everything opens fine.

PrzemyslawKlys commented 8 months ago

So in other words Create Word Document, add paragraph, add paragraph alignment and it stops opening in Word? Which version of word?

PrzemyslawKlys commented 8 months ago
public static void Example_BasicWord(string folderPath, bool openWord) {
    Console.WriteLine("[*] Creating standard document with paragraph");
    string filePath = System.IO.Path.Combine(folderPath, "BasicDocumentWithParagraphs.docx");
    using (WordDocument document = WordDocument.Create(filePath)) {
        var paragraph = document.AddParagraph("Adding paragraph with some text");
        paragraph.ParagraphAlignment = JustificationValues.Center;
        //Console.WriteLine(SixLabors.ImageSharp.Color.Blue.ToHexColor());
        //Console.WriteLine(SixLabors.ImageSharp.Color.Crimson.ToHexColor());
        //Console.WriteLine(SixLabors.ImageSharp.Color.Aquamarine.ToHexColor());

        //paragraph.Color = SixLabors.ImageSharp.Color.Red;

        //paragraph = document.AddParagraph("Adding another paragraph with some more text");
        //paragraph.Bold = true;
        //paragraph = paragraph.AddText(" , but now we also decided to add more text to this paragraph using different style");
        //paragraph.Underline = UnderlineValues.DashLong;
        //paragraph = paragraph.AddText(" , and we still continue adding more text to existing paragraph.");
        //paragraph.Color = SixLabors.ImageSharp.Color.CornflowerBlue;

        document.Save(true);
    }
}

I just did this and it worked. You need to be more specific.

tmheath commented 7 months ago

I was using LibreOffice for this from home last year, it seemed to work fine. If it's something specific like how you mentioned the property, I can't say anything, the documents were simple at the time.

tmheath commented 7 months ago

Just noticed this was about OpenOffice... It's a virtually dead project on life support for years, apparently it has a userbase but people really should not be using it, not sure if there's an issue with it being dead or not.