Exemple #1
0
        private static string ValidateWordDocument(OpenXmlPackage wordProcessingDocument)
        {
            StringBuilder stringBuilder = new StringBuilder();

            try
            {
                OpenXmlValidator validator = new OpenXmlValidator();
                int count = 0;
                foreach (ValidationErrorInfo error in
                         validator.Validate(wordProcessingDocument))
                {
                    count++;
                    stringBuilder.AppendLine("Error " + count);
                    stringBuilder.AppendLine("Description: " + error.Description);
                    stringBuilder.AppendLine("ErrorType: " + error.ErrorType);
                    stringBuilder.AppendLine("Node: " + error.Node);
                    stringBuilder.AppendLine("Path: " + error.Path.XPath);
                    stringBuilder.AppendLine("Part: " + error.Part.Uri);
                    stringBuilder.AppendLine("-------------------------------------------");
                }

                stringBuilder.AppendLine("Error Count: " + count);
            }
            catch (Exception ex)
            {
                stringBuilder.AppendLine(ex.Message);
            }

            wordProcessingDocument.Close();

            return(stringBuilder.ToString());
        }
 /// <summary>
 /// Method to make sure that all unmanaged resources are released properly.
 /// </summary>
 public void Dispose()
 {
     if (_oPkg != null)
     {
         _oPkg.Close();
         _oPkg.Dispose();
         _oPkg = null;
     }
     if (_pkg != null)
     {
         _pkg.Close();
         _pkg = null;
     }
     if (_stream != null)
     {
         _stream.Close();
         _stream.Dispose();
         _stream = null;
     }
 }
Exemple #3
0
        public static MemoryStream GetWordDocumentFromTemplateWithTempFile()
        {
            string tempFileName = Path.GetTempFileName();
            var    templatePath = AppDomain.CurrentDomain.BaseDirectory + @"Controllers\" + templateFileName;

            using (var document = WordprocessingDocument.CreateFromTemplate(templatePath))
            {
                var body = document.MainDocumentPart.Document.Body;

                //add some text
                Paragraph paraHeader = body.AppendChild(new Paragraph());
                Run       run        = paraHeader.AppendChild(new Run());
                run.AppendChild(new Text("This is body text"));

                OpenXmlPackage savedDoc = document.SaveAs(tempFileName); // Save result document, not modifying the template
                savedDoc.Close();                                        // can't read if it's open
                document.Close();
            }

            var memoryStream = new MemoryStream(File.ReadAllBytes(tempFileName)); // this works but I want to avoid saving and reading file

            //memoryStream.Position = 0; // should I rewind it?
            return(memoryStream);
        }
        public void CreateWithNoAutoSaveTest()
        {
            this.MyTestInitialize(TestContext.GetCurrentMethod());
            var testfiles = CopyTestFiles("bvt")
                            .Where(fi => fi.IsOpenXmlFile());

            OpenXmlPackage createdPackage = null;

            foreach (var testfile in testfiles)
            {
                string newlyCreatedName = null;
                if (testfile.IsWordprocessingFile())
                {
                    newlyCreatedName = Path.Combine(TestUtil.TestResultsDirectory, Guid.NewGuid().ToString() + ".docx");
                }
                else if (testfile.IsSpreadsheetFile())
                {
                    newlyCreatedName = Path.Combine(TestUtil.TestResultsDirectory, Guid.NewGuid().ToString() + ".xlsx");
                }
                else if (testfile.IsPresentationFile())
                {
                    newlyCreatedName = Path.Combine(TestUtil.TestResultsDirectory, Guid.NewGuid().ToString() + ".pptx");
                }

                Log.Comment("Opening source package in readOnly mode...");
                var sourcePackage = testfile.OpenPackage(false);
                Log.Comment("Creating new package with overload that no autoSave option");
                if (testfile.IsWordprocessingFile())
                {
                    createdPackage = WordprocessingDocument.Create(newlyCreatedName, WordprocessingDocumentType.Document);
                }
                else if (testfile.IsSpreadsheetFile())
                {
                    createdPackage = SpreadsheetDocument.Create(newlyCreatedName, SpreadsheetDocumentType.Workbook);
                }
                else if (testfile.IsPresentationFile())
                {
                    createdPackage = PresentationDocument.Create(newlyCreatedName, PresentationDocumentType.Presentation);
                }
                else
                {
                    Log.Fail("Unexpected document type passed in.");
                }
                if (createdPackage != null)
                {
                    Log.Comment("Feeding main part with DOM operations...");
                    duplicteMainPart(sourcePackage, createdPackage);
                    Log.Comment("Closing package...");
                    createdPackage.Close();
                }

                Log.Warning("Reopening...Expecting null for root element of main part as AutoSave default value is true thus DOM changes will be saved.");
                if (testfile.IsWordprocessingFile())
                {
                    createdPackage = WordprocessingDocument.Open(newlyCreatedName, false);
                }
                else if (testfile.IsSpreadsheetFile())
                {
                    createdPackage = SpreadsheetDocument.Open(newlyCreatedName, false);
                }
                else if (testfile.IsPresentationFile())
                {
                    createdPackage = PresentationDocument.Open(newlyCreatedName, false);
                }
                else
                {
                    Log.Fail("Unexpected document type passed in.");
                }
                var mainpart = createdPackage.MainPart();
                var dom      = mainpart.RootElement;
                if (dom != null)
                {
                    Log.Pass("Root element of main part is not null just as expected.");
                }
                else
                {
                    Log.Fail("Null root element of main part. Something must be wrong.");
                }

                createdPackage.Close();
                FileInfo fz = new FileInfo(newlyCreatedName);
                if (fz.Exists)
                {
                    fz.Delete();
                }
            }
        }
        private static string ValidateWordDocument(OpenXmlPackage wordProcessingDocument)
        {
            StringBuilder stringBuilder = new StringBuilder();
                try
                {
                    OpenXmlValidator validator = new OpenXmlValidator();
                    int count = 0;
                    foreach (ValidationErrorInfo error in
                        validator.Validate(wordProcessingDocument))
                    {
                        count++;
                        stringBuilder.AppendLine("Error " + count);
                        stringBuilder.AppendLine("Description: " + error.Description);
                        stringBuilder.AppendLine("ErrorType: " + error.ErrorType);
                        stringBuilder.AppendLine("Node: " + error.Node);
                        stringBuilder.AppendLine("Path: " + error.Path.XPath);
                        stringBuilder.AppendLine("Part: " + error.Part.Uri);
                        stringBuilder.AppendLine("-------------------------------------------");
                    }

                    stringBuilder.AppendLine("Error Count: " + count);
                }
                catch (Exception ex)
                {
                    stringBuilder.AppendLine(ex.Message);
                }

                wordProcessingDocument.Close();

            return stringBuilder.ToString();
        }