Quellcode durchsuchen

Fixed a bug with InsertParagraphAfterSelf and InsertParagraphBeforeSelf.

Big thanks to np83 not only for finding this bug but also posting a fix and unit tests.
master
coffeycathal_cp vor 15 Jahren
Ursprung
Commit
f54ef25657

+ 68
- 0
ConsoleApplication3/ConsoleApplication3.csproj Datei anzeigen

@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{8A8EACDE-66DD-4F07-83C3-7E2241942A36}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ConsoleApplication3</RootNamespace>
<AssemblyName>ConsoleApplication3</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
<SccProjectName>SAK</SccProjectName>
<SccLocalPath>SAK</SccLocalPath>
<SccAuxPath>SAK</SccAuxPath>
<SccProvider>SAK</SccProvider>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DocX\DocX.csproj">
<Project>{E863D072-AA8B-4108-B5F1-785241B37F67}</Project>
<Name>DocX</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- 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.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

+ 10
- 0
ConsoleApplication3/ConsoleApplication3.csproj.vspscc Datei anzeigen

@@ -0,0 +1,10 @@
""
{
"FILE_VERSION" = "9237"
"ENLISTMENT_CHOICE" = "NEVER"
"PROJECT_FILE_RELATIVE_PATH" = ""
"NUMBER_OF_EXCLUDED_FILES" = "0"
"ORIGINAL_PROJECT_FILE_PATH" = ""
"NUMBER_OF_NESTED_PROJECTS" = "0"
"SOURCE_CONTROL_SETTINGS_PROVIDER" = "PROVIDER"
}

+ 26
- 0
ConsoleApplication3/Program.cs Datei anzeigen

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Novacode;
using System.Drawing;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Add Headers to the document.
document.AddHeaders();
document.AddHeaders();
// Save the document.
document.Save();
}
}
}
}

+ 36
- 0
ConsoleApplication3/Properties/AssemblyInfo.cs Datei anzeigen

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ConsoleApplication3")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ConsoleApplication3")]
[assembly: AssemblyCopyright("Copyright © 2010")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7eb1e4cd-eb7d-43bc-8248-22fedacc46e6")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

+ 1
- 1
DocX/DocX.cs Datei anzeigen

@@ -1583,7 +1583,7 @@ namespace Novacode
}
finally
{
this.package.Flush();
this.package.Flush();
var documentRelsPart = this.package.GetPart(new Uri("/word/_rels/document.xml.rels", UriKind.Relative));
using (TextReader tr = new StreamReader(documentRelsPart.GetStream(FileMode.Open, FileAccess.Read)))
{

+ 24
- 8
DocX/Paragraph.cs Datei anzeigen

@@ -697,7 +697,9 @@ namespace Novacode
/// </example>
public override Paragraph InsertParagraphBeforeSelf(Paragraph p)
{
return base.InsertParagraphBeforeSelf(p);
Paragraph p2 = base.InsertParagraphBeforeSelf(p);
p2.PackagePart = mainPart;
return p2;
}
/// <summary>
@@ -723,7 +725,9 @@ namespace Novacode
/// </example>
public Paragraph InsertParagraphBeforeSelf(string text)
{
return InsertParagraphBeforeSelf(text, false, null);
Paragraph p = base.InsertParagraphBeforeSelf(text);
p.PackagePart = mainPart;
return p;
}
/// <summary>
@@ -750,7 +754,9 @@ namespace Novacode
/// </example>
public override Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges)
{
return base.InsertParagraphBeforeSelf(text, trackChanges);
Paragraph p = base.InsertParagraphBeforeSelf(text, trackChanges);
p.PackagePart = mainPart;
return p;
}
/// <summary>
@@ -781,7 +787,9 @@ namespace Novacode
/// </example>
public override Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges, Formatting formatting)
{
return base.InsertParagraphBeforeSelf(text, trackChanges, formatting);
Paragraph p = base.InsertParagraphBeforeSelf(text, trackChanges, formatting);
p.PackagePart = mainPart;
return p;
}
/// <summary>
@@ -1011,7 +1019,9 @@ namespace Novacode
/// </example>
public override Paragraph InsertParagraphAfterSelf(Paragraph p)
{
return base.InsertParagraphAfterSelf(p);
Paragraph p2 = base.InsertParagraphAfterSelf(p);
p2.PackagePart = mainPart;
return p2;
}
/// <summary>
@@ -1042,7 +1052,9 @@ namespace Novacode
/// </example>
public override Paragraph InsertParagraphAfterSelf(string text, bool trackChanges, Formatting formatting)
{
return base.InsertParagraphAfterSelf(text, trackChanges, formatting);
Paragraph p = base.InsertParagraphAfterSelf(text, trackChanges, formatting);
p.PackagePart = mainPart;
return p;
}
/// <summary>
@@ -1069,7 +1081,9 @@ namespace Novacode
/// </example>
public override Paragraph InsertParagraphAfterSelf(string text, bool trackChanges)
{
return base.InsertParagraphAfterSelf(text, trackChanges);
Paragraph p = base.InsertParagraphAfterSelf(text, trackChanges);
p.PackagePart = mainPart;
return p;
}
/// <summary>
@@ -1095,7 +1109,9 @@ namespace Novacode
/// </example>
public override Paragraph InsertParagraphAfterSelf(string text)
{
return base.InsertParagraphAfterSelf(text);
Paragraph p = base.InsertParagraphAfterSelf(text);
p.PackagePart = mainPart;
return p;
}
private void RebuildDocProperties()

+ 1
- 1
DocX/_BaseClasses.cs Datei anzeigen

@@ -139,7 +139,7 @@ namespace Novacode
newParagraph = Paragraph.CreateEdit(EditType.ins, DateTime.Now, newParagraph);
Xml.AddBeforeSelf(newParagraph);
XElement newlyInserted = Xml.ElementsBeforeSelf().First();
XElement newlyInserted = Xml.ElementsBeforeSelf().Last();
Paragraph p = new Paragraph(Document, newlyInserted, -1);

+ 46
- 0
UnitTests/UnitTest1.cs Datei anzeigen

@@ -117,6 +117,52 @@ namespace UnitTests
}
}
[TestMethod]
public void Test_Insert_Picture_ParagraphBeforeSelf()
{
// Create test document.
using (DocX document = DocX.Create(directory_documents + "Test.docx"))
{
// Add an Image to this document.
Novacode.Image img = document.AddImage(directory_documents + "purple.png");
// Create a Picture from this Image.
Picture pic = img.CreatePicture();
Assert.IsNotNull(pic);
// Main document.
Paragraph p0 = document.InsertParagraph("Hello");
Paragraph p1 = p0.InsertParagraphBeforeSelf("again");
p1.InsertPicture(pic, 3);
// Save this document.
document.Save();
}
}
[TestMethod]
public void Test_Insert_Picture_ParagraphAfterSelf()
{
// Create test document.
using (DocX document = DocX.Create(directory_documents + "Test.docx"))
{
// Add an Image to this document.
Novacode.Image img = document.AddImage(directory_documents + "purple.png");
// Create a Picture from this Image.
Picture pic = img.CreatePicture();
Assert.IsNotNull(pic);
// Main document.
Paragraph p0 = document.InsertParagraph("Hello");
Paragraph p1 = p0.InsertParagraphAfterSelf("again");
p1.InsertPicture(pic, 3);
// Save this document.
document.Save();
}
}
[TestMethod]
public void Test_EverybodyHasAHome_Created()
{

Laden…
Abbrechen
Speichern