Selaa lähdekoodia

Added support for opening and manipulating templates. Patch provided stimms

Some other small fixes.
master
MadBoy_cp 12 vuotta sitten
vanhempi
commit
64e10c9e0b

+ 10
- 7
DocX.sln Näytä tiedosto

 
Microsoft Visual Studio Solution File, Format Version 12.00 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}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{DF41F5CE-8BCB-40CC-835F-54A3DB7D802F}"
ProjectSection(SolutionItems) = preProject ProjectSection(SolutionItems) = preProject
DocX.vsmdi = DocX.vsmdi DocX.vsmdi = DocX.vsmdi
SccProjectName4 = Documentation SccProjectName4 = Documentation
SccLocalPath4 = Documentation SccLocalPath4 = Documentation
EndGlobalSection EndGlobalSection
GlobalSection(TestCaseManagementSettings) = postSolution
CategoryFile = DocX.vsmdi
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
Debug|Mixed Platforms = Debug|Mixed Platforms Debug|Mixed Platforms = Debug|Mixed Platforms
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Debug|Mixed Platforms.Build.0 = Debug|x86 {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.ActiveCfg = Debug|x86
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Debug|x86.Build.0 = 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.ActiveCfg = Release|x86
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Release|x86.Build.0 = 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 {1EB1EE8F-9978-425B-A742-533DCCEB4811}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
EndGlobalSection EndGlobalSection
GlobalSection(TestCaseManagementSettings) = postSolution
CategoryFile = DocX.vsmdi
EndGlobalSection
EndGlobal EndGlobal

+ 16
- 11
DocX/DocX.cs Näytä tiedosto

/// <seealso cref="DocX.Load(System.IO.Stream)"/> /// <seealso cref="DocX.Load(System.IO.Stream)"/>
/// <seealso cref="DocX.Load(string)"/> /// <seealso cref="DocX.Load(string)"/>
/// <seealso cref="DocX.Save()"/> /// <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 // Store this document in memory
MemoryStream ms = new MemoryStream(); MemoryStream ms = new MemoryStream();
// Create the docx package // Create the docx package
Package package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite); Package package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
PostCreation(ref package);
PostCreation(package, documentType);
DocX document = DocX.Load(ms); DocX document = DocX.Load(ms);
document.stream = stream; document.stream = stream;
return document; return document;
/// <seealso cref="DocX.Load(string)"/> /// <seealso cref="DocX.Load(string)"/>
/// <seealso cref="DocX.Save()"/> /// <seealso cref="DocX.Save()"/>
/// </example> /// </example>
public static DocX Create(string filename)
public static DocX Create(string filename, DocumentTypes documentType = DocumentTypes.Document)
{ {
// Store this document in memory // Store this document in memory
MemoryStream ms = new MemoryStream(); MemoryStream ms = new MemoryStream();
//WordprocessingDocument wdDoc = WordprocessingDocument.Create(ms, DocumentFormat.OpenXml.WordprocessingDocumentType.Document); //WordprocessingDocument wdDoc = WordprocessingDocument.Create(ms, DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
Package package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite); Package package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
PostCreation(ref package);
PostCreation(package, documentType);
DocX document = DocX.Load(ms); DocX document = DocX.Load(ms);
document.filename = filename; document.filename = filename;
return document; return document;
} }
internal static void PostCreation(ref Package package)
internal static void PostCreation(Package package, DocumentTypes documentType = DocumentTypes.Document)
{ {
XDocument mainDoc, stylesDoc, numberingDoc; XDocument mainDoc, stylesDoc, numberingDoc;
#region MainDocumentPart #region MainDocumentPart
// Create the main document part for this package // 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"); package.CreateRelationship(mainDocumentPart.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument");
// Load the document part into a XDocument object // Load the document part into a XDocument object
#region MainDocumentPart #region MainDocumentPart
document.mainPart = package.GetParts().Where 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(); ).Single();
using (TextReader tr = new StreamReader(document.mainPart.GetStream(FileMode.Open, FileAccess.Read))) using (TextReader tr = new StreamReader(document.mainPart.GetStream(FileMode.Open, FileAccess.Read)))

+ 1
- 0
DocX/DocX.csproj Näytä tiedosto

<Compile Include="Charts\PieChart.cs" /> <Compile Include="Charts\PieChart.cs" />
<Compile Include="Charts\XElementHelpers.cs" /> <Compile Include="Charts\XElementHelpers.cs" />
<Compile Include="Container.cs" /> <Compile Include="Container.cs" />
<Compile Include="DocumentTypes.cs" />
<Compile Include="Footers.cs" /> <Compile Include="Footers.cs" />
<Compile Include="Footer.cs" /> <Compile Include="Footer.cs" />
<Compile Include="CustomProperty.cs" /> <Compile Include="CustomProperty.cs" />

+ 12
- 0
DocX/DocumentTypes.cs Näytä tiedosto

using System;
using System.Linq;
using System.Collections.Generic;
namespace Novacode
{
public enum DocumentTypes
{
Document,
Template
}
}

+ 7
- 19
DocX/HelperFunctions.cs Näytä tiedosto

{ {
internal static class HelperFunctions 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> /// <summary>
/// Checks whether 'toCheck' has all children that 'desired' has and values of 'val' attributes are the same /// Checks whether 'toCheck' has all children that 'desired' has and values of 'val' attributes are the same
/// </summary> /// </summary>
{ {
settingsPart = package.CreatePart(settingsUri, "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml", CompressionOption.Maximum); 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"); mainDocumentPart.CreateRelationship(settingsUri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings");
using (TextWriter tw = new StreamWriter(wordNumbering.GetStream(FileMode.Create, FileAccess.Write))) using (TextWriter tw = new StreamWriter(wordNumbering.GetStream(FileMode.Create, FileAccess.Write)))
numberingDoc.Save(tw, SaveOptions.None); 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"); mainDocumentPart.CreateRelationship(wordNumbering.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering");
return numberingDoc; return numberingDoc;
PackagePart mainDocumentPart = package.GetParts().Where 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(); ).Single();
mainDocumentPart.CreateRelationship(word_styles.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"); mainDocumentPart.CreateRelationship(word_styles.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles");

BIN
Documentation/Help/Documentation.chm Näytä tiedosto


+ 9
- 1
Examples/Examples.csproj Näytä tiedosto

<UseVSHostingProcess>true</UseVSHostingProcess> <UseVSHostingProcess>true</UseVSHostingProcess>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>
<Optimize>true</Optimize> <Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath> <OutputPath>bin\Release\</OutputPath>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </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> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />

+ 22
- 1
UnitTests/DocXUnitTests.cs Näytä tiedosto

} }
} }
} }
[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] [TestMethod]
public void Test_ParentContainer_When_Creating_Doc() public void Test_ParentContainer_When_Creating_Doc()
{ {

Loading…
Peruuta
Tallenna