dotnet / Open-XML-SDK

Open XML SDK by Microsoft
https://www.nuget.org/packages/DocumentFormat.OpenXml/
MIT License
4k stars 544 forks source link

Bug with Open XML SDK #1700

Open cjalex1313 opened 6 months ago

cjalex1313 commented 6 months ago

Describe the bug Docx file generated with OpenXML isn't able to be exported by soffice in linux (has something to do with the content)

To Reproduce

            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (var doc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Create(memoryStream, DocumentFormat.OpenXml.WordprocessingDocumentType.Document, true))
                {
                    MainDocumentPart mainPart = doc.AddMainDocumentPart();
                    mainPart.Document = new Document();
                    Body body = mainPart.Document.AppendChild(new Body());
var table = new Table();
TableProperties tableProperties = new TableProperties();
tableProperties.TableWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct };
table.AppendChild(tableProperties);
TableGrid tableGrid = new TableGrid(
  new GridColumn(),
  new GridColumn()
);
table.AppendChild(tableGrid);
TableRow tableRow = new TableRow();
TableCell tc1 = new TableCell(
                new TableCellProperties(new TableCellWidth() { Width = "2500", Type = TableWidthUnitValues.Pct }),
                new Paragraph(new Run(
                        new RunProperties(new FontSize() { Val = "56" }, new RunFonts() { Ascii = "Times New Roman" }),
                        new Text("Patient Hours Detail")
                    )
                )
);
TableCell tc2 = new TableCell(
                new TableCellProperties(new TableCellWidth() { Width = "2500", Type = TableWidthUnitValues.Pct }),
                new Paragraph(new ParagraphProperties(new Justification() { Val = JustificationValues.Right }), new Run(new RunProperties(new FontSize() { Val = "34" }, new RunFonts() { Ascii = "Times New Roman" }), new Text(model.CaseName))),
                new Paragraph(new ParagraphProperties(new Justification() { Val = JustificationValues.Right }), new Run(new RunProperties(new FontSize() { Val = "26" }, new RunFonts() { Ascii = "Times New Roman" }), new Text($"{model.StartDate.ToString("MM/dd/yyyy")} through {model.EndDate.ToString("MM/dd/yyyy")}")))
);
tableRow.Append(tc1);
tableRow.Append(tc2);
table.AppendChild(tableRow);
body.Append(table);
doc.Save();
memoryStream.Position = 0;
var byteArray = memoryStream.ToArray();
return byteArray;

then save the byteArray as "sdfomsef.docx" locally

then soffice --headless --convert-to pdf sdfomsef.docx

Error: source file could not be loaded

Expected behavior If I try remove the 2nd table cell this works (this line tableRow.Append(tc2);)

Desktop (please complete the following information):

twsouthwick commented 3 months ago

I'm not sure what soffice is. Does it open in word?

mikeebowen commented 2 months ago

Hi @cjalex1313, I'm able to recreate the issue and error you see with soffice, but removing the 2nd table cell doesn't fix it for me. I even created a .docx with no table using Word and tried to convert it to a PDF with soffice, but it also got the same error. I'm using Ubuntu SSL on Windows, so it's not the same environment. Are you able to use soffice to convert any docx to pdf?