public SlideSource(PmlDocument source, int start, int count, bool keepMaster)
 {
     PmlDocument = source;
     Start = start;
     Count = count;
     KeepMaster = keepMaster;
 }
 public SlideSource(PmlDocument source, int start, bool keepMaster)
 {
     PmlDocument = source;
     Start = start;
     Count = Int32.MaxValue;
     KeepMaster = keepMaster;
 }
 public SlideSource(string fileName, int start, bool keepMaster)
 {
     PmlDocument = new PmlDocument(fileName);
     Start = start;
     Count = Int32.MaxValue;
     KeepMaster = keepMaster;
 }
        public static IEnumerable <ValidationErrorInfo> GetOpenXmlValidationErrors(string fileName,
                                                                                   string officeVersion)
        {
#if !NET35
            FileFormatVersions fileFormatVersion = FileFormatVersions.Office2013;
#else
            FileFormatVersions fileFormatVersion = FileFormatVersions.Office2010;
#endif
            try
            {
                fileFormatVersion = (FileFormatVersions)Enum.Parse(fileFormatVersion.GetType(), officeVersion);
            }
            catch (Exception)
            {
#if !NET35
                fileFormatVersion = FileFormatVersions.Office2013;
#else
                fileFormatVersion = FileFormatVersions.Office2010;
#endif
            }

            FileInfo fi = new FileInfo(fileName);
            if (Util.IsWordprocessingML(fi.Extension))
            {
                WmlDocument wml = new WmlDocument(fileName);
                using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(wml))
                    using (WordprocessingDocument wDoc = streamDoc.GetWordprocessingDocument())
                    {
                        OpenXmlValidator validator = new OpenXmlValidator(fileFormatVersion);
                        var errors = validator.Validate(wDoc);
                        return(errors.ToList());
                    }
            }
            else if (Util.IsSpreadsheetML(fi.Extension))
            {
                SmlDocument Sml = new SmlDocument(fileName);
                using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(Sml))
                    using (SpreadsheetDocument wDoc = streamDoc.GetSpreadsheetDocument())
                    {
                        OpenXmlValidator validator = new OpenXmlValidator(fileFormatVersion);
                        var errors = validator.Validate(wDoc);
                        return(errors.ToList());
                    }
            }
            else if (Util.IsPresentationML(fi.Extension))
            {
                PmlDocument Pml = new PmlDocument(fileName);
                using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(Pml))
                    using (PresentationDocument wDoc = streamDoc.GetPresentationDocument())
                    {
                        OpenXmlValidator validator = new OpenXmlValidator(fileFormatVersion);
                        var errors = validator.Validate(wDoc);
                        return(errors.ToList());
                    }
            }
            return(Enumerable.Empty <ValidationErrorInfo>());
        }
 public static PmlDocument SearchAndReplace(PmlDocument doc, string search, string replace, bool matchCase)
 {
     using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
     {
         using (PresentationDocument document = streamDoc.GetPresentationDocument())
         {
             SearchAndReplace(document, search, replace, matchCase);
         }
         return streamDoc.GetModifiedPmlDocument();
     }
 }
Ejemplo n.º 6
0
 public static PmlDocument SearchAndReplace(PmlDocument doc, string search, string replace, bool matchCase)
 {
     using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
     {
         using (PresentationDocument document = streamDoc.GetPresentationDocument())
         {
             SearchAndReplace(document, search, replace, matchCase);
         }
         return(streamDoc.GetModifiedPmlDocument());
     }
 }
