Some other small fixes.master
| @@ -1,6 +1,8 @@ | |||
| | |||
| Microsoft Visual Studio Solution File, Format Version 12.00 | |||
| # Visual Studio 2012 | |||
| # Visual Studio 2013 | |||
| VisualStudioVersion = 12.0.30110.0 | |||
| MinimumVisualStudioVersion = 10.0.40219.1 | |||
| Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{DF41F5CE-8BCB-40CC-835F-54A3DB7D802F}" | |||
| ProjectSection(SolutionItems) = preProject | |||
| DocX.vsmdi = DocX.vsmdi | |||
| @@ -35,9 +37,6 @@ Global | |||
| SccProjectName4 = Documentation | |||
| SccLocalPath4 = Documentation | |||
| EndGlobalSection | |||
| GlobalSection(TestCaseManagementSettings) = postSolution | |||
| CategoryFile = DocX.vsmdi | |||
| EndGlobalSection | |||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | |||
| Debug|Any CPU = Debug|Any CPU | |||
| Debug|Mixed Platforms = Debug|Mixed Platforms | |||
| @@ -72,9 +71,10 @@ Global | |||
| {F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Debug|Mixed Platforms.Build.0 = Debug|x86 | |||
| {F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Debug|x86.ActiveCfg = Debug|x86 | |||
| {F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Debug|x86.Build.0 = Debug|x86 | |||
| {F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Release|Any CPU.ActiveCfg = Release|x86 | |||
| {F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Release|Mixed Platforms.ActiveCfg = Release|x86 | |||
| {F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Release|Mixed Platforms.Build.0 = Release|x86 | |||
| {F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||
| {F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Release|Any CPU.Build.0 = Release|Any CPU | |||
| {F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU | |||
| {F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Release|Mixed Platforms.Build.0 = Release|Any CPU | |||
| {F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Release|x86.ActiveCfg = Release|x86 | |||
| {F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Release|x86.Build.0 = Release|x86 | |||
| {1EB1EE8F-9978-425B-A742-533DCCEB4811}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |||
| @@ -90,4 +90,7 @@ Global | |||
| GlobalSection(SolutionProperties) = preSolution | |||
| HideSolutionNode = FALSE | |||
| EndGlobalSection | |||
| GlobalSection(TestCaseManagementSettings) = postSolution | |||
| CategoryFile = DocX.vsmdi | |||
| EndGlobalSection | |||
| EndGlobal | |||
| @@ -2050,7 +2050,7 @@ namespace Novacode | |||
| /// <seealso cref="DocX.Load(System.IO.Stream)"/> | |||
| /// <seealso cref="DocX.Load(string)"/> | |||
| /// <seealso cref="DocX.Save()"/> | |||
| public static DocX Create(Stream stream) | |||
| public static DocX Create(Stream stream, DocumentTypes documentType = DocumentTypes.Document) | |||
| { | |||
| // Store this document in memory | |||
| MemoryStream ms = new MemoryStream(); | |||
| @@ -2058,7 +2058,7 @@ namespace Novacode | |||
| // Create the docx package | |||
| Package package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite); | |||
| PostCreation(ref package); | |||
| PostCreation(package, documentType); | |||
| DocX document = DocX.Load(ms); | |||
| document.stream = stream; | |||
| return document; | |||
| @@ -2095,7 +2095,7 @@ namespace Novacode | |||
| /// <seealso cref="DocX.Load(string)"/> | |||
| /// <seealso cref="DocX.Save()"/> | |||
| /// </example> | |||
| public static DocX Create(string filename) | |||
| public static DocX Create(string filename, DocumentTypes documentType = DocumentTypes.Document) | |||
| { | |||
| // Store this document in memory | |||
| MemoryStream ms = new MemoryStream(); | |||
| @@ -2104,19 +2104,27 @@ namespace Novacode | |||
| //WordprocessingDocument wdDoc = WordprocessingDocument.Create(ms, DocumentFormat.OpenXml.WordprocessingDocumentType.Document); | |||
| Package package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite); | |||
| PostCreation(ref package); | |||
| PostCreation(package, documentType); | |||
| DocX document = DocX.Load(ms); | |||
| document.filename = filename; | |||
| return document; | |||
| } | |||
| internal static void PostCreation(ref Package package) | |||
| internal static void PostCreation(Package package, DocumentTypes documentType = DocumentTypes.Document) | |||
| { | |||
| XDocument mainDoc, stylesDoc, numberingDoc; | |||
| #region MainDocumentPart | |||
| // Create the main document part for this package | |||
| PackagePart mainDocumentPart = package.CreatePart(new Uri("/word/document.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"); | |||
| PackagePart mainDocumentPart; | |||
| if (documentType == DocumentTypes.Document) | |||
| { | |||
| mainDocumentPart = package.CreatePart(new Uri("/word/document.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"); | |||
| } | |||
| else | |||
| { | |||
| mainDocumentPart = package.CreatePart(new Uri("/word/document.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml"); | |||
| } | |||
| package.CreateRelationship(mainDocumentPart.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"); | |||
| // Load the document part into a XDocument object | |||
| @@ -2162,11 +2170,8 @@ namespace Novacode | |||
| #region MainDocumentPart | |||
| document.mainPart = package.GetParts().Where | |||
| ( | |||
| p => p.ContentType.Equals | |||
| ( | |||
| "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", | |||
| StringComparison.CurrentCultureIgnoreCase | |||
| ) | |||
| p => p.ContentType.Equals(HelperFunctions.DOCUMENT_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase) || | |||
| p.ContentType.Equals(HelperFunctions.TEMPLATE_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase) | |||
| ).Single(); | |||
| using (TextReader tr = new StreamReader(document.mainPart.GetStream(FileMode.Open, FileAccess.Read))) | |||
| @@ -96,6 +96,7 @@ | |||
| <Compile Include="Charts\PieChart.cs" /> | |||
| <Compile Include="Charts\XElementHelpers.cs" /> | |||
| <Compile Include="Container.cs" /> | |||
| <Compile Include="DocumentTypes.cs" /> | |||
| <Compile Include="Footers.cs" /> | |||
| <Compile Include="Footer.cs" /> | |||
| <Compile Include="CustomProperty.cs" /> | |||
| @@ -0,0 +1,12 @@ | |||
| using System; | |||
| using System.Linq; | |||
| using System.Collections.Generic; | |||
| namespace Novacode | |||
| { | |||
| public enum DocumentTypes | |||
| { | |||
| Document, | |||
| Template | |||
| } | |||
| } | |||
| @@ -15,7 +15,8 @@ namespace Novacode | |||
| { | |||
| internal static class HelperFunctions | |||
| { | |||
| public const string DOCUMENT_DOCUMENTTYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"; | |||
| public const string TEMPLATE_DOCUMENTTYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml"; | |||
| /// <summary> | |||
| /// Checks whether 'toCheck' has all children that 'desired' has and values of 'val' attributes are the same | |||
| /// </summary> | |||
| @@ -215,14 +216,8 @@ namespace Novacode | |||
| { | |||
| settingsPart = package.CreatePart(settingsUri, "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml", CompressionOption.Maximum); | |||
| PackagePart mainDocumentPart = package.GetParts().Where | |||
| ( | |||
| p => p.ContentType.Equals | |||
| ( | |||
| "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", | |||
| StringComparison.CurrentCultureIgnoreCase | |||
| ) | |||
| ).Single(); | |||
| PackagePart mainDocumentPart = package.GetParts().Single(p => p.ContentType.Equals(DOCUMENT_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase) || | |||
| p.ContentType.Equals(TEMPLATE_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase)); | |||
| mainDocumentPart.CreateRelationship(settingsUri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings"); | |||
| @@ -344,11 +339,8 @@ namespace Novacode | |||
| using (TextWriter tw = new StreamWriter(wordNumbering.GetStream(FileMode.Create, FileAccess.Write))) | |||
| numberingDoc.Save(tw, SaveOptions.None); | |||
| PackagePart mainDocumentPart = package.GetParts().Single(p => p.ContentType.Equals | |||
| ( | |||
| "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", | |||
| StringComparison.CurrentCultureIgnoreCase | |||
| )); | |||
| PackagePart mainDocumentPart = package.GetParts().Single(p => p.ContentType.Equals(DOCUMENT_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase) || | |||
| p.ContentType.Equals(TEMPLATE_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase)); | |||
| mainDocumentPart.CreateRelationship(wordNumbering.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering"); | |||
| return numberingDoc; | |||
| @@ -378,11 +370,7 @@ namespace Novacode | |||
| PackagePart mainDocumentPart = package.GetParts().Where | |||
| ( | |||
| p => p.ContentType.Equals | |||
| ( | |||
| "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", | |||
| StringComparison.CurrentCultureIgnoreCase | |||
| ) | |||
| p => p.ContentType.Equals(DOCUMENT_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase)||p.ContentType.Equals(TEMPLATE_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase) | |||
| ).Single(); | |||
| mainDocumentPart.CreateRelationship(word_styles.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"); | |||
| @@ -31,7 +31,7 @@ | |||
| <UseVSHostingProcess>true</UseVSHostingProcess> | |||
| </PropertyGroup> | |||
| <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' "> | |||
| <PlatformTarget>x86</PlatformTarget> | |||
| <PlatformTarget>AnyCPU</PlatformTarget> | |||
| <DebugType>pdbonly</DebugType> | |||
| <Optimize>true</Optimize> | |||
| <OutputPath>bin\Release\</OutputPath> | |||
| @@ -39,6 +39,14 @@ | |||
| <ErrorReport>prompt</ErrorReport> | |||
| <WarningLevel>4</WarningLevel> | |||
| </PropertyGroup> | |||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'"> | |||
| <PlatformTarget>AnyCPU</PlatformTarget> | |||
| <OutputPath>bin\Debug\</OutputPath> | |||
| </PropertyGroup> | |||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'"> | |||
| <PlatformTarget>AnyCPU</PlatformTarget> | |||
| <OutputPath>bin\Release\</OutputPath> | |||
| </PropertyGroup> | |||
| <ItemGroup> | |||
| <Reference Include="System" /> | |||
| <Reference Include="System.Core" /> | |||
| @@ -1361,7 +1361,28 @@ namespace UnitTests | |||
| } | |||
| } | |||
| } | |||
| [TestMethod] | |||
| public void When_opening_a_template_no_error_should_be_thrown() | |||
| { | |||
| using (DocX document = DocX.Load(_directoryWithFiles + "Template.dotx")) | |||
| { | |||
| Assert.IsTrue(document.Paragraphs.Count > 0); | |||
| } | |||
| } | |||
| [TestMethod] | |||
| public void Saving_and_loading_a_template_should_work() | |||
| { | |||
| using (DocX document = DocX.Create("test template.dotx", DocumentTypes.Template)) | |||
| { | |||
| document.InsertParagraph("hello, this is a paragraph"); | |||
| document.Save(); | |||
| } | |||
| using (DocX document = DocX.Load("test template.dotx")) | |||
| { | |||
| Assert.IsTrue(document.Paragraphs.Count > 0); | |||
| } | |||
| } | |||
| [TestMethod] | |||
| public void Test_ParentContainer_When_Creating_Doc() | |||
| { | |||