using System.Text.RegularExpressions; using DocumentAutomation.Shared; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using DocxWorkbench.Core; namespace DocxWorkbench.OpenXml; public sealed class DocumentInspector : IDocumentInspector { public DocumentInspectionResult Inspect(string docxPath) { if (!File.Exists(docxPath)) throw new FileNotFoundException("DOCX не найден.", docxPath); var info = new FileInfo(docxPath); using var package = WordprocessingDocument.Open(docxPath, false); var main = package.MainDocumentPart ?? throw new InvalidDataException("В DOCX отсутствует основной документ."); var body = main.Document.Body; var text = main.Document.InnerText; var headers = main.HeaderParts.Count(); var footers = main.FooterParts.Count(); var warnings = new List(); if (Regex.IsMatch(text, "2019|старый|legacy", RegexOptions.IgnoreCase)) warnings.Add("Обнаружены возможные старые проектные реквизиты; проверьте вручную."); if (main.Document.Descendants().Any(s => s.GetFirstChild() is null)) warnings.Add("Есть секция без явной ссылки на header."); return new DocumentInspectionResult { FilePath = docxPath, FileSize = info.Length, LastWriteTimeUtc = info.LastWriteTimeUtc, Sections = Math.Max(1, main.Document.Descendants().Count()), Paragraphs = main.Document.Descendants().Count(), Tables = main.Document.Descendants().Count(), Images = main.ImageParts.Count(), Headers = headers, Footers = footers, HasPageFields = text.Contains("PAGE", StringComparison.OrdinalIgnoreCase), HasNumPagesFields = text.Contains("NUMPAGES", StringComparison.OrdinalIgnoreCase), HasSectionPagesFields = text.Contains("SECTIONPAGES", StringComparison.OrdinalIgnoreCase), Bookmarks = main.Document.Descendants().Count(), ContentControls = main.Document.Descendants().Count(), Comments = package.MainDocumentPart.GetPartsOfType().Count(), HasTrackedChanges = main.Document.Descendants().Any() || main.Document.Descendants().Any(), HasProtectedParts = main.Document.Descendants().Any(), Styles = main.StyleDefinitionsPart?.Styles?.Elements