Ejemplo n.º 7
0
        public static IEnumerable <ValidationErrorInfo> GetOpenXmlValidationErrors(string fileName,
                                                                                   string officeVersion)
        {
            FileFormatVersions fileFormatVersion;

            if (!Enum.TryParse(officeVersion, out fileFormatVersion))
            {
                fileFormatVersion = FileFormatVersions.Office2013;
            }

            FileInfo fi = new FileInfo(fileName);

            if (Util.IsWordprocessingML(fi.Extension))
            {
                WmlDocument wml = new WmlDocument(fileName);
                using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(wml))
                    using (WordprocessingDocument wDoc = streamDoc.GetWordprocessingDocument())
                    {
                        OpenXmlValidator validator = new OpenXmlValidator(fileFormatVersion);
                        var errors = validator.Validate(wDoc);
                        return(errors.ToList());
                    }
            }
            else if (Util.IsSpreadsheetML(fi.Extension))
            {
                SmlDocument Sml = new SmlDocument(fileName);
                using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(Sml))
                    using (SpreadsheetDocument wDoc = streamDoc.GetSpreadsheetDocument())
                    {
                        OpenXmlValidator validator = new OpenXmlValidator(fileFormatVersion);
                        var errors = validator.Validate(wDoc);
                        return(errors.ToList());
                    }
            }
            else if (Util.IsPresentationML(fi.Extension))
            {
                PmlDocument Pml = new PmlDocument(fileName);
                using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(Pml))
                    using (PresentationDocument wDoc = streamDoc.GetPresentationDocument())
                    {
                        OpenXmlValidator validator = new OpenXmlValidator(fileFormatVersion);
                        var errors = validator.Validate(wDoc);
                        return(errors.ToList());
                    }
            }
            return(Enumerable.Empty <ValidationErrorInfo>());
        }
        static void Main(string[] args)
        {
            string source1 = "../../Contoso.pptx";
            string source2 = "../../Companies.pptx";
            string source3 = "../../Customer Content.pptx";
            string source4 = "../../Presentation One.pptx";
            string source5 = "../../Presentation Two.pptx";
            string source6 = "../../Presentation Three.pptx";
            string contoso1 = "../../Contoso One.pptx";
            string contoso2 = "../../Contoso Two.pptx";
            string contoso3 = "../../Contoso Three.pptx";
            List<SlideSource> sources = null;

            var sourceDoc = new PmlDocument(source1);
            sources = new List<SlideSource>()
            {
                new SlideSource(sourceDoc, 0, 1, false),  // Title
                new SlideSource(sourceDoc, 1, 1, false),  // First intro (of 3)
                new SlideSource(sourceDoc, 4, 2, false),  // Sales bios
                new SlideSource(sourceDoc, 9, 3, false),  // Content slides
                new SlideSource(sourceDoc, 13, 1, false),  // Closing summary
            };
            PresentationBuilder.BuildPresentation(sources, "Out1.pptx");

            sources = new List<SlideSource>()
            {
                new SlideSource(new PmlDocument(source2), 2, 1, true),  // Choose company
                new SlideSource(new PmlDocument(source3), false),       // Content
            };
            PresentationBuilder.BuildPresentation(sources, "Out2.pptx");

            sources = new List<SlideSource>()
            {
                new SlideSource(new PmlDocument(source4), true),
                new SlideSource(new PmlDocument(source5), true),
                new SlideSource(new PmlDocument(source6), true),
            };
            PresentationBuilder.BuildPresentation(sources, "Out3.pptx");

            sources = new List<SlideSource>()
            {
                new SlideSource(new PmlDocument(contoso1), true),
                new SlideSource(new PmlDocument(contoso2), true),
                new SlideSource(new PmlDocument(contoso3), true),
            };
            PresentationBuilder.BuildPresentation(sources, "Out4.pptx");
        }
 public static XElement GetMetrics(string fileName, MetricsGetterSettings settings)
 {
     FileInfo fi = new FileInfo(fileName);
     if (!fi.Exists)
         throw new FileNotFoundException("{0} does not exist.", fi.FullName);
     if (Util.IsWordprocessingML(fi.Extension))
     {
         WmlDocument wmlDoc = new WmlDocument(fi.FullName, true);
         return GetDocxMetrics(wmlDoc, settings);
     }
     if (Util.IsSpreadsheetML(fi.Extension))
     {
         SmlDocument smlDoc = new SmlDocument(fi.FullName, true);
         return GetXlsxMetrics(smlDoc, settings);
     }
     if (Util.IsPresentationML(fi.Extension))
     {
         PmlDocument pmlDoc = new PmlDocument(fi.FullName, true);
         return GetPptxMetrics(pmlDoc, settings);
     }
     return null;
 }
 public SlideSource(string fileName, int start, int count, bool keepMaster)
 {
     PmlDocument = new PmlDocument(fileName);
     Start = start;
     Count = count;
     KeepMaster = keepMaster;
 }
        static void Main(string[] args)
        {
            string presentation = "../../Presentation1.pptx";
            string hiddenPresentation = "../../HiddenPresentation.pptx";

            // First, load both presentations into byte arrays, simulating retrieving presentations from some source
            // such as a SharePoint server
            var baPresentation = File.ReadAllBytes(presentation);
            var baHiddenPresentation = File.ReadAllBytes(hiddenPresentation);

            // Next, replace "thee" with "the" in the main presentation
            var pmlMainPresentation = new PmlDocument("Main.pptx", baPresentation);
            PmlDocument modifiedMainPresentation = null;
            using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(pmlMainPresentation))
            {
                using (PresentationDocument document = streamDoc.GetPresentationDocument())
                {
                    var pXDoc = document.PresentationPart.GetXDocument();
                    foreach (var slideId in pXDoc.Root.Elements(P.sldIdLst).Elements(P.sldId))
                    {
                        var slideRelId = (string)slideId.Attribute(R.id);
                        var slidePart = document.PresentationPart.GetPartById(slideRelId);
                        var slideXDoc = slidePart.GetXDocument();
                        var paragraphs = slideXDoc.Descendants(A.p).ToList();
                        OpenXmlRegex.Replace(paragraphs, new Regex("thee"), "the", null);
                        slidePart.PutXDocument();
                    }
                }
                modifiedMainPresentation = streamDoc.GetModifiedPmlDocument();
            }

            // Combine the two presentations into a single presentation
            var slideSources = new List<SlideSource>() {
                new SlideSource(modifiedMainPresentation, 0, 1, true),
                new SlideSource(new PmlDocument("Hidden.pptx", baHiddenPresentation), true),
                new SlideSource(modifiedMainPresentation, 1, true),
            };
            PmlDocument combinedPresentation = PresentationBuilder.BuildPresentation(slideSources);

            // Replace <# TRADEMARK #> with AdventureWorks (c)
            PmlDocument modifiedCombinedPresentation = null;
            using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(combinedPresentation))
            {
                using (PresentationDocument document = streamDoc.GetPresentationDocument())
                {
                    var pXDoc = document.PresentationPart.GetXDocument();
                    foreach (var slideId in pXDoc.Root.Elements(P.sldIdLst).Elements(P.sldId).Skip(1).Take(1))
                    {
                        var slideRelId = (string)slideId.Attribute(R.id);
                        var slidePart = document.PresentationPart.GetPartById(slideRelId);
                        var slideXDoc = slidePart.GetXDocument();
                        var paragraphs = slideXDoc.Descendants(A.p).ToList();
                        OpenXmlRegex.Replace(paragraphs, new Regex("<# TRADEMARK #>"), "AdventureWorks (c)", null);
                        slidePart.PutXDocument();
                    }
                }
                modifiedCombinedPresentation = streamDoc.GetModifiedPmlDocument();
            }

            // we now have a PmlDocument (which is essentially a byte array) that can be saved as necessary.
            modifiedCombinedPresentation.SaveAs("Modified.pptx");
        }
        protected void btnOk_Click(object sender, EventArgs e)
        {
            //Get the url parameters for SPListId and SPListItemId
            var listID = new Guid(Request["SPListId"]);

            String[] listItemIDs;
            if (Request["SPListItemId"] != null && Request["SPListItemId"] != "null")
            {
                listItemIDs = Request["SPListItemId"].Split(',');
            } else
            {
                listItemIDs = allIdsHidden.Value.Split(',');
            }

            // The following code gets the client context and Title property by using TokenHelper.
            // To access other properties, the app may need to request permissions on the host web.
            var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                var list = clientContext.Web.Lists.GetById(listID);
                clientContext.Load(list, l => l.RootFolder, l => l.RootFolder.ServerRelativeUrl);
                clientContext.ExecuteQuery();

                //setup sources for the combined file
                List<OrderedSlideSource> sources = new List<OrderedSlideSource>();

                //check if row is selected
                foreach (GridViewRow rowItem in gridViewSelectedFiles.Rows)
                {
                    CheckBox chk = (CheckBox)rowItem.Cells[0].FindControl("chkItem");
                    CheckBox chkFormat = (CheckBox)rowItem.Cells[0].FindControl("chkFormat");
                    DropDownList cbo = (DropDownList)rowItem.Cells[2].FindControl("cboOrder");
                    if (chk.Checked)
                    {
                        //read the binary and prepare to combine
                        var file = list.GetItemById(Convert.ToInt32(listItemIDs[rowItem.RowIndex])).File;
                        clientContext.Load(file);
                        clientContext.ExecuteQuery();

                        var stream = file.OpenBinaryStream();
                        clientContext.ExecuteQuery();

                        using (MemoryStream ms = new MemoryStream())
                        {
                            stream.Value.CopyTo(ms);
                            var doc = new PmlDocument(file.Name, ms.ToArray());
                            sources.Add(new OrderedSlideSource(doc, chkFormat.Checked, Convert.ToInt32(cbo.SelectedValue)));
                        }
                    }
                }

                //combine docs
                if (sources.Count > 1)
                {
                    //reorder
                    var s = new List<SlideSource>();
                    foreach (var ss in sources.OrderBy(i => i.Order))
                        s.Add(ss);

                    var combined = PresentationBuilder.BuildPresentation(s, 1);

                    using (var combinedStream = new MemoryStream(combined.DocumentByteArray))
                    {
                        string fileName = ((txtFileName.Text.EndsWith(".pptx", StringComparison.CurrentCultureIgnoreCase)) ? txtFileName.Text : txtFileName.Text + ".pptx");
                        list.RootFolder.UploadFile(fileName, combinedStream, true);
                    }
                }
            }

            //add script to page to close dialog and refresh page
            ScriptManager.RegisterClientScriptBlock(this, typeof(Combine), "closeDialog", "closeParentDialog(true);", true);
        }
        public static XElement GetPptxMetrics(PmlDocument pmlDoc, MetricsGetterSettings settings)
        {
            using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(pmlDoc))
            {
                using (PresentationDocument pDoc = streamDoc.GetPresentationDocument())
                {
                    List<XElement> metrics = new List<XElement>();

                    bool valid = ValidateAgainstSpecificVersion(pDoc, metrics, DocumentFormat.OpenXml.FileFormatVersions.Office2007, H.SdkValidationError2007);
                    valid |= ValidateAgainstSpecificVersion(pDoc, metrics, DocumentFormat.OpenXml.FileFormatVersions.Office2010, H.SdkValidationError2010);
#if !NET35
                    valid |= ValidateAgainstSpecificVersion(pDoc, metrics, DocumentFormat.OpenXml.FileFormatVersions.Office2013, H.SdkValidationError2013);
#endif
                    return new XElement(H.Metrics,
                        new XAttribute(H.FileName, pmlDoc.FileName),
                        new XAttribute(H.FileType, "PresentationML"),
                        metrics);
                }
            }
        }
        public void CU001_ChartUpdater(string chartFileName)
        {
            FileInfo templateFile = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, chartFileName));

            if (templateFile.Extension.ToLower() == ".docx")
            {
                WmlDocument wmlTemplate = new WmlDocument(templateFile.FullName);

                var afterUpdatingDocx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, templateFile.Name.Replace(".docx", "-processed-by-ChartUpdater.docx")));
                wmlTemplate.SaveAs(afterUpdatingDocx.FullName);

                using (var wDoc = WordprocessingDocument.Open(afterUpdatingDocx.FullName, true))
                {
                    var chart1Data = new ChartData
                    {
                        SeriesNames = new[] {
                            "Car",
                            "Truck",
                            "Van",
                            "Bike",
                            "Boat",
                        },
                        CategoryDataType = ChartDataType.String,
                        CategoryNames = new[] {
                            "Q1",
                            "Q2",
                            "Q3",
                            "Q4",
                        },
                        Values = new double[][] {
                            new double[] {
                                100, 310, 220, 450,
                            },
                            new double[] {
                                200, 300, 350, 411,
                            },
                            new double[] {
                                80, 120, 140, 600,
                            },
                            new double[] {
                                120, 100, 140, 400,
                            },
                            new double[] {
                                200, 210, 210, 480,
                            },
                        },
                    };
                    ChartUpdater.UpdateChart(wDoc, "Chart1", chart1Data);

                    var chart2Data = new ChartData
                    {
                        SeriesNames = new[] {
                            "Series"
                        },
                        CategoryDataType = ChartDataType.String,
                        CategoryNames = new[] {
                                "Cars",
                                "Trucks",
                                "Vans",
                                "Boats",
                            },
                        Values = new double[][] {
                            new double[] {
                                320, 112, 64, 80,
                            },
                        },
                    };
                    ChartUpdater.UpdateChart(wDoc, "Chart2", chart2Data);

                    var chart3Data = new ChartData
                    {
                        SeriesNames = new[] {
                            "X1",
                            "X2",
                            "X3",
                            "X4",
                            "X5",
                            "X6",
                        },
                        CategoryDataType = ChartDataType.String,
                        CategoryNames = new[] {
                            "Y1",
                            "Y2",
                            "Y3",
                            "Y4",
                            "Y5",
                            "Y6",
                        },
                        Values = new double[][] {
                            new double[] {      3.0,      2.1,       .7,      .7,      2.1,      3.0,      },
                            new double[] {      3.0,      2.1,       .8,      .8,      2.1,      3.0,      },
                            new double[] {      3.0,      2.4,      1.2,     1.2,      2.4,      3.0,      },
                            new double[] {      3.0,      2.7,      1.7,     1.7,      2.7,      3.0,      },
                            new double[] {      3.0,      2.9,      2.5,     2.5,      2.9,      3.0,      },
                            new double[] {      3.0,      3.0,      3.0,     3.0,      3.0,      3.0,      },
                        },
                    };
                    ChartUpdater.UpdateChart(wDoc, "Chart3", chart3Data);

                    var chart4Data = new ChartData
                    {
                        SeriesNames = new[] {
                            "Car",
                            "Truck",
                            "Van",
                        },
                        CategoryDataType = ChartDataType.DateTime,
                        CategoryFormatCode = 14,
                        CategoryNames = new[] {
                            ToExcelInteger(new DateTime(2013, 9, 1)),
                            ToExcelInteger(new DateTime(2013, 9, 2)),
                            ToExcelInteger(new DateTime(2013, 9, 3)),
                            ToExcelInteger(new DateTime(2013, 9, 4)),
                            ToExcelInteger(new DateTime(2013, 9, 5)),
                            ToExcelInteger(new DateTime(2013, 9, 6)),
                            ToExcelInteger(new DateTime(2013, 9, 7)),
                            ToExcelInteger(new DateTime(2013, 9, 8)),
                            ToExcelInteger(new DateTime(2013, 9, 9)),
                            ToExcelInteger(new DateTime(2013, 9, 10)),
                            ToExcelInteger(new DateTime(2013, 9, 11)),
                            ToExcelInteger(new DateTime(2013, 9, 12)),
                            ToExcelInteger(new DateTime(2013, 9, 13)),
                            ToExcelInteger(new DateTime(2013, 9, 14)),
                            ToExcelInteger(new DateTime(2013, 9, 15)),
                            ToExcelInteger(new DateTime(2013, 9, 16)),
                            ToExcelInteger(new DateTime(2013, 9, 17)),
                            ToExcelInteger(new DateTime(2013, 9, 18)),
                            ToExcelInteger(new DateTime(2013, 9, 19)),
                            ToExcelInteger(new DateTime(2013, 9, 20)),
                        },
                        Values = new double[][] {
                            new double[] {
                                1, 2, 3, 2, 3, 4, 5, 4, 5, 6, 5, 4, 5, 6, 7, 8, 7, 8, 8, 9,
                            },
                            new double[] {
                                2, 3, 3, 4, 4, 5, 6, 7, 8, 7, 8, 9, 9, 9, 7, 8, 9, 9, 10, 11,
                            },
                            new double[] {
                                2, 3, 3, 3, 3, 2, 2, 2, 3, 2, 3, 3, 4, 4, 4, 3, 4, 5, 5, 4,
                            },
                        },
                    };
                    ChartUpdater.UpdateChart(wDoc, "Chart4", chart4Data);
                }
            }
            if (templateFile.Extension.ToLower() == ".pptx")
            {
                PmlDocument pmlTemplate = new PmlDocument(templateFile.FullName);

                var afterUpdatingPptx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, templateFile.Name.Replace(".pptx", "-processed-by-ChartUpdater.pptx")));
                pmlTemplate.SaveAs(afterUpdatingPptx.FullName);

                using (var pDoc = PresentationDocument.Open(afterUpdatingPptx.FullName, true))
                {
                    var chart1Data = new ChartData
                    {
                        SeriesNames = new[] {
                            "Car",
                            "Truck",
                            "Van",
                        },
                        CategoryDataType = ChartDataType.String,
                        CategoryNames = new[] {
                            "Q1",
                            "Q2",
                            "Q3",
                            "Q4",
                        },
                        Values = new double[][] {
                            new double[] {
                                320, 310, 320, 330,
                            },
                            new double[] {
                                201, 224, 230, 221,
                            },
                            new double[] {
                                180, 200, 220, 230,
                            },
                        },
                    };
                    ChartUpdater.UpdateChart(pDoc, 1, chart1Data);
                }
            }
        }
 public OrderedSlideSource(PmlDocument doc, bool keepMaster, int order) : base(doc, keepMaster)
 {
     this.Order = order;
 }
        public static IEnumerable<ValidationErrorInfo> GetOpenXmlValidationErrors(string fileName,
            string officeVersion)
        {
#if !NET35
            FileFormatVersions fileFormatVersion = FileFormatVersions.Office2013;
#else
            FileFormatVersions fileFormatVersion = FileFormatVersions.Office2010;
#endif
            try
            {
                fileFormatVersion = (FileFormatVersions)Enum.Parse(fileFormatVersion.GetType(), officeVersion);
            }
            catch (Exception)
            {
#if !NET35
                fileFormatVersion = FileFormatVersions.Office2013;
#else
                fileFormatVersion = FileFormatVersions.Office2010;
#endif
            }

            FileInfo fi = new FileInfo(fileName);
            if (Util.IsWordprocessingML(fi.Extension))
            {
                WmlDocument wml = new WmlDocument(fileName);
                using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(wml))
                using (WordprocessingDocument wDoc = streamDoc.GetWordprocessingDocument())
                {
                    OpenXmlValidator validator = new OpenXmlValidator(fileFormatVersion);
                    var errors = validator.Validate(wDoc);
                    return errors.ToList();
                }
            }
            else if (Util.IsSpreadsheetML(fi.Extension))
            {
                SmlDocument Sml = new SmlDocument(fileName);
                using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(Sml))
                using (SpreadsheetDocument wDoc = streamDoc.GetSpreadsheetDocument())
                {
                    OpenXmlValidator validator = new OpenXmlValidator(fileFormatVersion);
                    var errors = validator.Validate(wDoc);
                    return errors.ToList();
                }
            }
            else if (Util.IsPresentationML(fi.Extension))
            {
                PmlDocument Pml = new PmlDocument(fileName);
                using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(Pml))
                using (PresentationDocument wDoc = streamDoc.GetPresentationDocument())
                {
                    OpenXmlValidator validator = new OpenXmlValidator(fileFormatVersion);
                    var errors = validator.Validate(wDoc);
                    return errors.ToList();
                }
            }
            return Enumerable.Empty<ValidationErrorInfo>();
        }
