Przeglądaj źródła

A weekends work

master
coffeycathal_cp 15 lat temu
rodzic
commit
d9e96520c3

+ 7
- 33
ConsoleApplication1/Program.cs Wyświetl plik

using Novacode; using Novacode;
using System.IO.Packaging; using System.IO.Packaging;
using System.Diagnostics; using System.Diagnostics;
using System.Xml.Linq;
namespace ConsoleApplication1 namespace ConsoleApplication1
{ {
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
using (DocX document = DocX.Load(@"D:\Source Control\DocX\UnitTests\documents\test.docx"))
{
Paragraph p = document.Paragraphs[0];
p.ReplaceText("foo", "bar", false);
//// Testing constants
//const string package_part_document = "/word/document.xml";
//const string package_part_header_first = "/word/header3.xml";
//const string package_part_header_odd = "/word/header2.xml";
//const string package_part_header_even = "/word/header1.xml";
//const string package_part_footer_first = "/word/footer3.xml";
//const string package_part_footer_odd = "/word/footer2.xml";
//const string package_part_footer_even = "/word/footer1.xml";
//// Load Test-01.docx
//using (DocX document = DocX.Load("../../data/Test-01.docx"))
//{
// // Get the headers from the document.
// Headers headers = document.Headers; Debug.Assert(headers != null);
// Header header_first = headers.first; Debug.Assert(header_first != null);
// Header header_odd = headers.odd; Debug.Assert(header_odd != null);
// Header header_even = headers.even; Debug.Assert(header_even != null);
// // Get the footers from the document.
// Footers footers = document.Footers; Debug.Assert(footers != null);
// Footer footer_first = footers.first; Debug.Assert(footer_first != null);
// Footer footer_odd = footers.odd; Debug.Assert(footer_odd != null);
// Footer footer_even = footers.even; Debug.Assert(footer_even != null);
// // Its important that each Paragraph knows the PackagePart it belongs to.
// document.Paragraphs.ForEach(p => Debug.Assert(p.PackagePart.Uri.ToString() == package_part_document));
// header_first.Paragraphs.ForEach(p => Debug.Assert(p.PackagePart.Uri.ToString() == package_part_header_first));
// header_odd.Paragraphs.ForEach(p => Debug.Assert(p.PackagePart.Uri.ToString() == package_part_header_odd));
// header_even.Paragraphs.ForEach(p => Debug.Assert(p.PackagePart.Uri.ToString() == package_part_header_even));
// footer_first.Paragraphs.ForEach(p => Debug.Assert(p.PackagePart.Uri.ToString() == package_part_footer_first));
// footer_odd.Paragraphs.ForEach(p => Debug.Assert(p.PackagePart.Uri.ToString() == package_part_footer_odd));
// footer_even.Paragraphs.ForEach(p => Debug.Assert(p.PackagePart.Uri.ToString() == package_part_footer_even));
//}
document.SaveAs("output2.docx");
}
} }
} }
} }

BIN
DocX/Help/Changes in this version 1.0.0.9.docx Wyświetl plik


BIN
DocX/Help/DocX v1.0.0.8 - Documentation.chm Wyświetl plik


+ 149
- 41
UnitTests/UnitTest1.cs Wyświetl plik

