public void CanCreateFlatOpcSpreadsheetDocuments1()
        {
            using (var inputStream = GetStream(TestFiles.Spreadsheet))
                using (var source = SpreadsheetDocument.Open(inputStream, false))
                {
                    var flatOpcDoc = source.ToFlatOpcDocument();

                    using (var dest = SpreadsheetDocument.FromFlatOpcDocument(flatOpcDoc))
                    {
                        PackageAssert.Equal(source, dest);
                    }
                }
        }
        public void CanCreateFlatOpcSpreadsheetDocuments()
        {
            using (var source = SpreadsheetDocument.Open(Path.Combine(TestUtil.TestResultsDirectory, SpreadsheetPath), true))
            {
                // Test FlatOpcDocument methods.
                // Check ToFlatOpcDocument() and FromFlatOpcDocument(XDocument).
                var flatOpcDoc = source.ToFlatOpcDocument();
                using (var dest = SpreadsheetDocument.FromFlatOpcDocument(flatOpcDoc))
                    AssertThatPackagesAreEqual(source, dest);

                // Check FromFlatOpcDocument(XDocument, Stream, bool).
                using (Stream stream = new MemoryStream())
                    using (SpreadsheetDocument.FromFlatOpcDocument(flatOpcDoc, stream, false))
                        using (var dest = SpreadsheetDocument.Open(stream, false))
                            AssertThatPackagesAreEqual(source, dest);

                // Check FromFlatOpcDocument(XDocument, string, bool).
                using (SpreadsheetDocument.FromFlatOpcDocument(flatOpcDoc, Path.Combine(TestUtil.TestResultsDirectory, SpreadsheetClonePath), false))
                    using (var dest = SpreadsheetDocument.Open(Path.Combine(TestUtil.TestResultsDirectory, SpreadsheetClonePath), false))
                        AssertThatPackagesAreEqual(source, dest);

                // Check FromFlatOpcDocument(XDocument, Package).
                using (var stream = new MemoryStream())
                    using (var package = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite))
                        using (var dest = SpreadsheetDocument.FromFlatOpcDocument(flatOpcDoc, package))
                            AssertThatPackagesAreEqual(source, dest);

                // Test FlatOpcString methods.
                // Check ToFlatOpcString() and FromFlatOpcString(string).
                var flatOpcString = source.ToFlatOpcString();
                using (var dest = SpreadsheetDocument.FromFlatOpcString(flatOpcString))
                    AssertThatPackagesAreEqual(source, dest);

                // Check FromFlatOpcString(string, Stream, bool).
                using (Stream stream = new MemoryStream())
                    using (SpreadsheetDocument.FromFlatOpcString(flatOpcString, stream, false))
                        using (var dest = SpreadsheetDocument.Open(stream, false))
                            AssertThatPackagesAreEqual(source, dest);

                // Check FromFlatOpcString(string, string, bool).
                using (SpreadsheetDocument.FromFlatOpcString(flatOpcString, Path.Combine(TestUtil.TestResultsDirectory, SpreadsheetClonePath), false))
                    using (var dest = SpreadsheetDocument.Open(Path.Combine(TestUtil.TestResultsDirectory, SpreadsheetClonePath), false))
                        AssertThatPackagesAreEqual(source, dest);

                // Check FromFlatOpcString(string, Package).
                using (var stream = new MemoryStream())
                    using (var package = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite))
                        using (var dest = SpreadsheetDocument.FromFlatOpcString(flatOpcString, package))
                            AssertThatPackagesAreEqual(source, dest);
            }
        }
        public void CanCreateFlatOpcSpreadsheetDocuments4()
        {
            using (var inputStream = GetStream(TestFiles.Spreadsheet))
                using (var source = SpreadsheetDocument.Open(inputStream, false))
                    using (var stream = new MemoryStream())
                        using (var package = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite))
                        {
                            var flatOpcDoc = source.ToFlatOpcDocument();

                            using (var dest = SpreadsheetDocument.FromFlatOpcDocument(flatOpcDoc, package))
                            {
                                PackageAssert.Equal(source, dest);
                            }
                        }
        }
        public void CanCreateFlatOpcSpreadsheetDocuments3()
        {
            using (var inputStream = GetStream(TestFiles.Spreadsheet))
                using (var source = SpreadsheetDocument.Open(inputStream, false))
                {
                    var flatOpcDoc = source.ToFlatOpcDocument();

                    using (var tempFile = TemporaryFile.Create())
                        using (SpreadsheetDocument.FromFlatOpcDocument(flatOpcDoc, tempFile.Path, false))
                            using (var dest = SpreadsheetDocument.Open(tempFile.Path, false))
                            {
                                PackageAssert.Equal(source, dest);
                            }
                }
        }
        /// <summary>
        /// Creates a new instance of DocumentType from a Flat OPC <see cref="XDocument" />.
        /// </summary>
        /// <param name="document">The Flat OPC <see cref="XDocument" />.</param>
        /// <typeparam name="TDocument">A subclass of <see cref="OpenXmlPackage" />.</typeparam>
        /// <returns>A new instance of DocumentType</returns>
        public static TDocument FromFlatOpcDocument <TDocument>(XDocument document)
            where TDocument : OpenXmlPackage
        {
            var t = typeof(TDocument);

            if (t == typeof(WordprocessingDocument))
            {
                return(WordprocessingDocument.FromFlatOpcDocument(document) as TDocument);
            }
            if (t == typeof(SpreadsheetDocument))
            {
                return(SpreadsheetDocument.FromFlatOpcDocument(document) as TDocument);
            }
            if (t == typeof(PresentationDocument))
            {
                return(PresentationDocument.FromFlatOpcDocument(document) as TDocument);
            }
            throw new OpenXmlTransformationException("Unsupported document type: " + t);
        }