Ejemplo n.º 17
0
        public static IEnumerable<ValidationErrorInfo> GetOpenXmlValidationErrors(string fileName,
            string officeVersion)
        {
            FileFormatVersions fileFormatVersion;
            if (!Enum.TryParse(officeVersion, out fileFormatVersion))
                fileFormatVersion = FileFormatVersions.Office2013;

            FileInfo fi = new FileInfo(fileName);
            if (Util.IsWordprocessingML(fi.Extension))
            {
                WmlDocument wml = new WmlDocument(fileName);
                using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(wml))
                using (WordprocessingDocument wDoc = streamDoc.GetWordprocessingDocument())
                {
                    OpenXmlValidator validator = new OpenXmlValidator(fileFormatVersion);
                    var errors = validator.Validate(wDoc);
                    return errors.ToList();
                }
            }
            else if (Util.IsSpreadsheetML(fi.Extension))
            {
                SmlDocument Sml = new SmlDocument(fileName);
                using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(Sml))
                using (SpreadsheetDocument wDoc = streamDoc.GetSpreadsheetDocument())
                {
                    OpenXmlValidator validator = new OpenXmlValidator(fileFormatVersion);
                    var errors = validator.Validate(wDoc);
                    return errors.ToList();
                }
            }
            else if (Util.IsPresentationML(fi.Extension))
            {
                PmlDocument Pml = new PmlDocument(fileName);
                using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(Pml))
                using (PresentationDocument wDoc = streamDoc.GetPresentationDocument())
                {
                    OpenXmlValidator validator = new OpenXmlValidator(fileFormatVersion);
                    var errors = validator.Validate(wDoc);
                    return errors.ToList();
                }
            }
            return Enumerable.Empty<ValidationErrorInfo>();
        }