Skip to content

Commit

Permalink
Add CreateEquation(...) and CreateEquationInSpecifiedFont(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuyafujisaki committed Jul 1, 2017
1 parent 904c856 commit f9be208
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 55 deletions.
10 changes: 5 additions & 5 deletions OpenXmlWordHelper.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ VisualStudioVersion = 15.0.26430.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenXmlWordHelper", "OpenXmlWordHelper\OpenXmlWordHelper.csproj", "{7EEA76A5-EB47-42AF-A082-F23A086EBE03}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test1", "Test1\Test1.csproj", "{D0056F7C-47AF-43CF-B5B2-4D372FB13273}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestProject1", "UnitTestProject1\UnitTestProject1.csproj", "{380441B2-F847-4315-ADA0-40FE9BD6E902}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -17,10 +17,10 @@ Global
{7EEA76A5-EB47-42AF-A082-F23A086EBE03}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7EEA76A5-EB47-42AF-A082-F23A086EBE03}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7EEA76A5-EB47-42AF-A082-F23A086EBE03}.Release|Any CPU.Build.0 = Release|Any CPU
{D0056F7C-47AF-43CF-B5B2-4D372FB13273}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D0056F7C-47AF-43CF-B5B2-4D372FB13273}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D0056F7C-47AF-43CF-B5B2-4D372FB13273}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D0056F7C-47AF-43CF-B5B2-4D372FB13273}.Release|Any CPU.Build.0 = Release|Any CPU
{380441B2-F847-4315-ADA0-40FE9BD6E902}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{380441B2-F847-4315-ADA0-40FE9BD6E902}.Debug|Any CPU.Build.0 = Debug|Any CPU
{380441B2-F847-4315-ADA0-40FE9BD6E902}.Release|Any CPU.ActiveCfg = Release|Any CPU
{380441B2-F847-4315-ADA0-40FE9BD6E902}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
22 changes: 22 additions & 0 deletions OpenXmlWordHelper/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,35 @@
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Math = DocumentFormat.OpenXml.Math;

