How can I debug a corrupt docx file?

前端 未结 4 1129
粉色の甜心
粉色の甜心 2021-02-05 07:17

I have an issue where .doc and .pdf files are coming out OK but a .docx file is coming out corrupt.

In order to solve that I am trying to debug why the .docx is corrup

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-05 08:01

    Many years late, but I found this which actually worked for me. (From https://msdn.microsoft.com/en-us/library/office/bb497334.aspx)

    (wordDoc is a WordprocessingDocument)

    using DocumentFormat.OpenXml.Validation;

            try
            {
                var validator = new OpenXmlValidator();
                var count = 0;
                foreach (var error in validator.Validate(wordDoc))
                {
                    count++;
                    Console.WriteLine("Error " + count);
                    Console.WriteLine("Description: " + error.Description);
                    Console.WriteLine("ErrorType: " + error.ErrorType);
                    Console.WriteLine("Node: " + error.Node);
                    Console.WriteLine("Path: " + error.Path.XPath);
                    Console.WriteLine("Part: " + error.Part.Uri);
                    Console.WriteLine("-------------------------------------------");
                }
    
                Console.WriteLine("count={0}", count);
            }
    
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
    

提交回复
热议问题