/// <summary>
        /// Validates an excel style sheet
        /// </summary>
        /// <param name="styleSheet">The style sheet to validate</param>
        private static void ValidateStylesheet(Stylesheet styleSheet)
        {
            var validator        = new DocumentFormat.OpenXml.Validation.OpenXmlValidator();
            var validationErrors = validator.Validate(styleSheet).ToList();

            if (validationErrors.Count > 0)
            {
                Console.WriteLine($"There were validation errors with the style sheet {string.Join(Environment.NewLine, validationErrors.Select(r => r.Description))}");
            }
        }
Beispiel #2
0
        public byte[] GetDocument()
        {
            FooterPart footerPart   = this.document.MainDocumentPart.GetPartsOfType <FooterPart>().First();
            string     footerPartId = this.document.MainDocumentPart.GetIdOfPart(footerPart);

            var sectionProperties = new SectionProperties(      // 1440 = 1 inch, 1728 = 1.2 inch
                new FooterReference()
            {
                Type = HeaderFooterValues.Default, Id = footerPartId
            },
                new FooterReference()
            {
                Type = HeaderFooterValues.Even, Id = footerPartId
            },
                new FooterReference()
            {
                Type = HeaderFooterValues.First, Id = footerPartId
            },
                new PageSize()
            {
                Width = 12240, Height = 15840
            },
                new PageMargin()
            {
                Left = 1080, Right = 1080, Top = 1440, Bottom = 1728, Gutter = 0, Header = 720, Footer = 720
            },
                new Columns()
            {
                Space = "720"
            },
                new DocGrid()
            {
                LinePitch = 360
            });

            this.document.MainDocumentPart.Document.Body.Append(sectionProperties);

            // Bug in DocumentFormat.OpenXml adding <numberingIdMacAtClean> at an incorrect location in the document
            var numberingPart           = this.document.MainDocumentPart.NumberingDefinitionsPart;
            var numberingIdMacAtCleanup = numberingPart.Numbering.OfType <NumberingIdMacAtCleanup>().FirstOrDefault();

            if (numberingIdMacAtCleanup != null)
            {
                numberingIdMacAtCleanup.Remove();
            }

            DocumentFormat.OpenXml.Validation.OpenXmlValidator           validator = new DocumentFormat.OpenXml.Validation.OpenXmlValidator(FileFormatVersions.Office2010);
            List <DocumentFormat.OpenXml.Validation.ValidationErrorInfo> errors    = validator.Validate(this.document).ToList();

            if (errors.Count > 0)
            {
                Log.For(this).Error("Exporting IG with id " + this.implementationGuide.Id + " produced the following OpenXml validation errors: ");

                foreach (var error in errors)
                {
                    Log.For(this).Error("Description: " + error.Description + "\r\nPath: " + error.Path + "\r\n");
                }
            }

            this.document.Close();

            // Get the data back from the reader now that the doc is saved/closed
            this.docStream.Position = 0;
            byte[] buffer = new byte[this.docStream.Length];
            this.docStream.Read(buffer, 0, (int)this.docStream.Length);

            return(buffer);
        }