namespace OpenXmlWordHelper
{
[SuppressMessage("ReSharper", "PossiblyMistakenUseOfParamsMethod")]
[SuppressMessage("ReSharper", "PossibleNullReferenceException")]
public static class Api
{
public static class Equation
{
public static Paragraph CreateEquation(string equation) =>
new Paragraph(
new Math.Paragraph(
new Math.OfficeMath(
new Math.Run(
new Math.RunProperties(
new Math.Text { Text = equation })))));

public static Paragraph CreateEquationInSpecifiedFont(string equation, string fontName) =>
new Paragraph(
new Math.Paragraph(
new Math.OfficeMath(
new Math.Run(
new Math.RunProperties(new Math.NormalText()),
new RunFonts { Ascii = fontName, HighAnsi = fontName, EastAsia = fontName, ComplexScript = fontName },
new Math.Text { Text = equation }))));

}

public static Paragraph CreateParagraphWithText(string s) => new Paragraph(new Run(new Text(s)));

public static void ProtectWord(string path, string password)
Expand Down
10 changes: 0 additions & 10 deletions Test1/Io.cs

This file was deleted.

7 changes: 0 additions & 7 deletions Test1/packages.config

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("Test1")]
[assembly: AssemblyTitle("UnitTestProject1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Test1")]
[assembly: AssemblyProduct("UnitTestProject1")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("d0056f7c-47af-43cf-b5b2-4d372fb13273")]
[assembly: Guid("380441b2-f847-4315-ada0-40fe9bd6e902")]
[assembly: AssemblyVersion("1.0.0.*")]
39 changes: 24 additions & 15 deletions Test1/UnitTest1.cs → UnitTestProject1/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
using System.Diagnostics;
using System.IO;

namespace Test1
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[ClassInitialize]
public static void Initialize(TestContext context)
{
Environment.CurrentDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
}
public static void Initialize(TestContext context) => Environment.CurrentDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

static void TestRunner(Action<MainDocumentPart> f)
{
Expand Down Expand Up @@ -50,14 +47,12 @@ static void TestRunner(string path, Action<MainDocumentPart> f)
public void TestCreateWordWithParagraphPropertiesDefault() => TestRunner(Api.ClearHeaderFooter);

[TestMethod]
public void TestCreateParagraphWithText()
{
public void TestCreateParagraphWithText() =>
TestRunner(mdp =>
{
mdp.Document.Body.AppendChild(Api.CreateParagraphWithText("Hello"));
mdp.Document.Body.AppendChild(Api.CreateParagraphWithText("Word"));
});
}

[TestMethod]
public void TestCreateNumberingParagraphs()
Expand Down Expand Up @@ -86,8 +81,8 @@ public void TestCreateNumberingParagraphs()
[TestMethod]
public void TestMergeDocuments()
{
var path1 = Io.Desktopize("Source1.docx");
var path2 = Io.Desktopize("Source2.docx");
const string path1 = "Source1.docx";
const string path2 = "Source2.docx";

Api.MergeDocuments(path1, path2);

Expand All @@ -97,9 +92,9 @@ public void TestMergeDocuments()
[TestMethod]
public void TestMergeDocumentsToNewFile()
{
var path1 = Io.Desktopize("Source1.docx");
var path2 = Io.Desktopize("Source2.docx");
var path3 = Io.Desktopize("Destination.docx");
const string path1 = "Source1.docx";
const string path2 = "Source2.docx";
const string path3 = "Destination.docx";

Api.MergeDocuments(path1, path2, path3);

Expand All @@ -109,7 +104,7 @@ public void TestMergeDocumentsToNewFile()
[TestMethod]
public void TestProtectWord()
{
var path = Io.Desktopize("Sample.docx");
const string path = "Sample.docx";

Api.ProtectWord(path, "dummy");

Expand All @@ -119,9 +114,23 @@ public void TestProtectWord()
[TestMethod]
public void TestSetColumnJustification()
{
var path = Io.Desktopize("Sample.docx");
const string path = "Sample.docx";

TestRunner(path, mdp => Api.SetColumnJustification(mdp.Document.Body.GetFirstChild<Table>(), 0, JustificationValues.Center));
}

[TestMethod]
public void TestCreateEquation() =>
TestRunner(mdp =>
{
mdp.Document.Body.AppendChild(Api.Equation.CreateEquation("1 + 1 = 2"));
});

[TestMethod]
public void TestCreateEquationInSpecifiedFont() =>
TestRunner(mdp =>
{
mdp.Document.Body.AppendChild(Api.Equation.CreateEquationInSpecifiedFont("1 + 1 = 2", "Yu Gothic UI"));
});
}
}
40 changes: 25 additions & 15 deletions Test1/Test1.csproj → UnitTestProject1/UnitTestProject1.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="https://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\MSTest.TestAdapter.1.1.18\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.1.18\build\net45\MSTest.TestAdapter.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D0056F7C-47AF-43CF-B5B2-4D372FB13273}</ProjectGuid>
<ProjectGuid>{380441B2-F847-4315-ADA0-40FE9BD6E902}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Test1</RootNamespace>
<AssemblyName>Test1</AssemblyName>
<RootNamespace>UnitTestProject1</RootNamespace>
<AssemblyName>UnitTestProject1</AssemblyName>
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Expand All @@ -21,21 +22,21 @@
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>pdbonly</DebugType>
<DebugSymbols>false</DebugSymbols>
<DebugType>none</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>send</ErrorReport>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<LangVersion>default</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>
</DefineConstants>
<ErrorReport>send</ErrorReport>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand All @@ -45,28 +46,29 @@
<HintPath>..\packages\DocumentFormat.OpenXml.2.7.2\lib\net46\DocumentFormat.OpenXml.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.1.11\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
<HintPath>..\packages\MSTest.TestFramework.1.1.18\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.1.11\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
<HintPath>..\packages\MSTest.TestFramework.1.1.18\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.IO.FileSystem.Primitives, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.FileSystem.Primitives.4.0.1\lib\net46\System.IO.FileSystem.Primitives.dll</HintPath>
<Reference Include="System.IO.FileSystem.Primitives, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll</HintPath>
</Reference>
<Reference Include="System.IO.Packaging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Packaging.4.0.0\lib\net46\System.IO.Packaging.dll</HintPath>
<Reference Include="System.IO.Packaging, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Packaging.4.3.0\lib\net46\System.IO.Packaging.dll</HintPath>
</Reference>
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Io.cs" />
<Compile Include="UnitTest1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenXmlWordHelper\OpenXmlWordHelper.csproj">
Expand All @@ -76,4 +78,12 @@
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see https://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.1.18\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.1.18\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.1.18\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.1.18\build\net45\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.1.1.18\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.1.18\build\net45\MSTest.TestAdapter.targets')" />
</Project>
8 changes: 8 additions & 0 deletions UnitTestProject1/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DocumentFormat.OpenXml" version="2.7.2" targetFramework="net47" />
<package id="MSTest.TestAdapter" version="1.1.18" targetFramework="net47" />
<package id="MSTest.TestFramework" version="1.1.18" targetFramework="net47" />
<package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net47" />
<package id="System.IO.Packaging" version="4.3.0" targetFramework="net47" />
</packages>

0 comments on commit f9be208

Please sign in to comment.