using System.Linq; using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using Novacode; using Novacode;
using System.Diagnostics;
using System.Reflection;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace UnitTests namespace UnitTests
{ {
[TestClass] [TestClass]
public class UnitTest1 public class UnitTest1
{ {
// Get the fullpath to the executing assembly.
string directory_executing_assembly;
string directory_documents;
string file_temp = "temp.docx";
const string package_part_document = "/word/document.xml";
public UnitTest1() public UnitTest1()
{ {
TestMethod1();
}
directory_executing_assembly = Assembly.GetExecutingAssembly().Location;
private TestContext testContextInstance;
// The documents directory
List<string> steps = directory_executing_assembly.Split('\\').ToList();
steps.RemoveRange(steps.Count() - 3, 3);
directory_documents = String.Join("\\", steps) + "\\documents\\";
}
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
[TestMethod]
public void Test_Tables()
{ {
get
using (DocX document = DocX.Load(directory_documents + "Tables.docx"))
{ {
return testContextInstance;
// There is only one Paragraph at the document level.
Assert.IsTrue(document.Paragraphs.Count() == 1);
// There is only one Table in this document.
Assert.IsTrue(document.Tables.Count() == 1);
// Extract the only Table.
Table t0 = document.Tables[0];
// This table has 12 Paragraphs.
Assert.IsTrue(t0.Paragraphs.Count() == 12);
} }
set
}
[TestMethod]
public void Test_Images()
{
using (DocX document = DocX.Load(directory_documents + "Images.docx"))
{ {
testContextInstance = value;
// Extract Images from Document.
List<Novacode.Image> document_images = document.Images;
// Make sure there are 3 Images in this document.
Assert.IsTrue(document_images.Count() == 3);
// Extract the headers from this document.
Headers headers = document.Headers;
Header header_first = headers.first;
Header header_odd = headers.odd;
Header header_even = headers.even;
#region Header_First
// Extract Images from the first Header.
List<Novacode.Image> header_first_images = header_first.Images;
// Make sure there is 1 Image in the first header.
Assert.IsTrue(header_first_images.Count() == 1);
#endregion
#region Header_Odd
// Extract Images from the odd Header.
List<Novacode.Image> header_odd_images = header_odd.Images;
// Make sure there is 1 Image in the first header.
Assert.IsTrue(header_odd_images.Count() == 1);
#endregion
#region Header_Even
// Extract Images from the odd Header.
List<Novacode.Image> header_even_images = header_even.Images;
// Make sure there is 1 Image in the first header.
Assert.IsTrue(header_even_images.Count() == 1);
#endregion
} }
} }
#region Additional test attributes
//
// You can use the following additional attributes as you write your tests:
//
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
#endregion
// Write the string "Hello World" into this Image.
private static void CoolExample(Novacode.Image i, Stream s, string str)
{
// Write "Hello World" into this Image.
Bitmap b = new Bitmap(s);
/*
* Get the Graphics object for this Bitmap.
* The Graphics object provides functions for drawing.
*/
Graphics g = Graphics.FromImage(b);
// Draw the string "Hello World".
g.DrawString
(
str,
new Font("Tahoma", 20),
Brushes.Blue,
new PointF(0, 0)
);
// Save this Bitmap back into the document using a Create\Write stream.
b.Save(i.GetStream(FileMode.Create, FileAccess.Write), ImageFormat.Png);
}
[TestMethod] [TestMethod]
public void TestMethod1()
public void Test_Document_Paragraphs()
{ {
using (DocX document = DocX.Load(@"C:\Users\cathal\Documents\Visual Studio 2010\Projects\DocX\UnitTests\documents\simple.docx"))
// Load the document 'Paragraphs.docx'
using (DocX document = DocX.Load(directory_documents + "Paragraphs.docx"))
{ {
Debug.Assert(document.Paragraphs.Count() == 1);
Paragraph p = document.Paragraphs[0];
Debug.Assert(p.Text == "Paragraph 1");
Debug.Assert(p.Pictures.Count == 1);
// Extract the Paragraphs from this document.
List<Paragraph> paragraphs = document.Paragraphs;
// There should be 3 Paragraphs in this document.
Assert.IsTrue(paragraphs.Count() == 3);
// Extract the 3 Paragraphs.
Paragraph p1 = paragraphs[0];
Paragraph p2 = paragraphs[1];
Paragraph p3 = paragraphs[2];
// Extract their Text properties.
string p1_text = p1.Text;
string p2_text = p2.Text;
string p3_text = p3.Text;
// Test their Text properties against absolutes.
Assert.IsTrue(p1_text == "Paragraph 1");
Assert.IsTrue(p2_text == "Paragraph 2");
Assert.IsTrue(p3_text == "Paragraph 3");
// Create a string to append to each Paragraph.
string appended_text = "foo bar foo";
// Test the appending of text to each Paragraph.
Assert.IsTrue(p1.Append(appended_text).Text == p1_text + appended_text);
Assert.IsTrue(p2.Append(appended_text).Text == p2_text + appended_text);
Assert.IsTrue(p3.Append(appended_text).Text == p3_text + appended_text);
// Test FindAll
List<int> p1_foos = p1.FindAll("foo");
Assert.IsTrue(p1_foos.Count() == 2 && p1_foos[0] == 11 && p1_foos[1] == 19);
// Test ReplaceText
p2.ReplaceText("foo", "bar", false);
Assert.IsTrue(p2.Text == "Paragraph 2bar bar bar");
// Test RemoveText
p3.RemoveText(1, 3, false);
Assert.IsTrue(p3.Text == "Pgraph 3foo bar foo");
// Its important that each Paragraph knows the PackagePart it belongs to.
document.Paragraphs.ForEach(p => Assert.IsTrue(p.PackagePart.Uri.ToString() == package_part_document));
// Test the saving of the document.
document.SaveAs(file_temp);
} }
// Delete the tempory file.
File.Delete(file_temp);
} }
} }
} }

+ 12
- 0
UnitTests/UnitTests.csproj Wyświetl plik

<ItemGroup> <ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" /> <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core"> <Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework> <RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference> </Reference>
<Reference Include="System.Data" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Drawing" />
<Reference Include="System.XML" />
<Reference Include="System.Xml.Linq" />
<Reference Include="WindowsBase" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies"> <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
<Name>DocX</Name> <Name>DocX</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="documents\Images.docx" />
<None Include="documents\Paragraphs.docx" />
<None Include="documents\Tables.docx" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.

BIN
UnitTests/documents/Images.docx Wyświetl plik


BIN
UnitTests/documents/Paragraphs.docx Wyświetl plik


BIN
UnitTests/documents/Tables.docx Wyświetl plik


Ładowanie…
Anuluj
Zapisz