Search code examples
.net-coreblazoropenxml-sdk

OpenXml sdk corrupt document


I'm using the open xml sdk to replace placeholder, with the many help from stackoverflow posts I was able to find how to replace my placeholder. Changing those placeholders seems to work which I can check in the changed xml, but for some reason the document seems corrupt no matter what.

What I tried:

change my docx zo ip extract and replaced some text manually and back to zip -> docx also corrupt.

My code:

public void GenerateInvoice(TemplateViewModel viewModel)
        {
            //step, copy file -> read document return body -> change body -> save body -> convert to pdf
            
            
            //read document
            var wordReader = new DocumentReader();
            //var result = wordReader.Reader(@"D:/apps/DeigonAccountancy/DeigonAccountancy/Deigon.Frontend.Blazor/Files/invoiceTemplate.docx", @"D:/apps/DeigonAccountancy/DeigonAccountancy/Deigon.Frontend.Blazor/Files/" + (viewModel.Client.FirstName + "-" + viewModel.Client.LastName) + ".docx");
            
            if (!File.Exists(@"D:/apps/DeigonAccountancy/DeigonAccountancy/Deigon.Frontend.Blazor/Files/invoiceTemplate.docx"))
            {
                throw new FileNotFoundException("tbesta ni kloot");
            }

            File.Copy(@"D:/apps/DeigonAccountancy/DeigonAccountancy/Deigon.Frontend.Blazor/Files/invoiceTemplate.docx", @"D:/apps/DeigonAccountancy/DeigonAccountancy/Deigon.Frontend.Blazor/Files/" + (viewModel.Client.FirstName + "-" + viewModel.Client.LastName) + ".docx", false);

            var stream = File.Open(@"D:/apps/DeigonAccountancy/DeigonAccountancy/Deigon.Frontend.Blazor/Files/" + (viewModel.Client.FirstName + "-" + viewModel.Client.LastName) + ".docx", FileMode.Open);
            
            List<string> innerTexts = new List<string>();
            using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(stream, true))
            {
                //replace item
                var elements = wordDocument.MainDocumentPart.Document.Body.Descendants<SdtElement>().ToList();

                foreach (var item in elements)
                {
                    switch (item.InnerText)
                    {
                        case nameof(TemplateOptions.UserDivision):
                            //replace alias
                            //var alias = item.Descendants<SdtAlias>().FirstOrDefault();
                            //alias.Val.Value = viewModel.Category.Name;

                            //replace child element
                            //var run = item.Descendants<SdtContentRun>().FirstOrDefault();

                            //run.Parent.ReplaceChild(new Run(new Text(viewModel.Category.Name)),run);

                            //var innerRun = run.Descendants<Run>().FirstOrDefault();
                            //var text = run.Descendants<Text>().FirstOrDefault();
                            //text.Text = viewModel.Category.Name;


                            var runs = wordDocument.MainDocumentPart.Document.Body.Descendants<Run>().FirstOrDefault();

                            var text = runs.Descendants<Text>().FirstOrDefault();
                            text.Text =  text.Text.Replace("DEIGON", "TEST");
                            
                            break;

                        case nameof(TemplateOptions.ClientAddress):
                            break;

                        case nameof(TemplateOptions.ClientCompany):
                            break;

                        case nameof(TemplateOptions.ClientVat):
                            break;

                        case nameof(TemplateOptions.HourlyRate):
                            break;

                        case nameof(TemplateOptions.InvoiceComments):
                            break;

                        case nameof(TemplateOptions.InvoiceDate):
                            break;

                        case nameof(TemplateOptions.InvoiceHours):
                            break;

                        case nameof(TemplateOptions.InvoiceId):
                            break;

                        case nameof(TemplateOptions.InvoicePrice):
                            break;

                        case nameof(TemplateOptions.InvoiceTotalExclusive):
                            break;

                        case nameof(TemplateOptions.InvoiceTotalInclusive):
                            break;

                        case nameof(TemplateOptions.InvoiceTotalVat):

                            break;

                        case nameof(TemplateOptions.UserBIC):
                            break;

                        case nameof(TemplateOptions.UserIBAN):
                            break;

                        case nameof(TemplateOptions.UserRightsPolicy):
                            break;

                        default:
                            break;

                    }
                }

                wordDocument.Save();
            }            

            //wordReader.SaveChanges(@"D:/apps/DeigonAccountancy/DeigonAccountancy/Deigon.Frontend.Blazor/Files/" + (viewModel.Client.FirstName + "-" + viewModel.Client.LastName) + ".docx", result);


        }

The simple text replacement worked: Difference check:

Photo

Does anyone know why at the end my document is corrupt?

using .net 5.0 with blazor frontend


Solution

  • You never Close or Dispose the stream.

    The easiest solution would be to use the WordprocessingDocument.Open(<fileName>, ...) overload and eliminate the stream. Otherwise, give the stream its own using() { } block..