소스 검색

ApplyTemplate bag has been fixed.

Added AppendEquation method into Paragraph and InsertEquation method into Container/Header/Footer/DocX classes.
master
DragonFire_cp 14 년 전
부모
커밋
c0f5ba0eed
8개의 변경된 파일440개의 추가작업 그리고 331개의 파일을 삭제
  1. 7
    0
      DocX/Container.cs
  2. 329
    311
      DocX/DocX.cs
  3. 7
    0
      DocX/Footer.cs
  4. 8
    0
      DocX/Header.cs
  5. 45
    0
      DocX/Paragraph.cs
  6. 2
    2
      DocX/_BaseClasses.cs
  7. 41
    17
      Examples/Program.cs
  8. 1
    1
      TraceAndTestImpact.testsettings

+ 7
- 0
DocX/Container.cs 파일 보기

@@ -413,6 +413,13 @@ namespace Novacode
return Paragraphs.Last();
}
public virtual Paragraph InsertEquation(string equation)
{
Paragraph p = InsertParagraph();
p.AppendEquation(equation);
return p;
}
public Table InsertTable(int coloumnCount, int rowCount)
{
XElement newTable = HelperFunctions.CreateTable(rowCount, coloumnCount);

+ 329
- 311
DocX/DocX.cs 파일 보기

@@ -16,13 +16,14 @@ namespace Novacode
/// <summary>
/// Represents a document.
/// </summary>
public class DocX: Container, IDisposable
public class DocX : Container, IDisposable
{
#region Namespaces
static internal XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
static internal XNamespace rel = "http://schemas.openxmlformats.org/package/2006/relationships";
static internal XNamespace r = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
static internal XNamespace m = "http://schemas.openxmlformats.org/officeDocument/2006/math";
static internal XNamespace customPropertiesSchema = "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties";
static internal XNamespace customVTypesSchema = "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes";
#endregion
@@ -129,7 +130,7 @@ namespace Novacode
settings.Root.AddFirst(documentProtection);
}
/// <summary>
/// Remove editing protection from this document.
/// </summary>
@@ -155,7 +156,7 @@ namespace Novacode
settings.Descendants(XName.Get("documentProtection", DocX.w.NamespaceName)).Remove();
}
public PageLayout PageLayout
public PageLayout PageLayout
{
get
{
@@ -197,12 +198,12 @@ namespace Novacode
/// }
/// </code>
/// </example>
public Headers Headers
public Headers Headers
{
get
get
{
return headers;
}
}
}
private Headers headers;
@@ -342,7 +343,7 @@ namespace Novacode
{
XElement body = mainDoc.Root.Element(w + "body");
XElement sectPr = body.Element(w + "sectPr");
if (sectPr != null)
{
XElement titlePg = sectPr.Element(w + "titlePg");
@@ -363,7 +364,7 @@ namespace Novacode
body.Add(new XElement(w + "sectPr", string.Empty));
sectPr = body.Element(w + "sectPr");
titlePg = sectPr.Element(w + "titlePg");
if (titlePg == null)
{
@@ -408,7 +409,7 @@ namespace Novacode
{
// Get the Xml file for this Header or Footer.
Uri partUri = mainPart.GetRelationship(Id).TargetUri;
// Weird problem with PackaePart API.
if (!partUri.OriginalString.StartsWith("/word/"))
partUri = new Uri("/word/" + partUri.OriginalString, UriKind.Relative);
@@ -463,8 +464,9 @@ namespace Novacode
internal Stream stream;
#endregion
internal DocX(DocX document, XElement xml): base(document, xml)
{
internal DocX(DocX document, XElement xml)
: base(document, xml)
{
}
@@ -492,7 +494,7 @@ namespace Novacode
/// <seealso cref="Paragraph.InsertPicture"/>
public List<Image> Images
{
get
get
{
PackageRelationshipCollection imageRelationships = mainPart.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image");
if (imageRelationships.Count() > 0)
@@ -563,7 +565,7 @@ namespace Novacode
/// <seealso cref="AddCustomProperty"/>
public Dictionary<string, CustomProperty> CustomProperties
{
get
get
{
if (package.PartExists(new Uri("/docProps/custom.xml", UriKind.Relative)))
{
@@ -587,34 +589,34 @@ namespace Novacode
}
}
///<summary>
/// Returns the list of document core properties with corresponding values.
///</summary>
public Dictionary<string, string> CoreProperties
{
get
///<summary>
/// Returns the list of document core properties with corresponding values.
///</summary>
public Dictionary<string, string> CoreProperties
{
if (package.PartExists(new Uri("/docProps/core.xml", UriKind.Relative)))
{
PackagePart docProps_Core = package.GetPart(new Uri("/docProps/core.xml", UriKind.Relative));
XDocument corePropDoc;
using (TextReader tr = new StreamReader(docProps_Core.GetStream(FileMode.Open, FileAccess.Read)))
corePropDoc = XDocument.Load(tr, LoadOptions.PreserveWhitespace);
// Get all of the core properties in this document
return (from docProperty in corePropDoc.Root.Elements()
select
new KeyValuePair<string, string>(
string.Format(
"{0}:{1}",
corePropDoc.Root.GetPrefixOfNamespace(docProperty.Name.Namespace),
docProperty.Name.LocalName),
docProperty.Value)).ToDictionary(p => p.Key, v => v.Value);
}
get
{
if (package.PartExists(new Uri("/docProps/core.xml", UriKind.Relative)))
{
PackagePart docProps_Core = package.GetPart(new Uri("/docProps/core.xml", UriKind.Relative));
XDocument corePropDoc;
using (TextReader tr = new StreamReader(docProps_Core.GetStream(FileMode.Open, FileAccess.Read)))
corePropDoc = XDocument.Load(tr, LoadOptions.PreserveWhitespace);
// Get all of the core properties in this document
return (from docProperty in corePropDoc.Root.Elements()
select
new KeyValuePair<string, string>(
string.Format(
"{0}:{1}",
corePropDoc.Root.GetPrefixOfNamespace(docProperty.Name.Namespace),
docProperty.Name.LocalName),
docProperty.Value)).ToDictionary(p => p.Key, v => v.Value);
}
return new Dictionary<string, string>();
return new Dictionary<string, string>();
}
}
}
/// <summary>
/// Get the Text of this document.
@@ -747,7 +749,7 @@ namespace Novacode
// Create a list of internal and external style elements for easy iteration.
var internal_style_list = internal_word_styles.Root.Elements(XName.Get("style", DocX.w.NamespaceName));
var external_style_list = external_word_styles.Root.Elements(XName.Get("style", DocX.w.NamespaceName));
// Loop through the external style elements
foreach (XElement style in external_style_list)
{
@@ -792,12 +794,12 @@ namespace Novacode
string uri_string = rel.TargetUri.ToString();
if (!uri_string.StartsWith("/"))
uri_string = "/" + uri_string;
PackagePart external_image_part = rel.Package.GetPart(new Uri("/word" + uri_string, UriKind.RelativeOrAbsolute));
PackagePart internal_image_part = package.CreatePart(new Uri(string.Format("/word/media/image{0}.jpeg", max + 1), UriKind.RelativeOrAbsolute), System.Net.Mime.MediaTypeNames.Image.Jpeg);
PackageRelationship pr = internal_word_document.CreateRelationship(internal_image_part.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image");
var query = from e in external_elements.DescendantsAndSelf()
let embed = e.Attribute(XName.Get("embed", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"))
where embed != null && embed.Value == rel.Id
@@ -821,7 +823,7 @@ namespace Novacode
#endregion
#region CustomProperties
// Check if the external document contains custom properties.
if (document.package.PartExists(new Uri("/docProps/custom.xml", UriKind.Relative)))
{
@@ -841,7 +843,7 @@ namespace Novacode
if (!package.PartExists(new Uri("/docProps/custom.xml", UriKind.Relative)))
HelperFunctions.CreateCustomPropertiesPart(this);
PackagePart internal_docProps_custom = package.GetPart(new Uri("/docProps/custom.xml", UriKind.Relative));
XDocument internal_customPropDoc;
using (TextReader tr = new StreamReader(internal_docProps_custom.GetStream(FileMode.Open, FileAccess.Read)))
@@ -850,7 +852,7 @@ namespace Novacode
foreach (XElement cp in external_customProperties)
{
// Does the internal document already have a custom property with this name?
XElement conflict =
XElement conflict =
(
from d in internal_customPropDoc.Descendants(XName.Get("property", customPropertiesSchema.NamespaceName))
let ExternalName = d.Attribute(XName.Get("name", customPropertiesSchema.NamespaceName))
@@ -879,7 +881,7 @@ namespace Novacode
// A document can only have one header and one footer.
#region Remove external (header & footer) references
var externalHeaderAndFooterReferences =
var externalHeaderAndFooterReferences =
(
from d in external_elements.Descendants()
where d.Name.LocalName == "headerReference" || d.Name.LocalName == "footerReference"
@@ -891,7 +893,7 @@ namespace Novacode
#endregion
}
/// <summary>
/// Insert a new Table at the end of this document.
@@ -1070,7 +1072,7 @@ namespace Novacode
t.mainPart = mainPart;
return t;
}
/// <summary>
/// Creates a document using a Stream.
/// </summary>
@@ -1192,7 +1194,7 @@ namespace Novacode
// 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");
package.CreateRelationship(mainDocumentPart.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument");
// Load the document part into a XDocument object
using (TextReader tr = new StreamReader(mainDocumentPart.GetStream(FileMode.Create, FileAccess.ReadWrite)))
{
@@ -1257,7 +1259,7 @@ namespace Novacode
headers.odd = document.GetHeaderByType("default");
headers.even = document.GetHeaderByType("even");
headers.first = document.GetHeaderByType("first");
Footers footers = new Footers();
footers.odd = document.GetFooterByType("default");
footers.even = document.GetFooterByType("even");
@@ -1313,7 +1315,7 @@ namespace Novacode
//}
document.Xml = document.mainDoc.Root.Element(w + "body");
document.headers = headers;
@@ -1321,7 +1323,7 @@ namespace Novacode
document.settingsPart = HelperFunctions.CreateOrGetSettingsPart(package);
}
/// <summary>
/// <summary>
/// Loads a document into a DocX object using a Stream.
/// </summary>
/// <param name="stream">The Stream to load the document from.</param>
@@ -1434,7 +1436,7 @@ namespace Novacode
throw new FileNotFoundException(string.Format("File could not be found {0}", filename));
MemoryStream ms = new MemoryStream();
using (FileStream fs = new FileStream(filename, FileMode.Open))
{
byte[] data = new byte[fs.Length];
@@ -1460,7 +1462,7 @@ namespace Novacode
///<exception cref="FileNotFoundException">The document template file not found.</exception>
public void ApplyTemplate(string templateFilePath)
{
ApplyTemplate(templateFilePath, true);
ApplyTemplate(templateFilePath, true);
}
///<summary>
@@ -1471,14 +1473,14 @@ namespace Novacode
///<exception cref="FileNotFoundException">The document template file not found.</exception>
public void ApplyTemplate(string templateFilePath, bool includeContent)
{
if (!File.Exists(templateFilePath))
{
throw new FileNotFoundException(string.Format("File could not be found {0}", templateFilePath));
}
using (FileStream packageStream = new FileStream(templateFilePath, FileMode.Open, FileAccess.Read))
{
ApplyTemplate(packageStream, includeContent);
}
if (!File.Exists(templateFilePath))
{
throw new FileNotFoundException(string.Format("File could not be found {0}", templateFilePath));
}
using (FileStream packageStream = new FileStream(templateFilePath, FileMode.Open, FileAccess.Read))
{
ApplyTemplate(packageStream, includeContent);
}
}
///<summary>
@@ -1487,7 +1489,7 @@ namespace Novacode
///<param name="templateStream">The stream of the document template file.</param>
public void ApplyTemplate(Stream templateStream)
{
ApplyTemplate(templateStream, true);
ApplyTemplate(templateStream, true);
}
///<summary>
@@ -1497,110 +1499,116 @@ namespace Novacode
///<param name="includeContent">Whether to copy the document template text content to document.</param>
public void ApplyTemplate(Stream templateStream, bool includeContent)
{
Package templatePackage = Package.Open(templateStream);
try
{
PackagePart documentPart = null;
XDocument documentDoc = null;
foreach (PackagePart packagePart in templatePackage.GetParts())
Package templatePackage = Package.Open(templateStream);
try
{
switch (packagePart.Uri.ToString())
{
case "/word/document.xml":
documentPart = packagePart;
using (XmlReader xr = XmlReader.Create(packagePart.GetStream(FileMode.Open, FileAccess.Read)))
{
documentDoc = XDocument.Load(xr);
}
break;
case "/_rels/.rels":
if (!this.package.PartExists(packagePart.Uri))
{
this.package.CreatePart(packagePart.Uri, packagePart.ContentType, packagePart.CompressionOption);
}
PackagePart globalRelsPart = this.package.GetPart(packagePart.Uri);
using (
StreamReader tr = new StreamReader(
packagePart.GetStream(FileMode.Open, FileAccess.Read), Encoding.UTF8))
{
using (
StreamWriter tw = new StreamWriter(
globalRelsPart.GetStream(FileMode.Create, FileAccess.Write), Encoding.UTF8))
PackagePart documentPart = null;
XDocument documentDoc = null;
foreach (PackagePart packagePart in templatePackage.GetParts())
{
switch (packagePart.Uri.ToString())
{
tw.Write(tr.ReadToEnd());
case "/word/document.xml":
documentPart = packagePart;
using (XmlReader xr = XmlReader.Create(packagePart.GetStream(FileMode.Open, FileAccess.Read)))
{
documentDoc = XDocument.Load(xr);
}
break;
case "/_rels/.rels":
if (!this.package.PartExists(packagePart.Uri))
{
this.package.CreatePart(packagePart.Uri, packagePart.ContentType, packagePart.CompressionOption);
}
PackagePart globalRelsPart = this.package.GetPart(packagePart.Uri);
using (
StreamReader tr = new StreamReader(
packagePart.GetStream(FileMode.Open, FileAccess.Read), Encoding.UTF8))
{
using (
StreamWriter tw = new StreamWriter(
globalRelsPart.GetStream(FileMode.Create, FileAccess.Write), Encoding.UTF8))
{
tw.Write(tr.ReadToEnd());
}
}
break;
case "/word/_rels/document.xml.rels":
break;
default:
if (!this.package.PartExists(packagePart.Uri))
{
this.package.CreatePart(packagePart.Uri, packagePart.ContentType, packagePart.CompressionOption);
}
Encoding packagePartEncoding = Encoding.Default;
if (packagePart.Uri.ToString().EndsWith(".xml") || packagePart.Uri.ToString().EndsWith(".rels"))
{
packagePartEncoding = Encoding.UTF8;
}
PackagePart nativePart = this.package.GetPart(packagePart.Uri);
using (
StreamReader tr = new StreamReader(
packagePart.GetStream(FileMode.Open, FileAccess.Read), packagePartEncoding))
{
using (
StreamWriter tw = new StreamWriter(
nativePart.GetStream(FileMode.Create, FileAccess.Write), tr.CurrentEncoding))
{
tw.Write(tr.ReadToEnd());
}
}
break;
}
}
break;
case "/word/_rels/document.xml.rels":
break;
default:
if (!this.package.PartExists(packagePart.Uri))
{
this.package.CreatePart(packagePart.Uri, packagePart.ContentType, packagePart.CompressionOption);
}
Encoding packagePartEncoding = Encoding.Default;
if (packagePart.Uri.ToString().EndsWith(".xml") || packagePart.Uri.ToString().EndsWith(".rels"))
{
packagePartEncoding = Encoding.UTF8;
}
PackagePart nativePart = this.package.GetPart(packagePart.Uri);
using (
StreamReader tr = new StreamReader(
packagePart.GetStream(FileMode.Open, FileAccess.Read), packagePartEncoding))
{
using (
StreamWriter tw = new StreamWriter(
nativePart.GetStream(FileMode.Create, FileAccess.Write), tr.CurrentEncoding))
}
if (documentPart != null)
{
string mainContentType = documentPart.ContentType.Replace("template.main", "document.main");
if (this.package.PartExists(documentPart.Uri))
{
tw.Write(tr.ReadToEnd());
this.package.DeletePart(documentPart.Uri);
}
}
break;
}
}
if (documentPart != null)
{
string mainContentType = documentPart.ContentType.Replace("template.main", "document.main");
if (this.package.PartExists(documentPart.Uri))
{
this.package.DeletePart(documentPart.Uri);
}
PackagePart documentNewPart = this.package.CreatePart(
documentPart.Uri, mainContentType, documentPart.CompressionOption);
using (XmlWriter xw = XmlWriter.Create(documentNewPart.GetStream(FileMode.Create, FileAccess.Write)))
{
documentDoc.WriteTo(xw);
}
foreach (PackageRelationship documentPartRel in documentPart.GetRelationships())
{
documentNewPart.CreateRelationship(
documentPartRel.TargetUri,
documentPartRel.TargetMode,
documentPartRel.RelationshipType,
documentPartRel.Id);
}
this.mainPart = documentNewPart;
this.mainDoc = documentDoc;
PopulateDocument(this, templatePackage);
}
if (!includeContent)
{
foreach (Paragraph paragraph in this.Paragraphs)
{
paragraph.Remove(false);
}
PackagePart documentNewPart = this.package.CreatePart(
documentPart.Uri, mainContentType, documentPart.CompressionOption);
using (XmlWriter xw = XmlWriter.Create(documentNewPart.GetStream(FileMode.Create, FileAccess.Write)))
{
documentDoc.WriteTo(xw);
}
foreach (PackageRelationship documentPartRel in documentPart.GetRelationships())
{
documentNewPart.CreateRelationship(
documentPartRel.TargetUri,
documentPartRel.TargetMode,
documentPartRel.RelationshipType,
documentPartRel.Id);
}
this.mainPart = documentNewPart;
this.mainDoc = documentDoc;
PopulateDocument(this, templatePackage);
// DragonFire: I added next line and recovered ApplyTemplate method.
// I do it, becouse PopulateDocument(...) writes into field "settingsPart" the part of Template's package
// and after line "templatePackage.Close();" in finally, field "settingsPart" becomes not available and method "Save" throw an exception...
// That's why I recreated settingsParts and unlinked it from Template's package =)
settingsPart = HelperFunctions.CreateOrGetSettingsPart(package);
}
if (!includeContent)
{
foreach (Paragraph paragraph in this.Paragraphs)
{
paragraph.Remove(false);
}
}
}
}
finally
{
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)))
finally
{
tr.Read();
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)))
{
tr.Read();
}
templatePackage.Close();
}
templatePackage.Close();
}
}
/// <summary>
@@ -1715,7 +1723,7 @@ namespace Novacode
);
Hyperlink h = new Hyperlink(this, i);
h.Text = text;
h.Uri = uri;
@@ -1737,7 +1745,7 @@ namespace Novacode
using (TextReader tr = new StreamReader(package.GetPart(word_styles_Uri).GetStream()))
word_styles = XDocument.Load(tr);
bool hyperlinkStyleExists =
bool hyperlinkStyleExists =
(
from s in word_styles.Element(w + "styles").Elements()
let styleId = s.Attribute(XName.Get("styleId", w.NamespaceName))
@@ -1752,11 +1760,11 @@ namespace Novacode
w + "style",
new XAttribute(w + "type", "character"),
new XAttribute(w + "styleId", "Hyperlink"),
new XElement(w + "name", new XAttribute(w + "val", "Hyperlink")),
new XElement(w + "basedOn", new XAttribute(w + "val", "DefaultParagraphFont")),
new XElement(w + "uiPriority", new XAttribute(w + "val", "99")),
new XElement(w + "name", new XAttribute(w + "val", "Hyperlink")),
new XElement(w + "basedOn", new XAttribute(w + "val", "DefaultParagraphFont")),
new XElement(w + "uiPriority", new XAttribute(w + "val", "99")),
new XElement(w + "unhideWhenUsed"),
new XElement(w + "rsid", new XAttribute(w + "val", "0005416C")),
new XElement(w + "rsid", new XAttribute(w + "val", "0005416C")),
new XElement
(
w + "rPr",
@@ -2004,7 +2012,7 @@ namespace Novacode
{
XDocument relsPartContent;
using (TextReader tr = new StreamReader(relsPart.GetStream(FileMode.Open, FileAccess.Read)))
relsPartContent = XDocument.Load(tr);
relsPartContent = XDocument.Load(tr);
IEnumerable<XElement> imageRelationships =
relsPartContent.Root.Elements().Where
@@ -2029,7 +2037,7 @@ namespace Novacode
}
}
}
// Loop through each image part in this document.
foreach (PackagePart pp in imageParts)
{
@@ -2057,7 +2065,7 @@ namespace Novacode
// Create a new image part.
imgPartUriPath = string.Format
(
"/word/media/{0}.{1}",
"/word/media/{0}.{1}",
Guid.NewGuid().ToString(), // The unique part.
extension
);
@@ -2426,161 +2434,161 @@ namespace Novacode
this.stream = stream;
Save();
}
/// <summary>
/// Add a core property to this document. If a core property already exists with the same name it will be replaced. Core property names are case insensitive.
/// </summary>
///<param name="propertyName">The property name.</param>
///<param name="propertyValue">The property value.</param>
///<example>
/// Add a core properties of each type to a document.
/// <code>
/// // Load Example.docx
/// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
/// {
/// // If this document does not contain a core property called 'forename', create one.
/// if (!document.CoreProperties.ContainsKey("forename"))
/// {
/// // Create a new core property called 'forename' and set its value.
/// document.AddCoreProperty("forename", "Cathal");
/// }
///
/// // Get this documents core property called 'forename'.
/// string forenameValue = document.CoreProperties["forename"];
///
/// // Print all of the information about this core property to Console.
/// Console.WriteLine(string.Format("Name: '{0}', Value: '{1}'\nPress any key...", "forename", forenameValue));
///
/// // Save all changes made to this document.
/// document.Save();
/// } // Release this document from memory.
///
/// // Wait for the user to press a key before exiting.
/// Console.ReadKey();
/// </code>
/// </example>
/// <seealso cref="CoreProperties"/>
/// <seealso cref="CustomProperty"/>
/// <seealso cref="CustomProperties"/>
public void AddCoreProperty(string propertyName, string propertyValue)
{
string propertyNamespacePrefix = propertyName.Contains(":") ? propertyName.Split(new[] { ':' })[0] : "cp";
string propertyLocalName = propertyName.Contains(":") ? propertyName.Split(new[] { ':' })[1] : propertyName;
// If this document does not contain a coreFilePropertyPart create one.)
if (!package.PartExists(new Uri("/docProps/core.xml", UriKind.Relative)))
throw new Exception("Core properties part doesn't exist.");
XDocument corePropDoc;
PackagePart corePropPart = package.GetPart(new Uri("/docProps/core.xml", UriKind.Relative));
using (TextReader tr = new StreamReader(corePropPart.GetStream(FileMode.Open, FileAccess.Read)))
{
corePropDoc = XDocument.Load(tr);
}
XElement corePropElement =
(from propElement in corePropDoc.Root.Elements()
where (propElement.Name.LocalName.Equals(propertyLocalName))
select propElement).SingleOrDefault();
if (corePropElement != null)
{
corePropElement.SetValue(propertyValue);
}
else
{
var propertyNamespace = corePropDoc.Root.GetNamespaceOfPrefix(propertyNamespacePrefix);
corePropDoc.Root.Add(new XElement(XName.Get(propertyLocalName, propertyNamespace.NamespaceName), propertyValue));
}
using(TextWriter tw = new StreamWriter(corePropPart.GetStream(FileMode.Create, FileAccess.Write)))
{
corePropDoc.Save(tw);
}
UpdateCorePropertyValue(this, propertyLocalName, propertyValue);
}
internal static void UpdateCorePropertyValue(DocX document, string corePropertyName, string corePropertyValue)
{
string matchPattern = string.Format(@"(DOCPROPERTY)?{0}\\\*MERGEFORMAT", corePropertyName).ToLower();
foreach (XElement e in document.mainDoc.Descendants(XName.Get("fldSimple", w.NamespaceName)))
{
string attr_value = e.Attribute(XName.Get("instr", w.NamespaceName)).Value.Replace(" ", string.Empty).Trim().ToLower();
if (Regex.IsMatch(attr_value, matchPattern))
{
XElement firstRun = e.Element(w + "r");
XElement firstText = firstRun.Element(w + "t");
XElement rPr = firstText.Element(w + "rPr");
// Delete everything and insert updated text value
e.RemoveNodes();
XElement t = new XElement(w + "t", rPr, corePropertyValue);
Novacode.Text.PreserveSpace(t);
e.Add(new XElement(firstRun.Name, firstRun.Attributes(), firstRun.Element(XName.Get("rPr", w.NamespaceName)), t));
}
}
#region Headers
IEnumerable<PackagePart> headerParts = from headerPart in document.package.GetParts()
where (Regex.IsMatch(headerPart.Uri.ToString(), @"/word/header\d?.xml"))
select headerPart;
foreach (PackagePart pp in headerParts)
{
XDocument header = XDocument.Load(new StreamReader(pp.GetStream()));
foreach (XElement e in header.Descendants(XName.Get("fldSimple", w.NamespaceName)))
{
string attr_value = e.Attribute(XName.Get("instr", w.NamespaceName)).Value.Replace(" ", string.Empty).Trim().ToLower();
if (Regex.IsMatch(attr_value, matchPattern))
{
XElement firstRun = e.Element(w + "r");
// Delete everything and insert updated text value
e.RemoveNodes();
/// <summary>
/// Add a core property to this document. If a core property already exists with the same name it will be replaced. Core property names are case insensitive.
/// </summary>
///<param name="propertyName">The property name.</param>
///<param name="propertyValue">The property value.</param>
///<example>
/// Add a core properties of each type to a document.
/// <code>
/// // Load Example.docx
/// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
/// {
/// // If this document does not contain a core property called 'forename', create one.
/// if (!document.CoreProperties.ContainsKey("forename"))
/// {
/// // Create a new core property called 'forename' and set its value.
/// document.AddCoreProperty("forename", "Cathal");
/// }
///
/// // Get this documents core property called 'forename'.
/// string forenameValue = document.CoreProperties["forename"];
///
/// // Print all of the information about this core property to Console.
/// Console.WriteLine(string.Format("Name: '{0}', Value: '{1}'\nPress any key...", "forename", forenameValue));
///
/// // Save all changes made to this document.
/// document.Save();
/// } // Release this document from memory.
///
/// // Wait for the user to press a key before exiting.
/// Console.ReadKey();
/// </code>
/// </example>
/// <seealso cref="CoreProperties"/>
/// <seealso cref="CustomProperty"/>
/// <seealso cref="CustomProperties"/>
public void AddCoreProperty(string propertyName, string propertyValue)
{
string propertyNamespacePrefix = propertyName.Contains(":") ? propertyName.Split(new[] { ':' })[0] : "cp";
string propertyLocalName = propertyName.Contains(":") ? propertyName.Split(new[] { ':' })[1] : propertyName;
// If this document does not contain a coreFilePropertyPart create one.)
if (!package.PartExists(new Uri("/docProps/core.xml", UriKind.Relative)))
throw new Exception("Core properties part doesn't exist.");
XDocument corePropDoc;
PackagePart corePropPart = package.GetPart(new Uri("/docProps/core.xml", UriKind.Relative));
using (TextReader tr = new StreamReader(corePropPart.GetStream(FileMode.Open, FileAccess.Read)))
{
corePropDoc = XDocument.Load(tr);
}
XElement t = new XElement(w + "t", corePropertyValue);
Novacode.Text.PreserveSpace(t);
e.Add(new XElement(firstRun.Name, firstRun.Attributes(), firstRun.Element(XName.Get("rPr", w.NamespaceName)), t));
XElement corePropElement =
(from propElement in corePropDoc.Root.Elements()
where (propElement.Name.LocalName.Equals(propertyLocalName))
select propElement).SingleOrDefault();
if (corePropElement != null)
{
corePropElement.SetValue(propertyValue);
}
else
{
var propertyNamespace = corePropDoc.Root.GetNamespaceOfPrefix(propertyNamespacePrefix);
corePropDoc.Root.Add(new XElement(XName.Get(propertyLocalName, propertyNamespace.NamespaceName), propertyValue));
}
}
using (TextWriter tw = new StreamWriter(pp.GetStream(FileMode.Create, FileAccess.Write)))
header.Save(tw);
using (TextWriter tw = new StreamWriter(corePropPart.GetStream(FileMode.Create, FileAccess.Write)))
{
corePropDoc.Save(tw);
}
UpdateCorePropertyValue(this, propertyLocalName, propertyValue);
}
#endregion
#region Footers
IEnumerable<PackagePart> footerParts = from footerPart in document.package.GetParts()
where (Regex.IsMatch(footerPart.Uri.ToString(), @"/word/footer\d?.xml"))
select footerPart;
foreach (PackagePart pp in footerParts)
internal static void UpdateCorePropertyValue(DocX document, string corePropertyName, string corePropertyValue)
{
XDocument footer = XDocument.Load(new StreamReader(pp.GetStream()));
string matchPattern = string.Format(@"(DOCPROPERTY)?{0}\\\*MERGEFORMAT", corePropertyName).ToLower();
foreach (XElement e in document.mainDoc.Descendants(XName.Get("fldSimple", w.NamespaceName)))
{
string attr_value = e.Attribute(XName.Get("instr", w.NamespaceName)).Value.Replace(" ", string.Empty).Trim().ToLower();
if (Regex.IsMatch(attr_value, matchPattern))
{
XElement firstRun = e.Element(w + "r");
XElement firstText = firstRun.Element(w + "t");
XElement rPr = firstText.Element(w + "rPr");
// Delete everything and insert updated text value
e.RemoveNodes();
XElement t = new XElement(w + "t", rPr, corePropertyValue);
Novacode.Text.PreserveSpace(t);
e.Add(new XElement(firstRun.Name, firstRun.Attributes(), firstRun.Element(XName.Get("rPr", w.NamespaceName)), t));
}
}
foreach (XElement e in footer.Descendants(XName.Get("fldSimple", w.NamespaceName)))
{
string attr_value = e.Attribute(XName.Get("instr", w.NamespaceName)).Value.Replace(" ", string.Empty).Trim().ToLower();
if (Regex.IsMatch(attr_value, matchPattern))
#region Headers
IEnumerable<PackagePart> headerParts = from headerPart in document.package.GetParts()
where (Regex.IsMatch(headerPart.Uri.ToString(), @"/word/header\d?.xml"))
select headerPart;
foreach (PackagePart pp in headerParts)
{
XElement firstRun = e.Element(w + "r");
XDocument header = XDocument.Load(new StreamReader(pp.GetStream()));
foreach (XElement e in header.Descendants(XName.Get("fldSimple", w.NamespaceName)))
{
string attr_value = e.Attribute(XName.Get("instr", w.NamespaceName)).Value.Replace(" ", string.Empty).Trim().ToLower();
if (Regex.IsMatch(attr_value, matchPattern))
{
XElement firstRun = e.Element(w + "r");
// Delete everything and insert updated text value
e.RemoveNodes();
// Delete everything and insert updated text value
e.RemoveNodes();
XElement t = new XElement(w + "t", corePropertyValue);
Novacode.Text.PreserveSpace(t);
e.Add(new XElement(firstRun.Name, firstRun.Attributes(), firstRun.Element(XName.Get("rPr", w.NamespaceName)), t));
}
}
XElement t = new XElement(w + "t", corePropertyValue);
Novacode.Text.PreserveSpace(t);
e.Add(new XElement(firstRun.Name, firstRun.Attributes(), firstRun.Element(XName.Get("rPr", w.NamespaceName)), t));
using (TextWriter tw = new StreamWriter(pp.GetStream(FileMode.Create, FileAccess.Write)))
header.Save(tw);
}
}
#endregion
#region Footers
IEnumerable<PackagePart> footerParts = from footerPart in document.package.GetParts()
where (Regex.IsMatch(footerPart.Uri.ToString(), @"/word/footer\d?.xml"))
select footerPart;
foreach (PackagePart pp in footerParts)
{
XDocument footer = XDocument.Load(new StreamReader(pp.GetStream()));
using (TextWriter tw = new StreamWriter(pp.GetStream(FileMode.Create, FileAccess.Write)))
footer.Save(tw);
foreach (XElement e in footer.Descendants(XName.Get("fldSimple", w.NamespaceName)))
{
string attr_value = e.Attribute(XName.Get("instr", w.NamespaceName)).Value.Replace(" ", string.Empty).Trim().ToLower();
if (Regex.IsMatch(attr_value, matchPattern))
{
XElement firstRun = e.Element(w + "r");
// Delete everything and insert updated text value
e.RemoveNodes();
XElement t = new XElement(w + "t", corePropertyValue);
Novacode.Text.PreserveSpace(t);
e.Add(new XElement(firstRun.Name, firstRun.Attributes(), firstRun.Element(XName.Get("rPr", w.NamespaceName)), t));
}
}
using (TextWriter tw = new StreamWriter(pp.GetStream(FileMode.Create, FileAccess.Write)))
footer.Save(tw);
}
#endregion
PopulateDocument(document, document.package);
}
#endregion
PopulateDocument(document, document.package);
}
/// <summary>
/// Add a custom property to this document. If a custom property already exists with the same name it will be replace. CustomProperty names are case insensitive.
@@ -2621,7 +2629,7 @@ namespace Novacode
public void AddCustomProperty(CustomProperty cp)
{
// If this document does not contain a customFilePropertyPart create one.
if(!package.PartExists(new Uri("/docProps/custom.xml", UriKind.Relative)))
if (!package.PartExists(new Uri("/docProps/custom.xml", UriKind.Relative)))
HelperFunctions.CreateCustomPropertiesPart(this);
XDocument customPropDoc;
@@ -2674,7 +2682,7 @@ namespace Novacode
UpdateCustomPropertyValue(this, cp.Name, cp.Value.ToString());
}
internal static void UpdateCustomPropertyValue(DocX document, string customPropertyName, string customPropertyValue)
{
@@ -2685,7 +2693,7 @@ namespace Novacode
if (attr_value.Equals(match_value, StringComparison.CurrentCultureIgnoreCase))
{
XElement firstRun = e.Element(w + "r");
XElement firstRun = e.Element(w + "r");
XElement firstText = firstRun.Element(w + "t");
XElement rPr = firstText.Element(w + "rPr");
@@ -2824,6 +2832,16 @@ namespace Novacode
}
}
/// <summary>
/// Create an equation and insert it in the new paragraph
/// </summary>
public override Paragraph InsertEquation(String equation)
{
Paragraph p = base.InsertEquation(equation);
p.PackagePart = mainPart;
return p;
}
#region IDisposable Members
/// <summary>

+ 7
- 0
DocX/Footer.cs 파일 보기

@@ -111,6 +111,13 @@ namespace Novacode
return p;
}
public override Paragraph InsertEquation(String equation)
{
Paragraph p = base.InsertEquation(equation);
p.PackagePart = mainPart;
return p;
}
public override List<Paragraph> Paragraphs
{
get

+ 8
- 0
DocX/Header.cs 파일 보기

@@ -115,6 +115,14 @@ namespace Novacode
return p;
}
public override Paragraph InsertEquation(String equation)
{
Paragraph p = base.InsertEquation(equation);
p.PackagePart = mainPart;
return p;
}
public override List<Paragraph> Paragraphs
{
get

+ 45
- 0
DocX/Paragraph.cs 파일 보기

@@ -1969,6 +1969,51 @@ namespace Novacode
return this;
}
/// <summary>
/// Add an equation to a document.
/// </summary>
/// <param name="equation">The Equation to append.</param>
/// <returns>The Paragraph with the Equation now appended.</returns>
/// <example>
/// Add an equation to a document.
/// <code>
/// using (DocX document = DocX.Create("Test.docx"))
/// {
/// // Add an equation to the document.
/// document.AddEquation("x=y+z");
///
/// // Save all changes made to this document.
/// document.Save();
/// }
/// </code>
/// </example>
public Paragraph AppendEquation(String equation)
{
// Create equation element
XElement oMathPara =
new XElement
(
XName.Get("oMathPara", DocX.m.NamespaceName),
new XElement
(
XName.Get("oMath", DocX.m.NamespaceName),
new XElement
(
XName.Get("r", DocX.w.NamespaceName),
new Formatting() { FontFamily = new System.Drawing.FontFamily("Cambria Math") }.Xml, // create formatting
new XElement(XName.Get("t", DocX.m.NamespaceName), equation) // create equation string
)
)
);
// Add equation element into paragraph xml and update runs collection
Xml.Add(oMathPara);
runs = Xml.Elements(XName.Get("oMathPara", DocX.m.NamespaceName)).ToList();
// Return paragraph with equation
return this;
}
internal string GetOrGenerateRel(Picture p)
{
string image_uri_string = p.img.pr.TargetUri.OriginalString;

+ 2
- 2
DocX/_BaseClasses.cs 파일 보기

@@ -47,9 +47,9 @@ namespace Novacode
/// This class provides functions for inserting new DocXElements before or after the current DocXElement.
/// Only certain DocXElements can support these functions without creating invalid documents, at the moment these are Paragraphs and Table.
/// </summary>
public abstract class InsertBeforeOrAfter:DocXElement
public abstract class InsertBeforeOrAfter : DocXElement
{
public InsertBeforeOrAfter(DocX document, XElement xml):base(document, xml) { }
public InsertBeforeOrAfter(DocX document, XElement xml) : base(document, xml) { }
public virtual void InsertPageBreakBeforeSelf()
{

+ 41
- 17
Examples/Program.cs 파일 보기

@@ -24,6 +24,8 @@ namespace Examples
HeadersAndFooters();
HyperlinksImagesTables();
Equations();
// Intermediate
Console.WriteLine("\nRunning Intermediate Examples");
CreateInvoice();
@@ -37,13 +39,35 @@ namespace Examples
Console.ReadKey();
}
/// <summary>
/// Create a document with two equations.
/// </summary>
private static void Equations()
{
Console.WriteLine("\nEquations()");
// Create a new document.
using (DocX document = DocX.Create(@"docs\Equations.docx"))
{
// Insert first Equation in this document.
Paragraph pEquation1 = document.InsertEquation("x = y+z");
// Insert second Equation in this document and add formatting.
Paragraph pEquation2 = document.InsertEquation("x = (y+z)/t").FontSize(18).Color(Color.Blue);
// Save this document to disk.
document.Save();
Console.WriteLine("\tCreated: docs\\Equations.docx\n");
}
}
/// <summary>
/// Create a document with a Paragraph whos first line is indented.
/// </summary>
private static void Indentation()
{
Console.WriteLine("\tIndentation()");
// Create a new document.
using (DocX document = DocX.Create(@"docs\Indentation.docx"))
{
@@ -270,7 +294,7 @@ namespace Examples
// Add the Happy Builders logo to this document.
Novacode.Image logo = template.AddImage(@"images\logo_the_happy_builder.png");
// Insert the Happy Builders logo into this Paragraph.
logo_paragraph.InsertPicture(logo.CreatePicture());
#endregion
@@ -295,7 +319,7 @@ namespace Examples
// Set the value of the custom property 'hired_company_details_line_one' and 'hired_company_details_line_two'.
template.AddCustomProperty(new CustomProperty("hired_company_details_line_one", "Business Street, Dublin, 12345"));
template.AddCustomProperty(new CustomProperty("hired_company_details_line_two", "Phone: 012-345-6789, Fax: 012-345-6789, e-mail: support@thehappybuilder.com"));
template.AddCustomProperty(new CustomProperty("hired_company_details_line_two", "Phone: 012-345-6789, Fax: 012-345-6789, e-mail: support@thehappybuilder.com"));
#endregion
/*
@@ -342,7 +366,7 @@ namespace Examples
CustomProperty company_name = new CustomProperty("company_name", "Company Name");
// Insert a field of type doc property (This will display the custom property 'company_name')
layout_table.Rows[0].Cells[0].Paragraphs[0].InsertDocProperty(company_name, f:dark_formatting);
layout_table.Rows[0].Cells[0].Paragraphs[0].InsertDocProperty(company_name, f: dark_formatting);
// Force the next text insert to be on a new line.
upper_left_paragraph.InsertText("\n", false);
@@ -353,7 +377,7 @@ namespace Examples
CustomProperty company_slogan = new CustomProperty("company_slogan", "Company slogan goes here.");
// Insert a field of type doc property (This will display the custom property 'company_slogan')
upper_left_paragraph.InsertDocProperty(company_slogan, f:light_formatting);
upper_left_paragraph.InsertDocProperty(company_slogan, f: light_formatting);
#endregion
#region Company Logo
@@ -362,10 +386,10 @@ namespace Examples
// Add a template logo image to this document.
Novacode.Image logo = document.AddImage(@"images\logo_template.png");
// Insert this template logo into the upper right Paragraph.
upper_right_paragraph.InsertPicture(logo.CreatePicture());
upper_right_paragraph.Alignment = Alignment.right;
#endregion
@@ -379,7 +403,7 @@ namespace Examples
lower_left_paragraph.InsertText("TO:\n", false, dark_formatting);
// Insert a field of type doc property (This will display the custom property 'hired_company_address_line_one')
lower_left_paragraph.InsertDocProperty(hired_company_address_line_one, f:light_formatting);
lower_left_paragraph.InsertDocProperty(hired_company_address_line_one, f: light_formatting);
// Force the next text insert to be on a new line.
lower_left_paragraph.InsertText("\n", false);
@@ -388,7 +412,7 @@ namespace Examples
CustomProperty hired_company_address_line_two = new CustomProperty("hired_company_address_line_two", "City,");
// Insert a field of type doc property (This will display the custom property 'hired_company_address_line_two')
lower_left_paragraph.InsertDocProperty(hired_company_address_line_two, f:light_formatting);
lower_left_paragraph.InsertDocProperty(hired_company_address_line_two, f: light_formatting);
// Force the next text insert to be on a new line.
lower_left_paragraph.InsertText("\n", false);
@@ -397,7 +421,7 @@ namespace Examples
CustomProperty hired_company_address_line_three = new CustomProperty("hired_company_address_line_three", "Zip Code");
// Insert a field of type doc property (This will display the custom property 'hired_company_address_line_three')
lower_left_paragraph.InsertDocProperty(hired_company_address_line_three, f:light_formatting);
lower_left_paragraph.InsertDocProperty(hired_company_address_line_three, f: light_formatting);
#endregion
#region Date & Invoice number
@@ -406,12 +430,12 @@ namespace Examples
CustomProperty invoice_date = new CustomProperty("invoice_date", DateTime.Today.Date.ToString("d"));
lower_right_paragraph.InsertText("Date: ", false, dark_formatting);
lower_right_paragraph.InsertDocProperty(invoice_date, f:light_formatting);
lower_right_paragraph.InsertDocProperty(invoice_date, f: light_formatting);
CustomProperty invoice_number = new CustomProperty("invoice_number", 1);
lower_right_paragraph.InsertText("\nInvoice: ", false, dark_formatting);
lower_right_paragraph.InsertText("#", false, light_formatting);
lower_right_paragraph.InsertDocProperty(invoice_number, f:light_formatting);
lower_right_paragraph.InsertDocProperty(invoice_number, f: light_formatting);
lower_right_paragraph.Alignment = Alignment.right;
#endregion
@@ -433,10 +457,10 @@ namespace Examples
CustomProperty hired_company_details_line_two = new CustomProperty("hired_company_details_line_two", "Phone: 000-000-0000, Fax: 000-000-0000, e-mail: support@companyname.com");
Paragraph companyDetails = document.InsertParagraph(string.Empty, false);
companyDetails.InsertDocProperty(hired_company_details_line_one, f:light_formatting);
companyDetails.InsertDocProperty(hired_company_details_line_one, f: light_formatting);
companyDetails.InsertText("\n", false);
companyDetails.InsertDocProperty(hired_company_details_line_two, f:light_formatting);
companyDetails.Alignment = Alignment.center;
companyDetails.InsertDocProperty(hired_company_details_line_two, f: light_formatting);
companyDetails.Alignment = Alignment.center;
#endregion
// Return the document now that it has been created.
@@ -574,7 +598,7 @@ namespace Examples
return table;
}
/// <summary>
/// Creates a simple document with the text Hello World.
/// </summary>
@@ -648,7 +672,7 @@ namespace Examples
Console.WriteLine("\tCreated: docs\\Output.docx\n");
}
}
/// <summary>
/// For each of the documents in the folder 'docs\',
/// Replace the string a with the string b,

+ 1
- 1
TraceAndTestImpact.testsettings 파일 보기

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<TestSettings name="Trace and Test Impact" enableDefaultDataCollectors="true" id="ddea5473-b23c-4e19-9fc6-bd924ed99e12" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<TestSettings name="Trace and Test Impact" id="ddea5473-b23c-4e19-9fc6-bd924ed99e12" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<Description>These are test settings for Trace and Test Impact.</Description>
<Execution>
<TestTypeSpecific />

Loading…
취소
저장